egg-cache-9
The plugin implements an easy-to-use caching function based on node-cache-9, which supports caching data into memory and caching data to redis.
中文说明
Install
$ npm i egg-cache-9 --save
Usage
// {app_root}/config/plugin.jsexportscache9 = enable: true package: 'egg-cache-9';
// {app_root}/config/config.default.jsexportscache9 = client: class: 'memory' ttl: 300 ; // {app_root}/app/****.jsconst cache = appcache9; // get data from a data source or cachelet data = await cache;cache; // update expiration timeawait cacheclear'key'; // clear cache // get a lot of datas from a data source or cachelet list json = await cache;cache; // update expiration timeawait cache; //clear cacheawait cache; //clear cache
// {app_root}/config/config.default.jsexportscache9 = default: ttl: 300 clients: cacheA: class: 'memory' cacheB: class: 'redis' rds: host: '127.0.0.1' ; // {app_root}/app/****.jsconst cacheA = appcache9;const cacheB = appcache9; // get data from a data source or cachelet data = await cacheA;cacheA; // update expiration timeawait cacheAclear'key'; //clear cache // get a lot of datas from a data source or cachelet list json = await cacheB;cacheB; // update expiration timeawait cacheB; //clear cacheawait cacheB; //clear cache
see node-cache-9 for more detail.
Use with egg-redis
When using the egg-redis plugin, you can specify the redis client directly in the configuration.
exportscache9 = default: ttl: 300 clients: cacheA: // The memory cache needs to set both pubRedis and subRedis as string class: 'memory' pubRedis: 'cachePub' // app.redis.get('cachePub') subRedis: 'cacheSub' // app.redis.get('cacheSub') cacheB: // redis cache can set redis as string class: 'redis' redis: 'cache' // app.redis.get('cache') cacheC: // If you have not configured getRedis, redis and rds, use app.redis class: 'redis' ;
Configuration
see config/config.default.js for more detail.
Unit tests
Run redis-server in localhost first
npm run test