- Notifications
You must be signed in to change notification settings - Fork59
Redis store for node-cache-manager using node_redis.
License
dabroek/node-cache-manager-redis-store
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Redis cache store fornode-cache-manager.
This is acompletely different version than the earliernode-cache-manager-redis. This package does not useredis-pool which is unnecessary and not actively maintained.
This package aims to providethe most simple wrapper possible by just passing the configuration to the underlyingnode_redis package.
npm install cache-manager-redis-store --save
or
yarn add cache-manager-redis-store
See examples below on how to implement the Redis cache store.
varcacheManager=require('cache-manager');varredisStore=require('cache-manager-redis-store').redisStore;varconfig={socket:{host:'localhost',// default valueport:6379,// default value},password:'XXXXX',db:0,ttl:600};varredisCache=cacheManager.caching({store:awaitredisStore(config),});// listen for redis connection error eventvarredisClient=redisCache.store.getClient();redisClient.on('error',(error)=>{// handle error hereconsole.log(error);});varttl=5;awaitredisCache.set('foo','bar',{ttl:ttl});// You can use either a Promise...varresult=awaitredisCache.get('foo');console.log(result);// ...or a callbackredisCache.get('foo',(err,result)=>{if(err){// handle error here}console.log(result);});// >> 'bar'console.log(awaitredisCache.del('foo'));// >> 1functiongetUser(id,cb){setTimeout(()=>{console.log("Returning user from slow database.");cb(null,{id:id,name:'Bob'});},100);}varuserId=123;varkey=`user_${userId}`;// Note: ttl is optional in wrap()redisCache.wrap(key,(cb)=>{getUser(userId,cb);},{ttl:ttl},(err,user)=>{console.log(user);// Second time fetches user from redisCacheredisCache.wrap(key,()=>getUser(userId)).then(console.log).catch(err=>{// handle error});});
varcacheManager=require('cache-manager');varredisStore=require('cache-manager-redis-store').redisStore;varredisCache=cacheManager.caching({store:awaitredisStore({ ...config,db:0,ttl:600})});varmemoryCache=cacheManager.caching({store:'memory',max:100,ttl:60});varmultiCache=cacheManager.multiCaching([memoryCache,redisCache]);varuserId2=456;varkey2=`user_${userId2}`;// Set value in all cachesawaitmultiCache.set('foo2','bar2',{ttl:ttl});// Fetches from highest priority cache that has the keyvarresult=awaitmultiCache.get('foo2');console.log(result);// >> 'bar2'// Delete from all cachesawaitmultiCache.del('foo2');// Note: ttl is optional in wrapmultiCache.wrap(key2,(cb)=>{getUser(userId2,cb);},(err,user)=>{console.log(user);// Second time fetches user from memoryCache, since it's highest priority.// If the data expires in the memory cache, the next fetch would pull it from// the 'someOtherCache', and set the data in memory again.multiCache.wrap(key2,(cb)=>{getUser(userId2,cb);},(err,user)=>{console.log(user);});});
Want to help improve this package? We takepull requests.
Thenode-cache-manager-redis-store is licensed under the MIT license.
About
Redis store for node-cache-manager using node_redis.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.