标签:
//内存层 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可以存放5MB的内容
以下公式可以计算localStorage的空间是否已经满了
1024*1024*5-unescape(encodeURIComponent(JSON.stringify(localStorage))).length
<html manifest="birds.appcache">
CACHE MANIFEST # The date below is a simple # way to make sure I change this #file # 2013-03-15r4 jquery-1.8.0.min.js gull-360x112.jpg gull-640x360.jpg gull-720x225.jpg FALLBACK: NETWORK: *
标签:
原文地址:http://my.oschina.net/lilugirl2005/blog/526032