Cacheman-Promise


Cacheman library with a promise interface.
Installation
$ npm install --save cacheman-promise
Usage
Cacheman-promise only support set
, get
, del
, clear
, pull
and wrap
promise interface.
Cacheman([name, [options]])
Please refer to Cacheman Options API
var Cacheman = require('cacheman-promise');
var options = {
ttl: 90,
engine: 'redis',
port: 9999,
host: '127.0.0.1'
};
var cache = new Cacheman('todo', options);
var cache = new Cacheman(options);
cache.set(key, value, [ttl, [fn]])
Usage:
var key = 'foo';
var data = 'bar';
cache.set(key, {name: data})
.then(function(val){
console.log(val);
});
cache.get(key, fn)
You can pass array
or string
as key.
Usage:
cache.set('foo', 'bar');
cache.get('foo')
.then(function(val){
console.log(val);
});
pass array
as multiple keys
Usage:
cache.set('foo', 1);
cache.set('bar', 2);
cache.get(['foo', 'bar'])
.then(function(result){
console.log(result);
});
cache.pull(key, default)
If you need to retrieve an item from the cache and then delete it, you may use the pull
method. Like the get
method, null
will be returned if the item does not exist in the cache.
cache.set('foo', 'bar');
cache.pull('foo')
.then(function(result){
console.log(result);
}).then(function() {
return cache.get('foo');
}).then(function(result) {
console.log(result);
});
You can pass default value as second paramaeter if the item doesn't exist in the cache.
cache.del('foo');
cache.pull('foo', 'bar')
.then(function(result){
console.log(result);
});
cache.del(key, [fn])
You can pass array
or string
as key.
Usage:
cache.del('foo')
.then(function(){
console.log('foo was deleted');
});
or
cache.del(['foo', 'bar'])
.then(function(){
console.log('foo and bar was deleted');
});
Clear some items with prefix name
:
cache.clear('foo*')
.then(function(){
console.log('clear cache with `foo` prefix name like `foo1`, `foo2` etc.');
});
cache.clear([fn])
Clear all cache as follwoing:
cache.clear()
.then(function(){
console.log('cache is now clear');
});
cache.wrap(key, default, [ttl, [fn]])
Wraps a function in cache. I.e., the first time the function is run, its results are stored in cache so subsequent calls retrieve from cache instead of calling the function.
var key = 'foo';
var data = 'bar';
cache.wrap(key, data)
.then(function(val) {
return cache.get(key);
}).then(function(val) {
console.log(val);
});
Run tests
$ npm test