This library is a wrapper around https://github.com/groupon/node-cached and https://github.com/cayasso/cacheman-redis, it makes easy to switch from one to the other with configuration. It also make all APIs the same, same methods, same parameters. Not all APIs are wrapped but the common ones. It also add the Promise style on all the method
- set which set a value
- key: string as the unique key to set
- value: any type that you want to store
- ttl: string that represent the time to live like 3d -> 3 days, 1h -> 1 hour ...
- get
- key: string as the unique key to retrieve
- unset
- key: string as the unique key to remove
npm install cacheswitcher
Simple memory cache
var cacheswitcher = require('cacheswitcher')('memory');
cacheswitcher.set('mykey',12345).then(function(){
cacheswitcher.get('mykey').then(function(data){
console.log(data);
cacheswitcher.unset('mykey').then(function(){
console.log('Clean');
});
})
})
Redis cache
var cacheswitcher = require('cacheswitcher')('redis',{server:'127.0.0.1',port:6379});
cacheswitcher.set('mykey',12345).then(function(){
cacheswitcher.get('mykey').then(function(data){
console.log(data);
cacheswitcher.unset('mykey').then(function(){
console.log('Clean');
});
})
})
Redis cache with ttl
var cacheswitcher = require('cacheswitcher')('redis',{server:'127.0.0.1',port:6379});
cacheswitcher.set('mykey',12345,'3d').then(function(){
//3 days later
cacheswitcher.get('mykey').then(function(data){
console.log(data); // -> undefined
cacheswitcher.unset('mykey').then(function(){
console.log('Clean');
});
})
})
- type: string like memory | redis
- opt: Object with following properties
- server: only for redis
- port: only for redis
- name: only for memory namespace
npm test