Memcached plugin of eggjs, Promise wrapped by bluebird. Will start a memcached connection before server start and using in app.
$ npm i egg-memcache --save
// {app_root}/config/plugin.js
exports.memcache = {
enable: true,
package: 'egg-memcache',
};
// {app_root}/config/config.default.js
exports.memcache = {
url: 'localhost:11211',
option: {}
};
see memcached API for more detail.
// {app_root}/service/store.js
module.exports = (app) => {
const store = {
set(key, value, lifetime) {
return app.memcached.setAsync(key, value, lifetime);
},
get(key) {
return app.memcached.getAsync(key);
}
}
};
// {app_root}/controller/index.js
class IndexController extends Controller {
* setKey() {
yield this.ctx.service.store.set('key', 'value', 0);
}
* getKey() {
const key = yield this.ctx.service.store.get('key');
}
}
Also can use native memcache api without
async
, such asapp.memcached.set