egg-cache
Based on cache-manager
All store engine based on cache-manager can be used.
Installation
npm i egg-cache -S
or
yarn add egg-cache
Configuration
// config/plugin.jsexportscache = enable: true package: 'egg-cache';
// config/config.default.jsexportscache = default: 'memory' stores: memory: driver: 'memory' max: 100 ttl: 0 ;
Usage
await appcache; await appcache; // 'bar' await appcache; // true await appcache;await appcache; // 'default' await appcache; // false // closureawait appcache; // 'bar' // Promiseawait appcache; // 'bar' // Get cached value. If it's not existed, get and save the value from closureawait appcache; // 'bar' // You can declare an `expire` optionawait appcache; // foo was cachedawait appcache; // 'bar' // clear cacheawait appcache;
Add store
- config: the
store
in the configuration uses thedriver
instead.
// config/config.default.js const redisStore = ; exportscache = default: 'memory' stores: memory: driver: 'memory' max: 100 ttl: 0 redis: // full config: https://github.com/dabroek/node-cache-manager-ioredis#single-store driver: redisStore host: 'localhost' port: 6379 password: '' db: 0 ttl: 600 _ !== null ;
- usage
const store = appcache; await store;await store; // 'bar' await store;await store; // false
Api
cache.set(name, value, [expire=null, options=null]);
Set Cache
name
cache namevalue
cache valueexpire
(Optional) expire(default from config file,the unit is second,0
means nerver expire)options
(Optional) Refer to cache-manager)
cache.get(name, [defaultValue=null, expire=null, options=null]);
cache.del(name);
cache.has(name);
cache.store(name, [options=null]);
cache.reset();
Default configuration
Refer to config/config.default.js
Unit Test
npm test
Issue
Refer to Issues.