Install

 npm install flex-cache 

Or require the flex-cache.js file to get FlexCache

Initialize

const FlexCache = require( "flex-cache" );

const myCache = new FlexCache;

Methods

Log key(s) in the cache (keys)

let myKeys = myCache.keys();

console.log( myKeys );
// returns [ key1, key2, key3, key4 ]

Returns an array of all existing keys.

Store key(s) in the cache (set)

let data = { name: John, age: 21};

myCache.set( "key", data, 10000 );
// returns true

Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.

Retrieve key(s) from cache (get):

let value = myCache.get( "key" );

if(!value) {

//handle miss

};
// returns { "myKey", keyValue };

Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.

Remove key value pair(s) (del):

let delete = myCache.del( "key" );
// returns 1

Delete a key. Returns the number of deleted entries. A delete will never fail.Retrieve a key and remove from cache (take):

let value = myCache.take( "key" );
// returns key value but also deletes key

Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value.

Change time to live of a key (ttl):

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.


Log time to live of a key (getTtl):

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



Check if key exist in cache (has):

let value = myCache.take( "key" );

// returns key value but also deletes key

Returns boolean indicating if the key is cached.



Statistics (getStats):

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.



Clear cache stats (flushStats):

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.



Reset cache data/stats(flushAll):

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.



Close the cache(close):

myCache.close();


This will clear the interval timeout which is set on check period option.