cache-point
Simple, filesystem-backed memoisation cache. Use to cache the output of expensive operations speeding up future invocations with the same input.
Synopsis
const Cache = /* a mock function to simulate a slow remote request */ { return { }} { thiscache = dir: 'tmp/example' } async { let user try /* cache.read() will resolve on hit, reject on miss */ user = await thiscache catch err if errcode === 'ENOENT' /* cache miss, fetch remote user */ user = await thiscache return user } { console const users = const user = await users console console}
The first invocation will take 1 second while the remote user is fetched.
$ node example/simple.js
getUser: 1.025s
{ id: 10, name: 'Layla' }
Since the cache is now warm, future invocations will be fast.
$ node example/simple.js
getUser: 17.07ms
{ id: 10, name: 'Layla' }
API Reference
cache-point
- cache-point
- Cache ⏏
- new Cache([options])
- .dir :
string
- .read(keys) ⇒
Promise
- .readSync(keys) ⇒
string
- .write(keys, content) ⇒
Promise
- .writeSync(keys, content)
- .getChecksum(keys) ⇒
string
- .clear() ⇒
Promise
- .remove() ⇒
Promise
- Cache ⏏
Cache ⏏
new Cache([options])
Param | Type |
---|---|
[options] | object |
[options.dir] | string |
string
cache.dir : Current cache directory. Can be changed at any time.
Kind: instance property of Cache
Promise
cache.read(keys) ⇒ A cache hit resolves with the stored value, a miss rejects with an ENOENT
error code.
Kind: instance method of Cache
Throws:
- ENOENT
Param | Type | Description |
---|---|---|
keys | * |
One or more values to uniquely identify the data. Can be any value, or an array of values of any type. |
string
cache.readSync(keys) ⇒ A cache hit returns the stored value, a miss returns null
.
Kind: instance method of Cache
Param | Type | Description |
---|---|---|
keys | * |
One or more values to uniquely identify the data. Can be any value, or an array of values of any type. |
Promise
cache.write(keys, content) ⇒ Write some data to the cache. Returns a promise which resolves when the write is complete.
Kind: instance method of Cache
Param | Type | Description |
---|---|---|
keys | * |
One or more values to index the data, e.g. a request object or set of function args. |
content | * |
the data to store |
cache.writeSync(keys, content)
Write some data to the cache with a key.
Kind: instance method of Cache
Param | Type | Description |
---|---|---|
keys | * |
One or more values to index the data, e.g. a request object or set of function args. |
content | * |
the data to store |
string
cache.getChecksum(keys) ⇒ Used internally to convert a key value into a hex checksum. Override if for some reason you need a different hashing strategy.
Kind: instance method of Cache
Param | Type | Description |
---|---|---|
keys | * |
One or more values to index the data, e.g. a request object or set of function args. |
Promise
cache.clear() ⇒ Clears the cache. Returns a promise which resolves once the cache is clear.
Kind: instance method of Cache
Promise
cache.remove() ⇒ Clears and removes the cache directory. Returns a promise which resolves once the remove is complete.
Kind: instance method of Cache
© 2016-20 Lloyd Brookes <75pound@gmail.com>.
Tested by test-runner. Documented by jsdoc-to-markdown.