位置:海鸟网 > IT > JavaScript >

一个简单的JavaScript数据缓存系统实现代码

  数据缓存系统,主要是将一些可复用的数据临时存放一下,放下数据后面的再次调用


var DataCache = function(){
if(!(this instanceof DataCache)){
return new DataCache();
}
this.id = 0;
this.caches = {};
};
DataCache.prototype = {
add : function(val){
val = val || null;
key = "dc_" + this.id;

this.caches[key] = val;
return key;
},
remove : function(key){
delete this.caches[key];
},
get : function(key){
return this.caches[key];
},
set : function(key,val){
this.caches[key] = val;
}
};