6.0.18 • Published 4 years ago
cachify-wrapper v6.0.18
cachify-wrapper
Wraps a function with a caching layer
Usage
callback
Wraps a function with a caching layer
Parameters
fnfunctioncallback-laststyle functionstorageStorage\<K, RecordPacked\> cache storage (optional, defaultInMemoryStorageCb)optionsOptions (optional, default{})hasherFunction creates key for KV-storage fromfnarguments (optional, defaultJSON.stringify)
Examples
const wrapper = require('cachify-wrapper').default;
class Storage {
constructor() {
this.data = new Map();
}
get = (key, cb) => cb(null, this.data.get(key))
set = (key, value, ttl, cb) => {
this.data.set(key, value);
if (ttl > 0) setTimeout(() => this.data.delete(key), ttl);
cb(null, true);
}
del = (key, cb) => cb(null, this.data.delete(key))
}
const storage = new Storage();
let count = 0;
const inc = (a, cb) => cb(null, count += a);
const cached = wrapper(inc, storage, {expire: 100});
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 0); // Invokes request
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 100); // Takes cached result
setTimeout(() => cached(1, (_error, payload) => console.info(payload)), 200); // Invokes second request
cached.set(2, 'manual value', 1000, () =>
cached.get(2, (_, result) => {
console.info(result);
cached.del(2, () =>
cached.get(2, (_, result) => console.info(result)))
}));Returns function
promise
Wraps a function with a caching layer
Parameters
fnfunctionstorageStorage\<K, RecordPacked\>?optionsOptions?hasherFunction?
Examples
const wrapperPromise = require('cachify-wrapper').promise;
let count = 0;
const inc = async(a) => count += a;
const cached = wrapperPromise(inc, storage, {expire: 1000});
const p1 = cached(1).then((payload) => console.info(payload)); // Invokes request
const p2 = p1.then(() => cached(1).then((payload) => console.info(payload))); // Takes cached resultReturns function (...any): Promise\
Options
Type: Object
Properties
storageObject?storage.timeoutnumber? max storage response time before considering it as failed, and invokingfn
sourceObject?source.timeoutnumber? maxfnresponse time before considering it as failed
expirenumber? time to consider cached data expired [in milliseconds]spreadnumber? expire time spread (prevents simultaneous deletions saved items from storage)locknumber? lock timeout (prevents simultaneous concurrent invoke offnat initial period)stalenumber? additional ttl for stale datattlnumber? forced ttl (TimeToLive) for data (useful if storage is using from multiply services with different expire)retriesnumber? number of storage requests passes beforefncallerrornumber? ttl for erroneous state cache (prevents frequent call offn)verbosenumber? verbosity flag
CacheAbsentError
Extends Error
no cache error
Functions
Storage
storage interface
get
Parameters
keyKcbCB\
set
Parameters
keyKvalueVttlnumbercbCB[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
del
Parameters
keyKcbCB[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
RecordPacked
Type: Object
Properties
InMemoryStorageCb
get
Parameters
keyKcbCB\
set
Parameters
keyKvalueVttlnumbercbCB[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
del
Parameters
keyKcbCB[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
InMemoryStorageSerializable
Extends InMemoryStorage
expire
Parameters
keyKttlnumber
export
Returns Iterable\<KRecordTuple\<K, V>>
import
Parameters
dumpIterable\<KRecordTuple\<K, V>>
InMemoryStorageRecord
Type: Object
Properties
InMemoryStorage
Parameters
sourceIterable\<KRecordTuple\<K, V>>?
get
Parameters
keyK
set
Parameters
keyKvalueV
del
Parameters
keyK
has
Parameters
keyK
expire
Parameters
keyKttlnumber
6.0.18
4 years ago
6.0.17
4 years ago
6.0.16
4 years ago
6.0.10
5 years ago
6.0.4
5 years ago
6.0.0
5 years ago
5.0.0
5 years ago
4.1.0
7 years ago
4.0.2
7 years ago
4.0.1
7 years ago
3.2.4
7 years ago
3.2.2
7 years ago
3.2.1
7 years ago
3.2.0
7 years ago
3.1.0
7 years ago
2.0.1
7 years ago
1.1.2
7 years ago
1.1.1
7 years ago
1.0.1
7 years ago