egg-mongoose-cache
Egg's cache plugin based on egg-mongoose and egg-redis for egg framwork
- egg-mongoose-cache plugin rewrite from cachegoose, increase support for populate.
Install
$ npm i egg-mongoose-cache --save
Dependencies
egg version
egg-mongoose-cache ver | egg 2.x | egg-mongoose 3.2.x |
---|---|---|
1.x | 😁 | 😁 |
0.x | ❌ | ❌ |
Dependencies plugins
- egg-mongoose ^3.2.0
- egg-redis ^2.4.0
Usage
// {app_root}/config/plugin.jsexportsmongooseCache = enable: true package: 'egg-mongoose-cache';
Configuration
see config/config.default.js for more detail.
- instanceName: when use egg-redis Multi Clients, set the redis instance name
- ttl: global default expiration time(second), default: 600
- keyPrefix: redis Prefix key, defalut: "mc:"
egg-redis Single Client:
exportsmongooseCache = ttl: 600 keyPrefix: 'mc:'
egg-redis Multi Clients:
exportsmongooseCache = instanceName: 'cache' ttl: 300 keyPrefix: 'mc:'
Example
In controller or service, you can use find/findOne/aggregate to cache data to redis.
Use FindOne/populate, cache 60 second:
let data = await ctxmodeluser;
Use Find, cache 600 second:
let data = await ctxmodeluser;
Use Aggregate, cache 60 second and user custom key:
let data = await ctxmodeluser;
Questions & Suggestions
Please open an issue egg-mongoose-cache issues.