Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Redis store for node-cache-manager using node_redis.

License

NotificationsYou must be signed in to change notification settings

dabroek/node-cache-manager-redis-store

Repository files navigation

npm versionGitHub issuescodecov

Redis store for node cache manager

Redis cache store fornode-cache-manager.

How is this package different fromnode-cache-manager-redis?

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.

Installation

npm install cache-manager-redis-store --save

or

yarn add cache-manager-redis-store

Usage Examples

See examples below on how to implement the Redis cache store.

Single 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});});

Multi-store

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);});});

Contribution

Want to help improve this package? We takepull requests.

License

Thenode-cache-manager-redis-store is licensed under the MIT license.

About

Redis store for node-cache-manager using node_redis.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp