Code coverage report for thinkjs/lib/Lib/Driver/Session/DbSession.js

Statements: 100% (39 / 39)      Branches: 92.86% (13 / 14)      Functions: 100% (13 / 13)      Lines: 100% (39 / 39)      Ignored: none     

All files » thinkjs/lib/Lib/Driver/Session/ » DbSession.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                                    2           2 2                       17 17 17 17 17 1   17             15 14 14 14 2         12 3   9     15               2 2 2                 2 2 2 2                 3 3 3 3 2   1                 2 2     2   2 1   2                 1          
/**
 * DbSession
 * 需要在数据库中建立对应的数据表
 *
 *  DROP TABLE IF EXISTS `think_session`;
  CREATE TABLE `think_session` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `key` varchar(255) NOT NULL DEFAULT '',
    `data` text,
    `expire` bigint(11) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `cookie` (`key`),
    KEY `expire` (`expire`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 *
 * 
 * @return {[type]} [description]
 */
module.exports = Cache(function(){
  'use strict';
  /**
   * Session的Model
   * @type {[type]}
   */
  var model = null;
  return {
    /**
     * gc类型
     * @type {String}
     */
    gcType: 'DbSession',
    /**
     * [init description]
     * @param  {[type]} options [description]
     * @return {[type]}         [description]
     */
    init: function(options){
      this.super_('init', options);
      this.key = this.options.cookie;
      this.data = {};
      this.isChanged = false;
      if (!model) {
        model = D('Session');
      }
      this.model = model;
    },
    /**
     * 初始化数据
     * @return {[type]} [description]
     */
    initData: function(){
      if (!this.promise) {
        var self = this;
        this.promise = model.where({key: this.key}).find().then(function(data){
          if (isEmpty(data)) {
            return model.add({
              key: self.key,
              expire: Date.now() + self.options.timeout * 1000
            });
          }
          if (Date.now() > data.expire) {
            return;
          }
          self.data = JSON.parse(data.data || '{}');
        });
      }
      return this.promise;
    },
    /**
     * 获取
     * @param  {[type]} name [description]
     * @return {[type]}      [description]
     */
    get: function(name){
      var self = this;
      return this.initData().then(function(){
        return self.data[name];
      });
    },
    /**
     * 设置
     * @param {[type]} name  [description]
     * @param {[type]} value [description]
     */
    set: function(name, value){
      var self = this;
      return this.initData().then(function(){
        self.isChanged = true;
        self.data[name] = value;
      });
    },
    /**
     * 删除
     * @param  {[type]} name [description]
     * @return {[type]}      [description]
     */
    rm: function(name){
      var self = this;
      return this.initData().then(function(){
        self.isChanged = true;
        if (name) {
          delete self.data[name];
        }else{
          self.data = {};
        }
      })
    },
    /**
     * 将数据保存到数据库中
     * @return {[type]} [description]
     */
    flush: function(){
      var self = this;
      var data = {
        expire: Date.now() + self.options.timeout * 1000
      };
      return this.initData().then(function(){
        //数据有修改时才更新data字段
        if (self.isChanged) {
          data.data = JSON.stringify(self.data);
        }
        return model.where({key: self.key}).update(data);
      });
    },
    /**
     * [gc description]
     * @param  {[type]} now [description]
     * @return {[type]}     [description]
     */
    gc: function(now){
      return model.where({
        expire: ['<', now]
      }).delete();
    }
  };
});