block-cache
v2.0.0
Published
fs read (incl. stream) operations cached in an lru-cache for consistent memory usage with custom fs support (hyperdrive).
Downloads
5
Maintainers
Readme
block-cache
block-cache
is a transparent(ish) cache that keeps data split in blocks in
an in-memory lru-cache. This is useful if you want to process a file, reusing
previously downloaded parts and improving the general performance without
caching more than your given memory limit.
The cache does not expose the passed-in API at any point which makes it suitable as a Sandbox.
npm i block-cache --save
Usage
The API of block-cache
is comparable to the
fs
API but all callbacks are optional and
if omitted will result in a Promise returned.
Here is a simple example of reading a file into the local cache.
const fs = require('fs')
const {Cache, CachedFile} = require('block-cache')
const cache = new Cache(fs, {
blkSize: 1024,
cacheSize: 2 * 1024 * 1024 // 2 MB
})
const fp = await cache.open('./Readme.md')
const data = await cache.read(fp)
console.log(data)
await cache.close(fp)
This example reads the entirety of the ./Readme.md
file into a 2 mega-byte
cache in 1 kilo-byte sized blocks and then closes the data. Even if the fp is
closed: the block stay in the cache!
Use-case: file parsing
This library usually comes in play when you have to parse parts of a file depending on the header. Take the beginning of this GIF parser for example:
const fs = require('fs')
const {Cache, CachedFile} = require('block-cache')
const cache = new Cache(fs, {
blkSize: 1024,
cacheSize: 2 * 1024 * 1024 // 2 MB
})
const fp = await cache.open('./Readme.md')
const signature = (await fp.read(null, 0, 6)).toString()
if (signature === 'GIF87a' || signature === 'GIF89a') {
const packed = await fp.read(null, 0, 10)
// etc.
}
await cache.close(fp)
As you can see in this example code, it is necessary to read only parts of a file at a time. Very small parts. But most of those bytes are already present in the cache. So, while the first operation needed to read 1Kb of the file, the second operation can already use it from the cached data.
API
new Cache(fs[, opts])
fs
is a FileSystem (require('fs')
)) or Hyperdrive archive (object).opts.cache
is alru-cache
instance (object, optional).opts.cacheSize
is the size of the lru-cache to be created in case aopts.cache
is missing. Defaults toCache.DEFAULT_CACHE_SIZE
(integer).opts.blkSize
is the default size in bytes of a cache-block. Defaults toCachedFile.DEFAULT_BLK_SIZE
. (integer).opts.prefix
is an optional prefix that can be added to the cached data, useful if you want to reuse the sameopts.cache
for multipleCache
instances. Defaults to''
. (string)
cache.open(path[, opts, cb])
Creates a cached file pointer reference for a given path. Note: It will open
the file reference in r
mode.
path
path to read the file from (string).opts.blkSize
is the size in bytes of a cache-block. Defaults to theopts.blkSize
defined in theCache
.cb(Error, CachedFile)
is an optional async callback handler method. The method will return aPromise
if the callback is not defined.
cache.close(fp[, cb])
Closes a created file pointer reference. After closing, future requests
on the CachedFile
will result in an err.code === 'ERR_CLOSED
error.
fp
is aCachedFile
instance, created with.open
or.openSync
cb(Error)
is an optional async callback handler method. The method will return aPromise
if the callback is not defined.
cache.disconnect()
Disconnects the cache from the file system instance. Any future operations on
the Cache or CachedFile instances create with the Cache will result in
an err.code === 'ERR_DISCONNECTED'
error. Disconnect also closes all open
file pointer references on the underlying file system.
cache.openSync(path[, opts])
like cache.open
but synchronous.
cache.read(fd[, buffer, offset, length, position, cb])
Reads the content of an opened file into a given buffer.
fd
is aCachedFile
instance, created with.open
or.openSync
buffer
is aBuffer
instance to write into. Unlike the Node API, this is optional which means that the reader will create a buffer instance ifnull
orundefined
is passed-in.offset
is the offset in the buffer to start writing at.length
is an integer specifying the number of bytes to read into buffer, defaults to length of the file (integer).position
is an argument specifying where to begin reading from in the file. The file descriptor will remember the end of the last read in thefd.position
property. It will default to 0.cb(Error, Buffer)
is an optional async callback handler method. The method will return aPromise
if the callback is not defined.
cache.createReadStream(path[, opts, cb])
Creates a cached file pointer reference for a given path and then reads it through a stream.
path
is the path to read the file from (string).opts.blkSize
is the block size for each block to be cached. Defaults tocache.opts.blkSize
. (integer).opts.start
is the start from while to read the file. Defaults to 0. (integer)opts.end
is the end until which to read the file. Defaults to the end of the file. (integer)
Cache.DEFAULT_CACHE_SIZE
The default size of a cache created if opts.cache
is not passed in: 10485760
(integer, equals 10 MegaByte)
new CachedFile(cache, path[, opts])
Creates a new instance for reading one file. The blocks will still be stored in
the passed-in cache
object. While it is possible to instantiate a new
CachedFile
, you can not pass-in a cache directly, use the
.open
, .openSync
or
.createReadStream
to interact with the cache
cacheInternal
a subset of theCache
API that is not accessible from outside.cacheInternal.open(path, opts, cb)
opens a file pointer to a givenpath
on the underlyingfs
.cacheInternal.stat(path, cb)
receives thestat
file from the underlyingfs
cacheInternal.close(fp, cb)
closes a file pointer on the underlyingfs
.cacheInternal.read(fp, prefix, start, end, cb)
reads bytes from the underlyingfs
into a buffer.opts.blkSize
specifies the block size for this file pointer (integer). Defaults toCachedFile.DEFAULT_BLK_SIZE
.
cachedFile.close([cb])
Closes the instance. After closing, future requests
on the CachedFile
will result in an err.code === 'ERR_CLOSED
error.
cb(Error)
is an optional async callback handler method. The method will return aPromise
if the callback is not defined.
cachedFile.read([buffer, offset, length, position, cb])
Like cache.read
but without the need to pass a descriptor.
cachedFile.createReadStream([opts, cb])
Like cache.createReadStream
but without the need
to pass a descriptor.
cachedFile.size([cb])
The size of the file as noted in the file descriptor.
cachedFile.stat([cb])
Retreives the actual
Stats
of the file
through fs.stat
.
CachedFile.DEFAULT_BLK_SIZE
The default opts.blkSize
used for caching: 512 (integer, equals 512 Byte).
Acknowledgement
This project was made for and supported by dotloom.
License
MIT