HTML5触摸界面设计与开发 读书笔记

2016-07-12 10:25:37来源:oschina作者:lilugirl人点击

第四章
创建缓存层
//内存层
var memoryCache={};
var CACHE_TTL=864000000;
var CACHE_PREFIX="ti";
function setCache(mykey,data){
var stamp,obj;
stamp=Date.now();
obj={
date:stamp,
data:data
};
//localstorage层
localStorage.setItem(CACHE_PREFIX+mykey,JSON.stringify(obj));
memoryCache[CACHE_PREFIX+mykey]=obj;
}
console.log(window);
function getCached(mykey){
var key,obj;
key=CACHE_PREFIX+mykey;
if(memoryCache[key]){
if(memoryCache[key].date-Date.now()>CACHE_TTL){
return false;
}
return memoryCache[key].data;
}
obj=localStorage.getItem(key);
if(obj){
obj=JSON.parse(obj);
if(Date.now()-obj.date>CACHE_TTL){
localStorage.removeItem(key);
delete(memoryCache[key]);
return false;
}
memoryCache[key]=obj;
return obj.data;
}
}setCache('lilu',39);
getCached('lilu');
console.log(memoryCache['tililu']);
console.log(localStorage);


管理localStorage

localStorage可以存放5MB的内容


以下公式可以计算localStorage的空间是否已经满了



1024*1024*5-unescape(encodeURIComponent(JSON.stringify(localStorage))).length


应用缓存Application Cache





CACHE MANIFEST
# The date below is a simple
# way to make sure I change this
#file
# 2013-03-15r4jquery-1.8.0.min.js
gull-360x112.jpg
gull-640x360.jpg
gull-720x225.jpg
FALLBACK:
NETWORK:
*


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台