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:
fs module to wrap. Defaults to
could also be used with something like
cache: An existing
node-lru instance 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
CachedFs instances can be backed by the same
instance. You can override this to explicitly force two
instances 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
implementation in-place. Defaults to
debug: Log when methods are called. Defaults to
context: The context to call the wrapped
in. (Probably not useful except internally). Defaults to the wrapped
stale : Passed to the
lru-cache constructor unless the
cache option is specified. See
the lru-cache README for
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-cache instance. Useful for checking
cache.itemCount, or purging all cached items via
cache.reset(), etc. See the lru-cache
argumentsStringifier: Function that turns an array of arguments
fs method into a cache key. Mostly exposed so it doesn't
have to be duplicated in the test suite.
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.