5 Star 25 Fork 8

crazymus/Smarge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
smarge1.0.js 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
crazymus 提交于 2015-08-11 09:16 . 创建项目。
/**
* @name Smarge
* @author crazymus < QQ:291445576 >
* @des A cache framework base on HTML5 localstorage.
* @version 1.0.0
* @Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
* @updated 2015-08-10
*/
var Smarge = {
db:'SMARGE0',
use:function(index){
if(index<0 || index>16){
console.log('warning:0-16 is legal.');
return ;
}
this.db = 'SMARGE'+index;
},
set:function(key,value,expire){
var data;
if(localStorage[this.db]){
data = JSON.parse(localStorage[this.db]);
}else{
data = {};
}
var d = new Date();
var obj = {
value:JSON.stringify(value),
time:(d.getTime()/1000).toFixed(0),
expire:expire?expire:0
};
data[key] = obj;
localStorage[this.db] = JSON.stringify(data);
},
get:function(key){
var data;
if(localStorage[this.db]){
data = JSON.parse(localStorage[this.db]);
}else{
return undefined;
}
if(!data[key]){
return undefined;
}
var obj = data[key];
if(obj.expire>0){
//检查过期时间
if(this._getTime() - obj.time > obj.expire){
data[key] = undefined;
localStorage[this.db] = JSON.stringify(data);
return undefined;
}
}
return JSON.parse(data[key].value);
},
drop:function(index){
localStorage['SMARGE'+index] = "";
},
//获取当前时间戳
_getTime:function(){
var d = new Date();
return (d.getTime()/1000).toFixed(0);
}
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/jiusem/Smarge.git
[email protected]:jiusem/Smarge.git
jiusem
Smarge
Smarge
master

搜索帮助