# Code coverage report for thinkjs/lib/Lib/Util/Cache.js

## Statements: 100% (58 / 58) Branches: 100% (51 / 51) Functions: 100% (8 / 8) Lines: 100% (58 / 58) Ignored: none

All files » thinkjs/lib/Lib/Util/ » Cache.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
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146

    2

    2

    2

    115
    40

    75
    73
    73
    72

    1

    2

    2

    115
    115
    115
    1

    115
    96

    115

    115

    115
    115

    11
    11
    2

    9
    9
    3
    3

    6
    3

    6

    6
    3
    3
    1

    6

    13
    1

    13

    13
    6
    7
    1

    13
    5

    8

    13

    4
    4
    2
    1

    2

    4

    4
    5
    5
    1

     /**
     * 缓存基类
     * @return {[type]} [description]
     */
    /**
     * 缓存数据
     * @type {Object}
     */
    var cacheData = {};
    /**
     * 定时器
     * @type {Number}
     */
    var gcTimer = {};
    /**
     * 清除已经过期的Cache
     * @return {[type]} [description]
     */
    var gc = function(instance){
      'use strict';
      if (APP_DEBUG || APP_MODE === 'cli' || gcTimer[instance.gcType]) {
        return;
      }
      gcTimer[instance.gcType] = setInterval(function(){
        var hour = (new Date()).getHours();
        if (C('cache_gc_hour').indexOf(hour) === -1) {
          return;
        }
        return instance.gc && instance.gc(Date.now());
      }, 3600 * 1000);
    };

    module.exports = Class(function(){
      'use strict';
      return {
        /**
         * gc的类型，用于定时器类型判断
         * @type {String}
         */
        gcType: 'Cache',
        /**
         * 初始化
         * @param  {[type]} options [description]
         * @return {[type]}         [description]
         */
        init: function(options){
          options = options || {};
          this.cacheData = options.cacheData || cacheData;
          if (options.gcType) {
            this.gcType = options.gcType;
          }
          if (!options.timeout) {
            options.timeout = C('cache_timeout')
          }
          this.options = options;
          //操作的key
          this.key = '';
          //是否更新expire值
          this.updateExpire = this.options.updateExpire || false;
          gc(this);
        },
        /**
         * 获取缓存值，返回一个promise
         * @param  {[type]} name [description]
         * @return {[type]}      [description]
         */
        get: function(name){
          var key = this.key || name;
          if (!(key in this.cacheData)) {
            return getPromise();
          }
          var value = this.cacheData[key];
          if (Date.now() > value.expire) {
            delete this.cacheData[key];
            return getPromise();
          }
          if (this.updateExpire) {
            this.cacheData[key].expire = Date.now() + value.timeout * 1000;
          }
          var data = value.data[name];
          //如果data是个对象或者数组，需要深度拷贝
          if (isObject(data)) {
            data = extend({}, data);
          }else if (isArray(data)) {
            data = extend([], data);
          }
          return getPromise(data);
        },
        /**
         * 设置缓存值
         * @param {[type]} name   [description]
         * @param {[type]} value  [description]
         */
        set: function(name, value, timeout){
          if (timeout === undefined) {
            timeout = this.options.timeout;
          }
          var key = this.key || name;
          //如果value是个对象或者数组，这里需要深度拷贝，防止程序里修改值导致缓存值被修改
          if (isObject(value)) {
            value = extend({}, value);
          }else if (isArray(value)) {
            value = extend([], value);
          }
          if (key in this.cacheData) {
            this.cacheData[key].data[name] = value;
          }else{
            this.cacheData[key] = {
              data: getObject(name, value),
              timeout: timeout,
              expire: Date.now() + timeout * 1000
            };
          }
          return getPromise();
        },
        /**
         * 移除缓存值
         * @param  {[type]} name [description]
         * @return {[type]}      [description]
         */
        rm: function(name){
          var key = this.key || name;
          if (this.key) {
            if (key in this.cacheData) {
              delete this.cacheData[key].data[name];
            }
          }else{
            delete this.cacheData[name];
          }
          return getPromise();
        },
        /**
         * gc
         * @param  {[type]} now [description]
         * @return {[type]}     [description]
         */
        gc: function(now){
          for(var key in this.cacheData){
            var item = this.cacheData[key];
            if (item && now > item.expire) {
              delete this.cacheData[key];
            }
          }
        }
      };
    });