thinkjs/lib/Lib/Core/Cache.js
2014-01-26 10:48:34 +08:00

78 lines
2.3 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.

/**
* 缓存类
* @return {[type]} [description]
*/
module.exports = Class(function(){
var caches = {};
return {
options: {},
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);
},
/**
* 获取缓存值返回一个promise
* @param {[type]} name [description]
* @return {[type]} [description]
*/
get: function(name){
if (!(name in caches)) {
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];
return getPromise();
};
// 这里不做深度复制,产品使用的地方根据需要做
// var isArr = false;
// if ((isArr = isArray(data)) || isObject) {
// data = extend(isArr ? [] : {}, data);
// };
return getPromise(data);
},
/**
* 设置缓存值
* @param {[type]} name [description]
* @param {[type]} value [description]
* @param {[type]} expire [description]
*/
set: function(name, value, expire){
if (expire === undefined) {
expire = this.options.expire;
};
caches[name] = {
expire: expire,
time: Date.now(),
data: value
};
return this;
},
/**
* 移除缓存值
* @param {[type]} name [description]
* @return {[type]} [description]
*/
rm: function(name){
delete caches[name];
},
/**
* 清空缓存值
* @return {[type]} [description]
*/
clear: function(){
caches = {};
}
}
});