mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
91 lines
3.1 KiB
JavaScript
91 lines
3.1 KiB
JavaScript
/**
|
|
* 文件缓存
|
|
* @return {[type]} [description]
|
|
*/
|
|
|
|
var fs = require("fs");
|
|
module.exports = Cache(function(){
|
|
return {
|
|
init: function(options){
|
|
this.super("init", options);
|
|
if (this.options.temp.substr(-1) !== "/") {
|
|
this.options.temp += "/";
|
|
};
|
|
if (!is_dir(this.options.temp)) {
|
|
mkdir(this.options.temp);
|
|
};
|
|
chmod(this.options.temp, "0777");
|
|
},
|
|
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 (!is_dir(this.options.temp + dir)) {
|
|
mkdir(this.options.temp + dir, "0777");
|
|
};
|
|
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;
|
|
},
|
|
get: function(name){
|
|
var filename = this.filename(name);
|
|
if (!is_file(filename)) {
|
|
return false;
|
|
};
|
|
N('cache_read', 1);
|
|
var content = file_get_contents(filename);
|
|
if (content) {
|
|
var expire = parseInt(content.substr(0, 20), 10) * 1000;
|
|
var fileState = file_state(filename);
|
|
var mtime = new Date(fileState.mtime) * 1;
|
|
if (expire != 0 && Date.now() > (mtime + expire)) {
|
|
fs.unlink(filename, function(){});
|
|
return false;
|
|
};
|
|
if(C('data_cache_check')){
|
|
var check = content.substr(20, 32);
|
|
content = content.substr(52);
|
|
if (check != md5(content)) {
|
|
fs.unlink(filename, function(){});
|
|
return false;
|
|
};
|
|
}else{
|
|
content = content.substr(20);
|
|
}
|
|
content = JSON.parse(content);
|
|
return content;
|
|
};
|
|
},
|
|
set: function(name, value, expire){
|
|
N("cache_write", 1);
|
|
if (expire === undefined) {
|
|
expire = this.options.expire;
|
|
};
|
|
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 file_put_contents(filename, data);
|
|
},
|
|
rm: function(name){
|
|
var filename = this.filename(name);
|
|
if (is_file(filename)) {
|
|
fs.unlink(filename);
|
|
};
|
|
},
|
|
clear: function(){
|
|
|
|
}
|
|
}
|
|
}) |