Fourth ChapterCreate a cache layer
Memory layer 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 Layer 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);
Management Localstorage
Localstorage can store 5MB of content
The following formula calculates whether the Localstorage space is full.
1024*1024*5-unescape (encodeURIComponent (json.stringify (localstorage)). length
App Cache Application Caches
CACHE manifest# The date below is a simple# the sure I #file # 2013-03-15r4jquery-1.8.0.min.jsgull-360x 112.jpggull-640x360.jpggull-720x225.jpgfallback:network:*
HTML5 Touch Interface design and development reading notes