Comparing version 0.5.6 to 0.5.7
@@ -423,2 +423,20 @@ var fs = require("fs"); | ||
return obj; | ||
}; | ||
}; | ||
/** | ||
* 将数组变成对象 | ||
* @param {[type]} arr [description] | ||
* @param {[type]} key [description] | ||
* @param {[type]} valueKeys [description] | ||
* @return {[type]} [description] | ||
*/ | ||
global.arrToObj = function(arr, key, valueKey){ | ||
var result = {}; | ||
arr.forEach(function(item){ | ||
var keyValue = item[key]; | ||
if (valueKey) { | ||
item = item[valueKey]; | ||
}; | ||
result[keyValue] = item; | ||
}) | ||
return result; | ||
} |
@@ -254,2 +254,5 @@ var fs = require("fs"); | ||
"use strict"; | ||
if (options && !isObject(options)) { | ||
options = {timeout: options}; | ||
} | ||
options = options || {}; | ||
@@ -266,4 +269,3 @@ var type = options.type === undefined ? C('cache_type') : options.type; | ||
} | ||
var timeout = isNumber(options) ? options : options.timeout; | ||
return instance.set(name, value, timeout); | ||
return instance.set(name, value, options.timeout); | ||
}; | ||
@@ -270,0 +272,0 @@ /** |
@@ -69,2 +69,5 @@ /** | ||
db_cache_on: true, //是否启用查询缓存,如果关闭那么cache方法则无效 | ||
db_cache_type: "", //缓存类型,默认为内存缓存 | ||
db_cache_path: CACHE_PATH + "/db", //缓存路径,File类型下有效 | ||
db_cache_timeout: 3600, //缓存时间,默认为1个小时 | ||
@@ -71,0 +74,0 @@ tpl_content_type: "text/html", //模版输出类型 |
@@ -8,3 +8,3 @@ /** | ||
db_cache_on: false, | ||
debug_retain_files: ['/node_modules/', '/Model.js'], //这些文件在debug模式下不清除缓存 | ||
debug_retain_files: ['/node_modules/', '/thinkjs/'], //这些文件在debug模式下不清除缓存 | ||
use_cluster: false, | ||
@@ -11,0 +11,0 @@ html_cache_on: false, |
@@ -5,5 +5,6 @@ /** | ||
*/ | ||
var tplFiles = {}; | ||
var path = require("path"); | ||
var fs = require("fs"); | ||
var tplFiles = {}; | ||
/** | ||
@@ -10,0 +11,0 @@ * 写入html缓存 |
@@ -200,6 +200,7 @@ var cluster = require("cluster"); | ||
//不使用cluster | ||
if (clusterNums === false) { | ||
if (!clusterNums) { | ||
return App.createServer(); | ||
} | ||
if (clusterNums === 0 || clusterNums === true) { | ||
//使用cpu的个数 | ||
if (clusterNums === true) { | ||
clusterNums = require('os').cpus().length; | ||
@@ -206,0 +207,0 @@ } |
@@ -185,4 +185,3 @@ /** | ||
if (value !== undefined) { | ||
instance.set(name, value); | ||
return this; | ||
return instance.set(name, value); | ||
} | ||
@@ -199,3 +198,3 @@ return instance.get(name); | ||
this.http.redirect(url, code); | ||
return this; | ||
return getDefer().promise; | ||
}, | ||
@@ -239,3 +238,3 @@ /** | ||
action: function(action) { | ||
A(action, this.http); | ||
return A(action, this.http); | ||
}, | ||
@@ -331,2 +330,3 @@ /** | ||
obj.data = data || ""; | ||
this.header("Content-Type", C('json_content_type')); | ||
this.end(obj); | ||
@@ -344,2 +344,3 @@ }, | ||
obj.data = data; | ||
this.header("Content-Type", C('json_content_type')); | ||
this.end(obj); | ||
@@ -346,0 +347,0 @@ }, |
@@ -83,3 +83,3 @@ var url = require("url"); | ||
} | ||
return "SET " + set.join(","); | ||
return " SET " + set.join(","); | ||
}, | ||
@@ -665,6 +665,7 @@ /** | ||
var self = this; | ||
var cacheOn = !isEmpty(cache) && C('db_cache_on'); | ||
//获取数据 | ||
function queryData(){ | ||
return self.query(sql).then(function(data){ | ||
if (cache) { | ||
if (cacheOn) { | ||
S(key, data, cache); | ||
@@ -675,3 +676,3 @@ } | ||
} | ||
if (!isEmpty(cache) && C('db_cache_on')) { | ||
if (cacheOn) { | ||
var key = isString(cache.key) && cache.key ? cache.key : md5(sql); | ||
@@ -678,0 +679,0 @@ return S(key, undefined, cache).then(function(value){ |
@@ -10,2 +10,6 @@ var util = require('util'); | ||
var tableFields = {}; | ||
//db缓存数据 | ||
var dbCacheData = {}; | ||
//等待数据 | ||
var dbWaitingData = {}; | ||
/** | ||
@@ -121,8 +125,15 @@ * Model类 | ||
} | ||
// 数据库初始化操作 | ||
// 获取数据库操作对象 | ||
// 当前模型有独立的数据库连接信息 | ||
this.promise = this.initDb(0, connection || this.connection); | ||
}, | ||
/** | ||
* 初始化数据库连接的promise | ||
* @return {[type]} [description] | ||
*/ | ||
initPromise: function(){ | ||
if (this.promise) { | ||
return this.promise; | ||
}; | ||
this.promise = this.initDb(0); | ||
return this.promise; | ||
}, | ||
/** | ||
* 初始化数据库连接 | ||
@@ -305,22 +316,37 @@ * @access public | ||
*/ | ||
cache: function(key, timeout, type){ | ||
cache: function(key, timeout){ | ||
if (key === undefined) { | ||
return this; | ||
} | ||
var options = this.getCacheOptions(key, timeout); | ||
this._options.cache = options; | ||
return this; | ||
}, | ||
/** | ||
* 获取缓存的选项 | ||
* @param {[type]} key [description] | ||
* @param {[type]} timeout [description] | ||
* @return {[type]} [description] | ||
*/ | ||
getCacheOptions: function(key, timeout, type){ | ||
if (isObject(key)) { | ||
this._options.cache = key; | ||
return key; | ||
} | ||
if (isNumber(key)) { | ||
timeout = key; | ||
key = ""; | ||
}; | ||
var cacheType = type === undefined ? C('db_cache_type') : type; | ||
var options = { | ||
key: key, | ||
timeout: timeout || C('db_cache_timeout'), | ||
type: cacheType, | ||
gcType: "dbCache" | ||
} | ||
if (cacheType === 'File') { | ||
options.cache_path = C('db_cache_path'); | ||
}else{ | ||
//如果没有key,则根据sql语句自动生成 | ||
if (isNumber(key)) { | ||
type = timeout; | ||
timeout = key; | ||
key = ""; | ||
} | ||
this._options.cache = { | ||
key: key, | ||
timeout: timeout, | ||
type: type | ||
}; | ||
options.cacheData = dbCacheData; | ||
} | ||
return this; | ||
return options; | ||
}, | ||
@@ -428,3 +454,3 @@ /** | ||
if (isEmpty(self.fields._field)) { | ||
promise = this.promise.then(function(){ | ||
promise = this.initPromise().then(function(){ | ||
return self.fields._field; | ||
@@ -966,3 +992,7 @@ }); | ||
}else{ | ||
parse = isArray(parse) ? parse : [parse]; | ||
if (parse === undefined) { | ||
parse = []; | ||
}else{ | ||
parse = isArray(parse) ? parse : [parse]; | ||
} | ||
parse.unshift(sql); | ||
@@ -978,6 +1008,35 @@ sql = util.format.apply(null, parse); | ||
} | ||
this.db.setModel(this.name); | ||
return promise; | ||
return this.initDb().then(function(){ | ||
self.db.setModel(self.name); | ||
return promise; | ||
}); | ||
}, | ||
/** | ||
* 等待第一个回调成功后,才执行后面的回调 | ||
* @param {[type]} key [description] | ||
* @param {Function} callback [description] | ||
* @return {[type]} [description] | ||
*/ | ||
waiting: function(key, callback){ | ||
if (key in dbWaitingData) { | ||
if (dbWaitingData[key].status == 'done') { | ||
return getPromise(dbWaitingData[key].data); | ||
}else{ | ||
var deferred = getDefer(); | ||
dbWaitingData[key].deferred.push(deferred); | ||
return deferred.promise; | ||
} | ||
}; | ||
dbWaitingData[key] = {data: null, status: "waiting", deferred: []}; | ||
return getPromise(callback()).then(function(data){ | ||
dbWaitingData[key].data = data; | ||
dbWaitingData[key].status = 'done'; | ||
dbWaitingData[key].deferred.forEach(function(deferred){ | ||
deferred.resolve(data); | ||
}); | ||
dbWaitingData[key].deferred.length = 0; | ||
return data; | ||
}) | ||
}, | ||
/** | ||
* 设置数据对象值 | ||
@@ -1067,2 +1126,14 @@ * @return {[type]} [description] | ||
dbConfigs.length = 0; | ||
}; | ||
}; | ||
/** | ||
* 清除数据库缓存 | ||
* @return {[type]} [description] | ||
*/ | ||
Model.clearCache = function(){ | ||
var cacheType = C('db_cache_type'); | ||
if (cacheType === 'File') { | ||
//this._options.cache.cache_path = CACHE_PATH + "/db"; | ||
}else{ | ||
dbCacheData = {}; | ||
} | ||
} |
@@ -23,3 +23,3 @@ /** | ||
} | ||
if (isString(name) && arguments.length === 1) { | ||
if (isString(name) && value === undefined) { | ||
return this.tVar[name]; | ||
@@ -26,0 +26,0 @@ } |
@@ -11,3 +11,2 @@ var fs = require("fs"); | ||
init: function(options){ | ||
this.super_("init", options); | ||
this.options = extend({ | ||
@@ -17,5 +16,7 @@ cache_path: C('cache_path'), //缓存目录 | ||
cache_file_suffix: C('cache_file_suffix') //缓存文件后缀名 | ||
}, this.options); | ||
}, options); | ||
mkdir(this.options.cache_path); | ||
this.gcType += ":" + this.options.cache_path; | ||
mkdir(this.options.cache_path); | ||
this.super_("init", this.options); | ||
}, | ||
@@ -85,3 +86,4 @@ /** | ||
}; | ||
return setFileContent(filePath, JSON.stringify(data)); | ||
setFileContent(filePath, JSON.stringify(data)); | ||
return getPromise(); | ||
}, | ||
@@ -88,0 +90,0 @@ /** |
@@ -10,6 +10,2 @@ /** | ||
return { | ||
/** | ||
* gc类型 | ||
* @type {String} | ||
*/ | ||
gcType: "FileSession", | ||
@@ -21,9 +17,9 @@ /** | ||
init: function(options){ | ||
options = options || {}; | ||
options.cache_path = C('session_path') || (os.tmpdir() + "/thinkjs"); | ||
this.super_("init", options); | ||
this.options.cache_path = C('session_path') || (os.tmpdir() + "/thinkjs"); | ||
this.key = this.options.cookie; | ||
this.key = options.cookie; | ||
this.updateExpire = true; | ||
mkdir(this.options.cache_path); | ||
} | ||
}; | ||
}); |
@@ -11,8 +11,8 @@ /** | ||
var content = getFileContent(templateFile); | ||
var conf = C('tpl_engine_config'); | ||
conf.filename = templateFile; //enable include | ||
var fn = ejs.compile(content, conf); | ||
var html = fn(tVar); | ||
return html; | ||
var conf = extend({ | ||
filename: templateFile, | ||
cache: true | ||
}, C('tpl_engine_config')); | ||
return ejs.compile(content, conf)(tVar); | ||
} | ||
}; |
@@ -156,3 +156,3 @@ /** | ||
var mapKey, mapfKey; | ||
return mapOptions.model.promise.then(function(){ | ||
return mapOptions.model.initPromise().then(function(){ | ||
mapKey = mapOptions.model.getModelName().toLowerCase() + "_id"; | ||
@@ -177,3 +177,3 @@ mapfKey = mapOptions.model.getPk(); | ||
var self = this; | ||
return mapOptions.model.promise.then(function(){ | ||
return mapOptions.model.initPromise().then(function(){ | ||
var where = self.parseRelationWhere(data, mapOptions.mapKey, mapOptions.mapfKey); | ||
@@ -425,3 +425,3 @@ var whereStr = self.db.parseWhere(where); | ||
case UPDATE: | ||
promise = model.promise.then(function(){ | ||
promise = model.initPromise().then(function(){ | ||
var promises = []; | ||
@@ -460,3 +460,3 @@ var pk = model.getPk(); | ||
var model = mapOptions.model; | ||
var promise = model.promise; | ||
var promise = model.initPromise(); | ||
var rfKey = value.rfKey || (model.getModelName().toLowerCase() + "_id"); | ||
@@ -463,0 +463,0 @@ //var relationTable = value.rTable || self.getRelationTableName(model); |
@@ -51,5 +51,15 @@ /** | ||
init: function(options){ | ||
this.options = extend({ | ||
timeout: C('cache_timeout') | ||
}, options || {}); | ||
options = options || {}; | ||
if (options.cacheData) { | ||
this.cacheData = options.cacheData; | ||
}else{ | ||
this.cacheData = cacheData; | ||
} | ||
if (options.gcType) { | ||
this.gcType = options.gcType; | ||
} | ||
if (!options.timeout) { | ||
options.timeout = C('cache_timeout') | ||
} | ||
this.options = options; | ||
//操作的key | ||
@@ -59,4 +69,2 @@ this.key = ""; | ||
this.updateExpire = false; | ||
//缓存数据 | ||
this.cacheData = cacheData; | ||
gc(this); | ||
@@ -103,2 +111,3 @@ }, | ||
} | ||
return getPromise(); | ||
}, | ||
@@ -130,10 +139,2 @@ /** | ||
}; | ||
}); | ||
/** | ||
* 获取缓存数据 | ||
* @param {[type]} name [description] | ||
* @return {[type]} [description] | ||
*/ | ||
module.exports.getCacheData = function(name){ | ||
return name ? cacheData[name] : cacheData; | ||
} | ||
}); |
@@ -71,6 +71,11 @@ var crypto = require('crypto'); | ||
var type = C('session_type'); | ||
if (APP_DEBUG && !type) { | ||
if (!type && APP_DEBUG) { | ||
type = "File"; | ||
console.log("in debug mode, session can't use memory type for storage, it will be convert to `File` type"); | ||
console.log("in debug mode, session can't use memory type for storage, convert to File type"); | ||
} | ||
//使用cluster的时候,不能使用内存来缓存Session | ||
if (!type && C('use_cluster')) { | ||
type = "File"; | ||
console.log("in cluster mode, session can't use memory type for storage, convert to File type") | ||
} | ||
name = type + "Session"; | ||
@@ -77,0 +82,0 @@ //session类 |
{ | ||
"name": "thinkjs", | ||
"description": "thinkphp web framework for nodejs", | ||
"version": "0.5.6", | ||
"version": "0.5.7", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "welefen", |
## 介绍 | ||
thinkjs是一款轻量级的nodejs web框架,里面很多特性借鉴于thinkphp。 | ||
Think.js是一款基于Promise的Node.js MVC框架,无缝支持WebSocket,里面很多特性借鉴于ThinkPHP。 | ||
更多介绍请见 http://www.thinkjs.org/ | ||
## 使用案例 | ||
<http://www.welefen.com> 源码: https://github.com/welefen/thinkpress | ||
<http://www.imququ.com> | ||
<http://www.gitpress.org> 源码: https://github.com/akira-cn/gitpress | ||
以及数字公司内部多个系统 |
235676
6715
15