Caching wrapper for node.js' built-in fs module (or compatible)
Wrap node.js' fs module in a cached read-only version that exposes the same interface. Can speed up things if you're reading the same files and directories multiple times, and things don't change on disc.
Doesn't expose the functions that write to disc, so no cache invalidation is ever performed internally.
var CachedFs = require'cachedfs'fs = ;fsreadFile'foo.txt'fsreadFile'foo.txt'// Much faster this time!;;
You can also patch the built-in
fs module or a compatible one
in-place (this should be considered a bit experimental):
require'cachedfs'patchInPlace;require'fs'readFile'foo.txt'// Yup, this will be cached!;
CachedFs constructor and
CachedFs.patchInPlace support an
options object with the following properties:
fsmodule to wrap. Defaults to
require('fs'), but could also be used with something like gitfakefs.
cache: An existing
node-lruinstance to use for the cached data. The default is to create a new one (exposed via
cacheKeyPrefix: Defaults to a session-unique number so that multiple
CachedFsinstances can be backed by the same
lru-cacheinstance. You can override this to explicitly force two
CachedFsinstances to share the same cached data for some reason.
skipUnimplemented: Don't add "not implemented" stubs that throw exceptions. Mostly useful when patching an existing
fsimplementation in-place. Defaults to
debug: Log when methods are called. Defaults to
context: The context to call the wrapped
fsfunctions in. (Probably not useful except internally). Defaults to the wrapped
stale: Passed to the
lru-cacheconstructor unless the
cacheoption is specified. See the lru-cache README for details.
If you don't specify a
length option, it will default to a function
that approximates the number of bytes occupied by the cached
values. That means you can use the
max option to set an upper limit
on the memory usage in bytes:
var CachedFs = require'cachedfs'cachedFs = max: 104857600;
or when patching the built-in
fs module in-place:
var CachedFs = require'cachedfs';CachedFspatchInPlacemax: 104857600;
CachedFs has the following properties:
cacheKeyPrefix: The string prefix of all keys stored in the cache.
lru-cacheinstance. Useful for checking
cache.itemCount, or purging all cached items via
cache.reset(), etc. See the lru-cache README.
argumentsStringifier: Function that turns an array of arguments for a
fsmethod into a cache key. Mostly exposed so it doesn't have to be duplicated in the test suite.
- File names are absolutified and normalized before being used in a cache key, so you'll get cache hits even if you refer to the same file with different syntaxes, eg. a relative and an absolute path.
- Errors are also cached.
- Even if the underlying
fsimplementation doesn't support a given sync method, it will produce the correct result if the CachedFs instance happens to have a cached copy of the async method's result.
Make sure you have node.js and npm installed, then run:
npm install cachedfs
3-clause BSD license -- see the
LICENSE file for details.