simpcache
v1.0.4
Published
![simpcache](https://socialify.git.ci/bayungrh/simpcache/image?description=1&descriptionEditable=A%20simple%20in-memory%20cache%20library%20for%20NodeJS&font=KoHo&forks=1&language=1&name=1&owner=1&pattern=Charlie%20Brown&pulls=1&stargazers=1&theme=Dark)
Downloads
13
Maintainers
Readme
⚡️ simpcache
A simple in-memory cache library for NodeJS
Installation
npm install simpcache --save
Example
Initialize
without options
const cache = require('simpcache')();
with options
const cache = require('simpcache')({
persistence: true
});
Options
persistence
: (default:false
) If enabled, all cache will be stored on disk.db
: (default:cache.json
) ifpersistence
is enabled cache will be stored oncache.json
file.
Example Code
const cache = require('simpcache')({
persistence: false // set `false` to use in-memory cache
});
// without expire time
cache.set('foo', 'bar');
console.log('foo', cache.get('foo'));
// @returns 'bar'
// with expire time 10sec, time in ms
cache.set('foo2', 'bar2', 10000);
console.log('foo2', cache.get('foo2'));
// @returns 'bar2'
// with timeout callback
cache.set('foo3', 'bar3', 10000, function (key, value) {
console.log('foo3 deleted from cache');
});
// @returns 'foo3 deleted from cache' after cache expired.
// check cache exists
cache.has('foo');
// @returns true
// delete cache key
cache.del('foo');
// clear all cache
cache.flushAll();
// get keys from the cache
console.log('keys', cache.keys());
// @returns ['foo', 'foo2']
Methods
- set(key, value, expire, timeoutCallback)
void
, set a new entry and value, and you can set the expiration time. - get(key)
string
ornull
, get the entry's value from a given key. - has(key)
boolean
, check key exists from cache. - del(key)
void
, remove a given key from the cache. - keys()
array
, returns the keys from the cache. - flushAll()
void
, remove all data from cache. - ttl(key)
number
, returns the remaining time to live of a key that has a timeout.