Comparing version 0.4.10 to 0.5.0
@@ -323,4 +323,5 @@ var fs = require("fs"); | ||
}; | ||
var encoding = C("encoding") || "utf8"; | ||
return fs.readFileSync(file, encoding); | ||
return fs.readFileSync(file, { | ||
encoding: C("encoding") || "utf8" | ||
}); | ||
} | ||
@@ -327,0 +328,0 @@ /** |
@@ -91,11 +91,6 @@ var fs = require("fs"); | ||
global.B = function(name, http, data){ | ||
var cls = name + "Behavior"; | ||
if (APP_DEBUG) { | ||
G('behavior_start'); | ||
if (typeof name === 'function') { | ||
return name(http, data); | ||
}; | ||
var result = thinkRequire(cls)(http).run(data); | ||
if (APP_DEBUG) { | ||
G('behavior_end'); | ||
}; | ||
return result; | ||
return thinkRequire(name + "Behavior")(http).run(data); | ||
} | ||
@@ -125,5 +120,2 @@ | ||
if (tags.length) { | ||
if (APP_DEBUG) { | ||
G(name + '_start'); | ||
}; | ||
tags = tags.slice(); | ||
@@ -259,14 +251,5 @@ function runBehavior(){ | ||
global.S = function(name, value, options){ | ||
if (options === undefined) { | ||
options = {}; | ||
}else if (options === true) { | ||
options = {type: true}; | ||
}; | ||
var type = options.type || C('data_cache_type'); | ||
if (type && type !== true) { | ||
type = ucfirst(type.toLowerCase()) + "Cache"; | ||
}else{ | ||
type = "Cache"; | ||
} | ||
var instance = thinkRequire(type)(options); | ||
options = options || {}; | ||
var type = options.type === undefined ? C('cache_type') : options.type; | ||
var instance = thinkRequire(ucfirst(type.toLowerCase()) + "Cache")(options); | ||
//获取缓存 | ||
@@ -280,4 +263,4 @@ if (value === undefined) { | ||
}; | ||
var expire = isNumber(options) ? options : undefined; | ||
return instance.set(name, value, expire); | ||
var timeout = isNumber(options) ? options : options.timeout; | ||
return instance.set(name, value, timeout); | ||
} | ||
@@ -310,3 +293,2 @@ /** | ||
} | ||
/** | ||
@@ -313,0 +295,0 @@ * 计数 |
@@ -1,2 +0,2 @@ | ||
/** | ||
/** | ||
* 框架默认配置 | ||
@@ -9,3 +9,4 @@ * 可以在App/Conf/config.js里修改下面的配置值 | ||
encoding: 'utf8', //编码 | ||
pathname_prefix: "", //不解析的pathname前缀 | ||
url_pathname_prefix: "", //不解析的pathname前缀 | ||
url_pathname_suffix: ".html", //url后缀,这样可以做到伪静态,对搜索引擎更友好 | ||
app_tag_on: true, //是否支持标签功能 | ||
@@ -16,3 +17,2 @@ url_resource_on: true, //是否监听静态资源类请求 | ||
url_route_on: true, //开启自定义路由功能 | ||
url_html_suffix: ".html", //url后缀,这样可以做到伪静态,对搜索引擎更友好 | ||
app_group_list: ["Home", "Admin"], //分组列表 | ||
@@ -35,31 +35,31 @@ default_group: 'Home', //默认分组 | ||
autoload_path: {}, //autoload查找的path,用于thinkRequire加载自定义库的时候查找 | ||
use_websocket: false, //是否使用websocket | ||
deny_ip: [], //阻止的ip | ||
error_tpl_path: THINK_PATH + "/View/error.html", //错误页模版 | ||
error_no_key: "errno", //错误number的key | ||
error_msg_key: "errmsg", //错误消息的key | ||
use_websocket: false, //是否使用websocket | ||
deny_ip: [], //阻止的ip | ||
load_ext_config: [], //加载额外的配置文件 CONF_PATH | ||
load_ext_file: [], //加载额外的文件 COMMON_PATH | ||
cookie_domain: "", //cookie有效域名 | ||
cookie_path: "", //cookie路径 | ||
cookie_expires: 0, //cookie默认保持时间数,默认随浏览器关闭失效 | ||
cookie_prefix: "", //cookie前缀,避免冲突 | ||
cookie_path: "/", //cookie路径 | ||
cookie_timeout: 0, //cookie失效时间,0为浏览器关闭,单位:秒 | ||
session_auto_start: false, //自动启动session | ||
session_id: "thinkjs", //session id | ||
session_type: "File", //session存储类型, 空为内存,还可以为File, Db | ||
session_options: {}, //session的一些选项 | ||
session_cookie_sign: "", //session对应的cookie使用签名,如果使用这里填密钥 | ||
session_name: "thinkjs", //session对应的cookie名称 | ||
session_type: "File", //session存储类型, 空为内存,还可以为File | ||
session_path: "", //File类型下文件存储位置,默认为系统的tmp目录 | ||
session_options: {}, //session对应的cookie选项 | ||
session_sign: "", //session对应的cookie使用签名 | ||
session_timeout: 24 * 3600, //session失效时间,单位:秒 | ||
load_ext_config: [], //加载额外的配置文件 CONF_PATH | ||
load_ext_file: [], //加载额外的文件 COMMON_PATH | ||
db_type: "mysql", // 数据库类型 | ||
db_host: "localhost",// 服务器地址 | ||
db_name: "",// 数据库名 | ||
db_user: "root",// 用户名 | ||
db_pwd: "",// 密码 | ||
db_port: "",// 端口 | ||
db_prefix: "think_",// 数据库表前缀 | ||
db_fieldtype_check: false,// 是否进行字段类型检查 | ||
db_host: "localhost", // 服务器地址 | ||
db_name: "", // 数据库名 | ||
db_user: "root", // 用户名 | ||
db_pwd: "", // 密码 | ||
db_port: "", // 端口 | ||
db_prefix: "think_", // 数据库表前缀 | ||
db_fieldtype_check: false, // 是否进行字段类型检查 | ||
db_fields_cache: true, // 启用字段缓存 | ||
@@ -77,20 +77,16 @@ db_charset: "utf8", // 数据库编码默认采用utf8 | ||
log_record: false, //是否记录日志 | ||
cache_type: "File", //数据缓存类型 | ||
cache_timeout: 6 * 3600, //数据缓存有效期,单位: 秒 | ||
cache_path: CACHE_PATH, //缓存路径设置 (File缓存方式有效) | ||
cache_file_suffix: ".json", //File缓存方式下文件后缀名 | ||
cache_gc_hour: [4], //缓存清除的时间点,数据为小时 | ||
data_cache_type: "File", //数据缓存类型,支持:File|true | ||
data_cache_time: 0, //数据缓存有效期,0表示永久缓存,单位: 秒 | ||
data_cache_prefix: "", //缓存前缀 | ||
data_cache_check: false, //数据缓存是否校验缓存 | ||
data_cache_path: CACHE_PATH, //缓存路径设置 (仅对File方式缓存有效) | ||
data_path_level: 1, // 子目录缓存级别,默认不使用子目录 (仅对File方式缓存有效) | ||
data_file_suffix: ".json", //缓存文件后缀 (仅对File方式缓存有效) | ||
memcache_host: "127.0.0.1", //memcache host | ||
memcache_port: 11211, //memecache端口 | ||
html_cache_on: false, //HTML静态缓存 | ||
html_cache_time: 3600, //缓存时间,单位为秒 | ||
html_cache_timeout: 3600, //缓存时间,单位为秒 | ||
html_cache_rules: {}, //缓存规则 | ||
html_cache_file_callback: undefined, //生成缓存文件的回调函数 | ||
html_cache_file_suffix: ".html", //缓存文件后缀名 | ||
memcache_host: "127.0.0.1", //memcache host | ||
memcache_port: 11211, //memecache端口 | ||
}; |
@@ -12,3 +12,3 @@ //获取模版文件 | ||
'html_cache_on': false, //是否开启缓存 | ||
'html_cache_time': 3600, //缓存时间 | ||
'html_cache_timeout': 3600, //缓存时间 | ||
'html_cache_rules': {}, //缓存规则 | ||
@@ -100,3 +100,3 @@ 'html_cache_file_callback': undefined, //生成缓存文件的回调函数 | ||
this.http.html_filename = filename; | ||
var cacheTime = html[1] || this.options.html_cache_time; | ||
var cacheTime = html[1] || this.options.html_cache_timeout; | ||
return cacheTime; | ||
@@ -103,0 +103,0 @@ }, |
@@ -241,6 +241,2 @@ /** | ||
http.setHeader("X-Powered-By", "thinkjs-" + THINK_VERSION); | ||
//初始化Session | ||
if (C('session_auto_start')) { | ||
thinkRequire('Session').start(http); | ||
}; | ||
var domainInstance = domain.create(); | ||
@@ -276,15 +272,15 @@ domainInstance.on("error", function(err){ | ||
console.log(message); | ||
if (!http.res) { | ||
return; | ||
}; | ||
if (APP_DEBUG) { | ||
http.res && http.res.end(message); | ||
http.res.end(message); | ||
}else{ | ||
if (http.res) { | ||
http.setHeader('Content-Type', 'text/html; charset=' + C('encoding')); | ||
var readStream = fs.createReadStream(C('error_tpl_path')); | ||
readStream.pipe(http.res); | ||
readStream.on("end", function(){ | ||
http.res.end(); | ||
}) | ||
}; | ||
http.setHeader('Content-Type', 'text/html; charset=' + C('encoding')); | ||
var readStream = fs.createReadStream(C('error_tpl_path')); | ||
readStream.pipe(http.res); | ||
readStream.on("end", function(){ | ||
http.res.end(); | ||
}) | ||
} | ||
} |
@@ -503,3 +503,2 @@ var url = require("url"); | ||
buildSelectSql: function(options){ | ||
options = options || {}; | ||
options = this.pageToLimit(options); | ||
@@ -506,0 +505,0 @@ var sql = this.parseSql(this.selectSql, options); |
@@ -17,16 +17,15 @@ var url = require("url"); | ||
parsePathName: function(){ | ||
var pathname = this.http.pathname; | ||
//URL后缀 | ||
var extname = path.extname(pathname); | ||
//判断URL后缀 | ||
if (extname === C('url_html_suffix')) { | ||
pathname = path.dirname(pathname) + "/" + path.basename(pathname, C('url_html_suffix')); | ||
}; | ||
pathname = pathname.split("/").filter(function(item){ | ||
var pathname = this.http.pathname.split("/").filter(function(item){ | ||
return item.trim(); | ||
}).join("/"); | ||
//去除pathname前缀 | ||
if (C('pathname_prefix') && pathname.indexOf(C('pathname_prefix')) === 0) { | ||
pathname = pathname.substr(C('pathname_prefix').length); | ||
var prefix = C('url_pathname_prefix'); | ||
if (prefix && pathname.indexOf(prefix) === 0) { | ||
pathname = pathname.substr(prefix.length); | ||
}; | ||
//判断URL后缀 | ||
var suffix = C('url_pathname_suffix'); | ||
if (suffix && pathname.substr(0 - suffix.length) === suffix) { | ||
pathname = pathname.substr(0, pathname.length - suffix.length); | ||
}; | ||
this.http.pathname = pathname; | ||
@@ -48,3 +47,3 @@ }, | ||
//将group list变为小写 | ||
var groupList = (C('app_group_list') || []).map(function(item){ | ||
var groupList = C('app_group_list').map(function(item){ | ||
return item.toLowerCase(); | ||
@@ -60,3 +59,3 @@ }); | ||
if (paths.length) { | ||
for(var i = 0,length = Math.ceil(paths.length)/2; i < length; i++){ | ||
for(var i = 0,length = Math.ceil(paths.length) / 2; i < length; i++){ | ||
self.http.get[paths[i * 2]] = paths[i * 2 + 1] || ""; | ||
@@ -63,0 +62,0 @@ } |
@@ -132,2 +132,3 @@ /** | ||
_response: function(){ | ||
var self = this; | ||
var res = { | ||
@@ -150,16 +151,5 @@ /** | ||
setCookie: function(name, value, options){ | ||
//将cookie发送出去 | ||
if (name === true) { | ||
var cookies = Object.values(this._cookie); | ||
if (cookies.length) { | ||
this.setHeader("Set-Cookie", cookies); | ||
this._cookie = {}; | ||
}; | ||
return true; | ||
}; | ||
options = options || {}; | ||
if (typeof options == 'number') { | ||
options = { | ||
expires: options | ||
} | ||
options = {expires: options}; | ||
}; | ||
@@ -187,2 +177,13 @@ var expires = options.expires; | ||
/** | ||
* 将队列中的cookie发送出去 | ||
* @return {[type]} [description] | ||
*/ | ||
sendCookie: function(){ | ||
var cookies = Object.values(this._cookie); | ||
if (cookies.length) { | ||
this.setHeader("Set-Cookie", cookies); | ||
this._cookie = {}; | ||
}; | ||
}, | ||
/** | ||
* url跳转 | ||
@@ -214,3 +215,3 @@ * @param {[type]} url [description] | ||
echo: function(obj, encoding){ | ||
this.setCookie(true); | ||
this.sendCookie(); | ||
if (isArray(obj) || isObject(obj)) { | ||
@@ -229,4 +230,7 @@ obj = JSON.stringify(obj); | ||
end: function(){ | ||
this.setCookie(true); | ||
this.sendCookie(); | ||
this.res.end(); | ||
//刷新session | ||
var session = self.http.session; | ||
session && session.flush && session.flush(); | ||
} | ||
@@ -233,0 +237,0 @@ } |
@@ -275,3 +275,3 @@ var util = require('util'); | ||
*/ | ||
cache: function(key, expire, type){ | ||
cache: function(key, timeout, type){ | ||
if (key === undefined) { | ||
@@ -285,4 +285,4 @@ return this; | ||
if (isNumber(key)) { | ||
type = expire; | ||
expire = key; | ||
type = timeout; | ||
timeout = key; | ||
key = ""; | ||
@@ -292,3 +292,3 @@ }; | ||
key: key, | ||
expire: expire, | ||
timeout: timeout, | ||
type: type | ||
@@ -659,3 +659,3 @@ }; | ||
data = self.parseData(data); | ||
if (isEmpty(parsedOptions.where) || isEmpty(parsedOptions.where[pk])) { | ||
if (isEmpty(parsedOptions.where)) { | ||
// 如果存在主键数据 则自动作为更新条件 | ||
@@ -662,0 +662,0 @@ if (!isEmpty(data[pk])) { |
@@ -8,4 +8,2 @@ //自动加载进行识别的路径 | ||
var Think = module.exports = { | ||
//setInterval timer | ||
timer: 0, | ||
/** | ||
@@ -168,3 +166,3 @@ * [start description] | ||
var self = this; | ||
this.timer = setInterval(function(){ | ||
setInterval(function(){ | ||
//清除已经绑定的自定义事件 | ||
@@ -171,0 +169,0 @@ E(null); |
var fs = require("fs"); | ||
/** | ||
* 文件缓存 | ||
* 基于文件的缓存 | ||
* @return {[type]} [description] | ||
@@ -8,33 +8,24 @@ */ | ||
return { | ||
gcType: "FileCache", | ||
init: function(options){ | ||
this.super_("init", options); | ||
if (this.options.temp.substr(-1) !== "/") { | ||
this.options.temp += "/"; | ||
}; | ||
if (!isDir(this.options.temp)) { | ||
mkdir(this.options.temp); | ||
}; | ||
chmod(this.options.temp); | ||
this.options = extend({ | ||
cache_path: C('cache_path'), //缓存目录 | ||
cache_path_level: 2, //缓存子目录深度 | ||
cache_file_suffix: C('cache_file_suffix') //缓存文件后缀名 | ||
}, this.options); | ||
mkdir(this.options.cache_path); | ||
}, | ||
/** | ||
* 缓存的文件名 | ||
* 存储的缓存文件 | ||
* @param {[type]} name [description] | ||
* @return {[type]} [description] | ||
*/ | ||
filename: function(name){ | ||
name = md5(name); | ||
var filename = ""; | ||
if (C('data_path_level')) { | ||
var dir = ""; | ||
for(var i=0,length=C('data_path_level');i<length;i++){ | ||
dir += name[i] + "/"; | ||
} | ||
if (!isDir(this.options.temp + dir)) { | ||
mkdir(this.options.temp + dir); | ||
}; | ||
filename = dir + this.options.prefix + name + C('data_file_suffix'); | ||
}else{ | ||
filename = this.options.prefix + name + C('data_file_suffix'); | ||
} | ||
return this.options.temp + filename; | ||
getStoredFile: function(name){ | ||
name = md5(this.key || name); | ||
var dir = name.split("").slice(0, this.options.cache_path_level).join("/"); | ||
mkdir(this.options.cache_path + "/" + dir); | ||
var path = this.options.cache_path + "/" + dir + "/" + name + this.options.cache_file_suffix; | ||
return path; | ||
}, | ||
@@ -47,30 +38,30 @@ /** | ||
get: function(name){ | ||
var filename = this.filename(name); | ||
if (!isFile(filename)) { | ||
var filePath = this.getStoredFile(name); | ||
if (!isFile(filePath)) { | ||
return getPromise(); | ||
}; | ||
N('cache_read', 1); | ||
var content = getFileContent(filename); | ||
if (content) { | ||
var expire = parseInt(content.substr(0, 20), 10) * 1000; | ||
var fileState = fs.statSync(filename); | ||
var mtime = new Date(fileState.mtime) * 1; | ||
if (expire !== 0 && Date.now() > (mtime + expire)) { | ||
fs.unlink(filename, function(){}); | ||
return getPromise(); | ||
var deferred = getDefer(); | ||
var self = this; | ||
fs.exists(filePath, function(exists){ | ||
if (!exists) { | ||
return deferred.resolve(); | ||
}; | ||
if(C('data_cache_check')){ | ||
var check = content.substr(20, 32); | ||
content = content.substr(52); | ||
if (check != md5(content)) { | ||
fs.unlink(filename, function(){}); | ||
return getPromise(); | ||
fs.readFile(filePath, function(error, content){ | ||
if (error || !content) { | ||
return deferred.resolve(); | ||
}; | ||
}else{ | ||
content = content.substr(20); | ||
} | ||
content = JSON.parse(content); | ||
return getPromise(content); | ||
}; | ||
return getPromise(); | ||
var data = JSON.parse(content); | ||
if (Date.now() > data.expire) { | ||
fs.unlink(filePath); | ||
return deferred.resolve(); | ||
}; | ||
//更新expire时间 | ||
if (self.updateExpire) { | ||
data.expire = Date.now() + data.timeout * 1000; | ||
fs.writeFile(filePath, JSON.stringify(data), function(){}); | ||
}; | ||
deferred.resolve(data.data[name]); | ||
}) | ||
}) | ||
return deferred.promise; | ||
}, | ||
@@ -83,28 +74,49 @@ /** | ||
*/ | ||
set: function(name, value, expire){ | ||
N("cache_write", 1); | ||
if (expire === undefined) { | ||
expire = this.options.expire; | ||
set: function(name, value, timeout){ | ||
if (timeout === undefined) { | ||
timeout = this.options.timeout; | ||
}; | ||
var filename = this.filename(name); | ||
var data = JSON.stringify(value); | ||
var check = ""; | ||
if (C('data_cache_check')) { | ||
check = md5(data); | ||
}; | ||
var length = (expire + "").length; | ||
expire = (new Array(21 - length)).join("0") + expire; | ||
data = expire + check + data; | ||
return setFileContent(filename, data); | ||
var filePath = this.getStoredFile(name); | ||
var data = { | ||
data: getObject(name, value), | ||
expire: Date.now() + timeout * 1000, | ||
timeout: timeout | ||
} | ||
return setFileContent(filePath, JSON.stringify(data)); | ||
}, | ||
/** | ||
* 删除缓存 | ||
* @param {[type]} name [description] | ||
* @return {[type]} [description] | ||
*/ | ||
rm: function(name){ | ||
var filename = this.filename(name); | ||
if (isFile(filename)) { | ||
fs.unlink(filename); | ||
}; | ||
var filePath = this.getStoredFile(name); | ||
isFile(filePath) && fs.unlink(filePath); | ||
}, | ||
clear: function(){ | ||
/** | ||
* gc | ||
* @param {[type]} now [description] | ||
* @return {[type]} [description] | ||
*/ | ||
gc: function(now, path){ | ||
path = path || this.options.cache_path; | ||
var self = this; | ||
var files = fs.readdirSync(path); | ||
files.forEach(function(item){ | ||
var filePath = path + "/" + item; | ||
var stat = fs.statSync(filePath); | ||
if (stat.isDirectory()) { | ||
self.gc(now, filePath); | ||
}else if (stat.isFile()) { | ||
var data = getFileContent(filePath); | ||
try{ | ||
data = JSON.parse(data); | ||
if (now > data.expire) { | ||
fs.unlink(filePath, function(){}) | ||
}; | ||
}catch(e){} | ||
}; | ||
}) | ||
} | ||
} | ||
}) |
@@ -7,85 +7,21 @@ /** | ||
var os = require("os"); | ||
var fs = require("fs"); | ||
//生成存放cookie的临时目录 | ||
var sessionTmpDir = os.tmpdir() + "/thinkjs"; | ||
mkdir(sessionTmpDir); | ||
module.exports = Session(function(){ | ||
module.exports = inherits("FileCache", function(){ | ||
return { | ||
/** | ||
* 差异化的init | ||
* @return {[type]} [description] | ||
* gc类型 | ||
* @type {String} | ||
*/ | ||
afterInit: function(){ | ||
this.data = null; | ||
//cookie文件 | ||
this.file = sessionTmpDir + "/" + this.cookie + ".json"; | ||
}, | ||
gcType: "FileSession", | ||
/** | ||
* 异步获取数据,返回一个promise | ||
* @param {[type]} name [description] | ||
* @return {[type]} [description] | ||
*/ | ||
get: function(name){ | ||
if (this.data) { | ||
return getPromise(this.data[name] || ""); | ||
}; | ||
var deferred = getDefer(); | ||
var self = this; | ||
fs.exists(this.file, function(exists){ | ||
self.data = {}; | ||
if (!exists) { | ||
return deferred.resolve(""); | ||
}; | ||
fs.readFile(self.file, function(err, data){ | ||
if (err) { | ||
return deferred.resolve(""); | ||
}; | ||
data = JSON.parse(data); | ||
self.data = data; | ||
deferred.resolve(data[name]); | ||
}) | ||
}) | ||
return deferred.promise; | ||
}, | ||
/** | ||
* 设置session | ||
* @param {[type]} name [description] | ||
* @param {[type]} value [description] | ||
*/ | ||
set: function(name, value){ | ||
if (this.data === null) { | ||
this.data = {}; | ||
}; | ||
this.data[name] = value; | ||
fs.writeFile(this.file, JSON.stringify(this.data), function(){}); | ||
}, | ||
/** | ||
* 删除session | ||
* 差异化的init | ||
* @return {[type]} [description] | ||
*/ | ||
rm: function(){ | ||
this.data = {}; | ||
fs.unlink(this.file, function(){}); | ||
}, | ||
/** | ||
* gc | ||
* @param {[type]} now [description] | ||
* @param {[type]} timeout [description] | ||
* @return {[type]} [description] | ||
*/ | ||
gc: function(now, timeout){ | ||
fs.readdir(sessionTmpDir, function(err, files){ | ||
files.forEach(function(file){ | ||
file = sessionTmpDir + '/' + file; | ||
var mtime = fs.statSync(file).mtime; | ||
var time = (new Date(mtime)).getTime(); | ||
if (now - time > timeout) { | ||
fs.unlink(file, function(){}); | ||
}; | ||
}) | ||
}) | ||
init: function(options){ | ||
this.super_("init", options); | ||
this.options.cache_path = C('session_path') || (os.tmpdir() + "/thinkjs"); | ||
this.key = this.options.cookie; | ||
this.updateExpire = true; | ||
mkdir(this.options.cache_path); | ||
} | ||
} | ||
}) |
/** | ||
* 缓存类 | ||
* 缓存基类 | ||
* @return {[type]} [description] | ||
*/ | ||
/** | ||
* 缓存数据 | ||
* @type {Object} | ||
*/ | ||
var cacheData = {}; | ||
/** | ||
* 定时器 | ||
* @type {Number} | ||
*/ | ||
var gcTimer = {}; | ||
/** | ||
* 清除已经过期的Cache | ||
* @return {[type]} [description] | ||
*/ | ||
var gc = function(instance){ | ||
if (APP_DEBUG || APP_MODE || gcTimer[instance.gcType]) { | ||
return; | ||
}; | ||
gcTimer[instance.gcType] = setInterval(function(){ | ||
var hour = (new Date).getHours(); | ||
if (C('cache_gc_hour').indexOf(hour) === -1) { | ||
return; | ||
}; | ||
var now = Date.now(); | ||
instance.gc && instance.gc(now); | ||
}, 3600 * 1000); | ||
} | ||
module.exports = Class(function(){ | ||
var caches = {}; | ||
return { | ||
options: {}, | ||
/** | ||
* gc的类型,用于定时器类型判断 | ||
* @type {String} | ||
*/ | ||
gcType: "Cache", | ||
/** | ||
* 初始化 | ||
* @param {[type]} options [description] | ||
* @return {[type]} [description] | ||
*/ | ||
init: function(options){ | ||
options = isObject(options) ? options : {}; | ||
this.options = extend({ | ||
"temp": C('data_cache_path'), //存储目录 | ||
"prefix": C('data_cache_prefix'), //key前缀 | ||
"expire": C('data_cache_time') //缓存时间 | ||
}, options); | ||
timeout: C('cache_timeout') | ||
}, options || {}); | ||
//操作的key | ||
this.key = ""; | ||
//是否更新expire值 | ||
this.updateExpire = false; | ||
//缓存数据 | ||
this.cacheData = cacheData; | ||
gc(this); | ||
}, | ||
@@ -23,23 +63,15 @@ /** | ||
get: function(name){ | ||
if (!(name in caches)) { | ||
var key = this.key || name; | ||
if (!(key in this.cacheData)) { | ||
return getPromise(); | ||
}; | ||
var value = caches[name]; | ||
var data = value.data; | ||
var expire = value.expire * 1000; | ||
//永久缓存 | ||
if (expire === 0) { | ||
return getPromise(data); | ||
}; | ||
var time = value.time; | ||
if (Date.now() > (time + expire)) { | ||
delete caches[name]; | ||
var value = this.cacheData[key]; | ||
if (Date.now() > value.expire) { | ||
delete this.cacheData[key]; | ||
return getPromise(); | ||
}; | ||
// 这里不做深度复制,产品使用的地方根据需要做 | ||
// var isArr = false; | ||
// if ((isArr = isArray(data)) || isObject) { | ||
// data = extend(isArr ? [] : {}, data); | ||
// }; | ||
return getPromise(data); | ||
if (this.updateExpire) { | ||
this.cacheData[key].expire = Date.now() + value.timeout * 1000; | ||
}; | ||
return getPromise(value.data[name]); | ||
}, | ||
@@ -50,14 +82,17 @@ /** | ||
* @param {[type]} value [description] | ||
* @param {[type]} expire [description] | ||
*/ | ||
set: function(name, value, expire){ | ||
if (expire === undefined) { | ||
expire = this.options.expire; | ||
set: function(name, value, timeout){ | ||
if (timeout === undefined) { | ||
timeout = this.options.timeout; | ||
}; | ||
caches[name] = { | ||
expire: expire, | ||
time: Date.now(), | ||
data: value | ||
}; | ||
return this; | ||
var key = this.key || name; | ||
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 | ||
} | ||
} | ||
}, | ||
@@ -70,12 +105,21 @@ /** | ||
rm: function(name){ | ||
delete caches[name]; | ||
var key = this.key || name; | ||
if (key in this.cacheData) { | ||
delete this.cacheData[key].data[name]; | ||
}; | ||
}, | ||
/** | ||
* 清空缓存值 | ||
* @return {[type]} [description] | ||
* gc | ||
* @param {[type]} now [description] | ||
* @return {[type]} [description] | ||
*/ | ||
clear: function(){ | ||
caches = {}; | ||
gc: function(now){ | ||
for(var key in this.cacheData){ | ||
var item = this.cacheData[key] || {}; | ||
if (Date.now() > item.expire) { | ||
delete this.cacheData[key]; | ||
}; | ||
} | ||
} | ||
} | ||
}); |
@@ -35,100 +35,7 @@ var crypto = require('crypto'); | ||
/** | ||
* 定时器 | ||
* @type {Number} | ||
*/ | ||
var gcTimer = 0; | ||
/** | ||
* 清除已经过期的session数据 | ||
* @return {[type]} [description] | ||
*/ | ||
var gc = function(instance){ | ||
if (APP_DEBUG || APP_MODE || gcTimer) { | ||
return; | ||
}; | ||
//超时时间 | ||
var timeout = Math.max(24 * 3600 * 1000, C('cookie_expires')); | ||
gcTimer = setInterval(function(){ | ||
var hour = (new Date).getHours(); | ||
//早上四点清理 | ||
if (hour !== 4) { | ||
return; | ||
}; | ||
var now = Date.now(); | ||
instance.gc && instance.gc(now, timeout); | ||
}, 3600 * 1000); | ||
} | ||
/** | ||
* 存储session的数据变量 | ||
* @type {Object} | ||
*/ | ||
var session_data = {}; | ||
var Session = module.exports = Class(function(){ | ||
var Session = module.exports = Cache(function(){ | ||
return { | ||
cookie: null, | ||
init: function(http){ | ||
this.http = http; | ||
//session_cookie是未签名的cookie | ||
this.cookie = http.session_cookie; | ||
gc(this); | ||
this.afterInit && this.afterInit(); | ||
}, | ||
/** | ||
* 差异化的init放在这里实现 | ||
* @return {[type]} [description] | ||
*/ | ||
afterInit: function(){ | ||
if (!(this.cookie in session_data)) { | ||
session_data[this.cookie] = {}; | ||
}; | ||
}, | ||
/** | ||
* 获取session,返回一个promise | ||
* @param {[type]} name | ||
* @return {[type]} | ||
*/ | ||
get: function(name){ | ||
session_data[this.cookie].__ACTIVE_TIME__ = Date.now(); | ||
var value = session_data[this.cookie][name] || ""; | ||
return getPromise(value); | ||
}, | ||
/** | ||
* 设置session | ||
* @param {[type]} name [description] | ||
* @param {[type]} value [description] | ||
*/ | ||
set: function(name, value){ | ||
if (isObject(name)) { | ||
for(var key in name){ | ||
session_data[this.cookie][key] = name[key]; | ||
} | ||
}else { | ||
session_data[this.cookie][name] = value; | ||
} | ||
}, | ||
/** | ||
* 移除session | ||
* @return {[type]} [description] | ||
*/ | ||
rm: function(name){ | ||
if (name) { | ||
delete session_data[this.cookie][name]; | ||
}else{ | ||
session_data[this.cookie] = {}; | ||
} | ||
}, | ||
/** | ||
* gc | ||
* @param {[type]} now [description] | ||
* @param {[type]} timeout [description] | ||
* @return {[type]} [description] | ||
*/ | ||
gc: function(now, timeout){ | ||
for(var cookie in session_data){ | ||
var time = session_data[cookie].__ACTIVE_TIME__ || 0; | ||
if (now - time > timeout) { | ||
delete session_data[cookie]; | ||
}; | ||
} | ||
init: function(options){ | ||
this.super_("init", options); | ||
this.key = this.options.cookie; | ||
} | ||
@@ -140,8 +47,8 @@ } | ||
Session.start = function(http){ | ||
if (http.session_cookie) { | ||
return http.session_cookie; | ||
if (http.session) { | ||
return http.session; | ||
}; | ||
var name = C('session_id'); | ||
var name = C('session_name'); | ||
//是否使用签名 | ||
var secret = C('session_cookieSign'); | ||
var secret = C('session_sign'); | ||
var cookie = http.cookie[name]; | ||
@@ -160,10 +67,9 @@ if (cookie && secret) { | ||
} | ||
//将cookie值放到http对象上,方便后续获取 | ||
http.session_cookie = session_cookie; | ||
var name = C('session_type') + "Session"; | ||
//session类 | ||
http.session = thinkRequire(name)(http); | ||
http.session = thinkRequire(name)({ | ||
cookie: session_cookie, | ||
timeout: C('session_timeout') | ||
}); | ||
return cookie; | ||
}; |
{ | ||
"name": "thinkjs", | ||
"description": "thinkphp web framework for nodejs", | ||
"version": "0.4.10", | ||
"version": "0.5.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "welefen", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
32
4
209642
6462