keepsake

Keepsake of items infinitely or forget them after a number of times

KEEPSAKE

Keepsake of items infinitely or forget them after a number of times.

See also the examples folder.

npm install keepsake

Include the module

var keepsake = require('keepsake');
  • MemoryStore for using memory storage

NB! For now you can only use the MemoryStore.

Possible store options are the following :

  • ttl - define a global TTL in milliseconds for each items added, after that time the key will be removed (default to 0 means infinity)
  • overwrite - set if you want to update value for a key that already exists (default to false)
  • maxCacheSize - set the store size in bytes (default to 10485760 Bytes = 10 MB)

Example :

var MemoryStore = require('keepsake').MemoryStore;
 
// with custom options 
var customCache = new MemoryStore({
    ttl: 1000,
    overwrite: true,
    maxCacheSize: 1024
});
 
// with defaults values 
var defaultCache = new MemoryStore();

After initialization, you can update TTL, overwrite and maxCacheSize attributes with these methods :

  • setTTL - define new global time-to-live (in ms)
  • setOverwrite - set if store can update or not the key value
  • setMaxCacheSize - define new cache size (in bytes)

Example :

defaultCache.setTTL(5000); // set TTL to 5 seconds 
defaultCache.setOverwrite(true); // key can be update 
defaultCache.setMaxCacheSize(2048); // set max cache size to 2MB 

Possible parameters :

  • Array - an array of object with 2 attributes : key and value
  • Function - callback function

Example :

// add one item 
defaultCache.addItems(
    [
        { "key": "one", "value": "oneValue" }
    ],
    function (err) {
        if (err) return console.log(err);
        console.log('Item(s) added');
    }
);
 
// add more than one item 
defaultCache.addItems(
    [
        { "key": "one", "value": "oneValue" },
        { "key": "two", "value": "twoValue" },
        { "key": "three", "value": "threeValue" },
        { "key": "four", "value": "fourValue" }
    ],
    function (err) {
        if (err) return console.log(err);
        console.log('Item(s) added');
    }
);

NB! If the size of an element is greater than the available space, the least recently used items will be removed until there is enough space for the new element to add.

Possible parameters :

  • Array, String - can be a key or an array of keys
  • Function - callback function

Example :

// retrieve value from one item 
defaultCache.retrieveItems(
    "one",
    function (errdata) {
        if (err) return console.log(err);
        if (!data) return console.log('Item(s) not found');
        console.log(data);
    }
);
 
// retrieve values from multiple items 
defaultCache.retrieveItems(
    [ "one", "two", "three", "four" ],
    function (errdata) {
        if (err) return console.log(err);
        if (!data) return console.log('Item(s) not found');
        console.log(data);
    }
);

Possible parameters :

  • Array, String - can be a key or an array of keys
  • Function - callback function

Example :

// retrieve value from one item 
defaultCache.removeItems(
    "one",
    function (err) {
        if (err) return console.log(err);
        console.log('Item(s) removed');
    }
);
 
// retrieve values from multiple items 
defaultCache.removeItems(
    [ "one", "two", "three", "four" ],
    function (errdata) {
        if (err) return console.log(err);
        console.log('Item(s) removed');
    }
);

Possible parameter :

  • Function - callback function

Example :

defaultCache.clear(function (err) {
    if (err) return console.log(err);
    console.log('Storage cleared');
});

Example :

console.log(defaultCache.getItemsCount()); // display the number of items in memory store 

Run the tests with npm in keepsake directory

npm test

keepsake is licensed under MIT License.