
@vltpkg/cache
The filesystem cache for @vltpkg/registry-client, but also, a
general-purpose filesystem-backed LRUCache
Usage ~ Note
Overview
This is very minimal on features, because it has a very narrow use
case, but if you want to have a persistently fs-backed LRU memory
cache of Buffers using strings as keys, then this is the thing to use.
Usage
import { Cache } from '@vltpkg/cache'
const numberOfItemsToKeepInMemory = 10_000
const cache = new Cache({
path: '/path/to/fs/cache/folder',
max: 10_000,
})
const someCachedValue = await cache.fetch(someKey)
const integrity =
'sha512-6/mh1E2u2YgEsCHdY0Yx5oW+61gZU+1vXaoiHHrpKeuRNNgFvS+/jrwHiQhB5apAf5oB7UB7E19ol2R2LKH8hQ=='
const valueByInt = await cache.fetch('blah', {
context: { integrity },
})
const valueFromMemoryCache = cache.get(someKey)
const valueUsingSyncFileSystemOps = cache.fetchSync(someKey)
cache.set('some-key', Buffer.from('some-value'))
cache.set(someKey, someValue, { integrity })
await cache.promise()
const otherValue = await cache.fetch(otherKey, {
context: { integrity },
})
Note
- The key type must be a string. It gets sha512 hashed to determine
the file on disk.
- The value must be a Buffer, so that it can be written to a file and
read from it without having to convert anything.