Code coverage report for thinkjs/lib/Lib/Driver/Cache/MemcacheCache.js

Statements: 100% (14 / 14)      Branches: 100% (6 / 6)      Functions: 100% (6 / 6)      Lines: 100% (14 / 14)      Ignored: none     

All files » thinkjs/lib/Lib/Driver/Cache/ » MemcacheCache.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 581 1           1   1                       7 7 7 1   7               2 2                   1 1               1      
var memcache = thinkRequire('MemcacheSocket');
module.exports = Cache(function(){
  'use strict';
  /**
   * 同一份配置只有一个连接
   * @type {Object}
   */
  var instances = {};
 
  return {
    /**
     * key前缀
     * @type {[type]}
     */
    namePrefix: C('cache_key_prefix'),
    /**
     * 初始化
     * @param  {[type]} options [description]
     * @return {[type]}         [description]
     */
    init: function(options){
      this.super_('init', options);
      var key = JSON.stringify(options);
      if (!(key in instances)) {
        instances[key] = memcache(C('memcache_port'), C('memcache_host'));
      }
      this.handle = instances[key];
    },
    /**
     * 获取
     * @param  {[type]} name [description]
     * @return {[type]}      [description]
     */
    get: function(name){
      return this.handle.get(this.namePrefix + name).then(function(value){
        return value ? JSON.parse(value) : value;
      })
    },
    /**
     * 设置
     * @param {[type]} name    [description]
     * @param {[type]} value   [description]
     * @param {[type]} timeout [description]
     */
    set: function(name, value, timeout){
      timeout = timeout || this.options.timeout;
      return this.handle.set(this.namePrefix + name, JSON.stringify(value), timeout);
    },
    /**
     * 删除
     * @param  {[type]} name [description]
     * @return {[type]}      [description]
     */
    rm: function(name){
      return this.handle.delete(this.namePrefix + name);
    }
  };
});