KEEPSAKE
Keepsake of items infinitely or forget them after a number of times.
Example
See also the examples folder.
Installation
npm install keepsake
Usage
Include the module
var keepsake = ;
Possible storages
- MemoryStore for using memory storage
NB! For now you can only use the MemoryStore.
Setting up 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 = MemoryStore; // with custom optionsvar customCache = ttl: 1000 overwrite: true maxCacheSize: 1024; // with defaults valuesvar defaultCache = ;
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; // set TTL to 5 secondsdefaultCache; // key can be updatedefaultCache; // set max cache size to 2MB
Available methods
Add item(s) in store
Possible parameters :
- Array - an array of object with 2 attributes : key and value
- Function - callback function
Example :
// add one itemdefaultCache; // add more than one itemdefaultCache;
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.
Retrieve item(s) from store
Possible parameters :
- Array, String - can be a key or an array of keys
- Function - callback function
Example :
// retrieve value from one itemdefaultCache; // retrieve values from multiple itemsdefaultCache;
Remove item(s) from store
Possible parameters :
- Array, String - can be a key or an array of keys
- Function - callback function
Example :
// retrieve value from one itemdefaultCache; // retrieve values from multiple itemsdefaultCache;
Clear store
Possible parameter :
- Function - callback function
Example :
defaultCacheclear { if err return console; console;};
Retrieve the number of items in store
Example :
console; // display the number of items in memory store
Tests
Run the tests with npm in keepsake directory
npm test
License
keepsake is licensed under MIT License.