thinkjs/lib/Lib/Driver/Session/FileSession.class.js
2014-01-07 13:35:00 +08:00

69 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 文件Session
* @return {[type]} [description]
*/
var os = require("os");
var fs = require("fs");
module.exports = Session(function(){
return {
file: "",
data: null,
init: function(http){
this.super_("init", http);
this.data = null;
//生成存放cookie的临时目录
var tmpDir = os.tmpdir() + "/thinkjs/";
if (!isDir(tmpDir)) {
mkdir(tmpDir);
};
//cookie文件
this.file = tmpDir + this.cookie + ".json";
},
/**
* 异步获取数据返回一个promise
* @param {[type]} name [description]
* @return {[type]} [description]
*/
get: function(name){
var deferrd = getDefer();
if (this.data === null) {
var data = getFileContent(this.file);
data = JSON.parse(data || "{}") || {};
this.data = data;
};
var self = this;
process.nextTick(function(){
deferrd.resolve(self.data[name]);
})
return deferrd.promise;
},
/**
* 设置session
* @param {[type]} name [description]
* @param {[type]} value [description]
*/
set: function(name, value){
if (this.data === null) {
this.data = {};
};
this.data[name] = value;
this.flush();
},
/**
* 删除session
* @return {[type]} [description]
*/
rm: function(){
fs.unlink(this.file, function(){});
},
/**
* 刷新数据,将数据写入到文件
* @return {[type]} [description]
*/
flush: function(){
var data = JSON.stringify(this.data || {});
setFileContent(this.file, data);
}
}
})