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

Statements: 100% (29 / 29)      Branches: 100% (12 / 12)      Functions: 100% (11 / 11)      Lines: 100% (29 / 29)      Ignored: none     

All files » thinkjs/lib/Lib/Driver/Session/ » FileSession.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          2 2   2             21 21 21 21 21             60 10 10 10 10     60               16 16 16                   10 10 10 10 1                 5 3   2   5             30 30 30        
/**
 * 文件Session
 * @return {[type]} [description]
 */
 
var os = require('os');
module.exports = Class(function(){
  'use strict';
  return {
    gcType: 'FileSession',
    /**
     * 差异化的init
     * @return {[type]} [description]
     */
    init: function(options){
      options = options || {};
      options.cache_path = C('session_path') || (os.tmpdir() + '/thinkjs');
      this.super_('init', options);
      this.key = options.cookie;
      this.data = {};
    },
    /**
     * 初始化数据
     * @return {[type]} [description]
     */
    initData: function(){
      if (!this.promise) {
        var self = this;
        this.promise = this.getData().then(function(data){
          self.data = data || {};
          return self.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]
     * @param {[type]} timeout [description]
     */
    set: function(name, value, timeout){
      var self = this;
      return this.initData().then(function(){
        self.data[name] = value;
        if (timeout) {
          self.options.timeout = timeout;
        }
      });
    },
    /**
     * 删除数据
     * @return {[type]} [description]
     */
    rm: function(name){
      if (name) {
        delete this.data[name];
      }else{
        this.data = {};
      }
      return getPromise();
    },
    /**
     * 将数据写入到文件中
     * @return {[type]} [description]
     */
    flush: function(){
      var self = this;
      return this.initData().then(function(){
        return self.setData(self.data);
      })
    }
  };
}, thinkRequire('FileCache'));