npm install flex-cache
Or require the flex-cache.js file to get FlexCache
const FlexCache = require( "flex-cache" );
const myCache = new FlexCache;
let myKeys = myCache.keys();
console.log( myKeys );
// returns [ key1, key2, key3, key4 ]
let data = { name: John, age: 21};
myCache.set( "key", data, 10000 );
// returns true
let value = myCache.get( "key" );
if(!value) {
//handle miss
};
// returns { "myKey", keyValue };
let delete = myCache.del( "key" );
// returns 1
let value = myCache.take( "key" );
// returns key value but also deletes key
let changeTtl1 = myCache.ttl( "exsitent_Key", 100 );
// returns true
let changeTtl2 = myCache.ttl( "non-Exsitent_Key", 100 );
// returns false
Redefine the ttl of a key. Returns true if the key has been found and changed. Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
The key will be deleted when passing in a ttl < 0.
let value = myCache.take( "key" );
// returns key value but also deletes key
Receive the ttl of a key. You will get:
- undefined if the key does not exist
- 0 if this key has no ttl
- a timestamp in ms representing the time at which the key will expire
let value = myCache.take( "key" );
// returns key value but also deletes key
Returns boolean indicating if the key is cached.
myCache.getStats();
/* {
hits: 0 //global hit count
misses: 0 //global miss count
keys: 0 //global key count
ksize: 0 //global key size count in approximately bytes
vsize: 0 //value size count in approximately bytes
}
*/
Returns the statistics.
myCache.flushStats();
myCache.getStats();
/* {
hits: 0 //global hit count
misses: 0 //global miss count
keys: 0 //global key count
ksize: 0 //global key size count in approximately bytes
vsize: 0 //value size count in approximately bytes
}
*/
Flush the stats.
myCache.flushAll();
myCache.getStats();
/* {
hits: 0 //global hit count
misses: 0 //global miss count
keys: 0 //global key count
ksize: 0 //global key size count in approximately bytes
vsize: 0 //value size count in approximately bytes
}
*/
Flush all data.
myCache.close();
This will clear the interval timeout which is set on check period option.