skyfore / egg-memcache

Memcached Plugin for eggjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

egg-memcache

NPM version npm download

Memcached plugin of eggjs, Promise wrapped by bluebird. Will start a memcached connection before server start and using in app.

Install

$ npm i egg-memcache --save

Usage

// {app_root}/config/plugin.js
exports.memcache = {
  enable: true,
  package: 'egg-memcache',
};

Configuration

// {app_root}/config/config.default.js
exports.memcache = {
  url: 'localhost:11211',
  option: {}
};

see memcached API for more detail.

Example

//  {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 as app.memcached.set

License

MIT

About

Memcached Plugin for eggjs

License:MIT License


Languages

Language:JavaScript 100.0%