An in-memory cache with extra options to fix value types and set constants in cache, for more consistency.
- Returns a promise that resolves with the set value.
key
should be a string.value
can be anything except forundefined
,null
orNaN
.options
(object)makeConstant
(boolean): if set totrue
, does not allow the value to be updated.fixType
(boolean): if set totrue
, does not allow the type of the value to be changed.ttl
(number) (time in milliseconds): if specified, removes the value after the given interval.expiryCallback(value)
(function): if provided with validttl
, this function gets called right after a value is expired with the value.
- .setSync(...) is the synchronous version with the same parameters, returns the value.
- fixes the type for the
value
against the givenkey
, and can be only updated with avalue
with same type as inital type ofvalue
. - Returns a promise that resolves with the set value.
- .setFixTypeSync(...) is the synchronous version with the same parameters, returns the value.
- sets the value as a constant i.e. the value cannot be changed later on.
- Returns a promise that resolves with the set value.
- .setConstantSync(...) is the synchronous version with the same parameters, returns the value.
- Returns a promise that resolves with the value stored against the
key
. - .getSync(...) is the synchronous version with the same parameters, returns the value.
- Updates existing value against
key
withvalue
. - Returns a promise that resolves with the updated value.
- Updates or sets value against
key
withvalue
. - Returns a promise that resolves with the updated or set value.
const cache = require('cache-flex');
cache.setSync('someKey', 'someValue')
let value = cache.getSync('someKey');
cache.set('someKey', {a: 'some value'})
.then(value => {
// 'value' is {a: 'some value'}
let result = cache.get('someKey')
.then(result => {
// 'value' is {a: 'some value'}
})
.catch(err => {
// handle error with 'get'
});
})
.catch(err => {
// handle error with 'set'
});