我的应用程序使用API数量有限的请求从另一个服务器接收数据。数据变化很少,但即使在刷新页面之后也可能是必需的。在客户端使用js缓存数据的最佳方式是什么?
- 什么是最好的解决方案这个问题,使用Cookie或HTML5 WebStorage?
- 而且可能有别的办法来解决这个任务?
我的应用程序使用API数量有限的请求从另一个服务器接收数据。数据变化很少,但即使在刷新页面之后也可能是必需的。在客户端使用js缓存数据的最佳方式是什么?
尽管跨浏览器兼容性问题,cookie
是唯一的选择,而不是网络存储。
但这个问题真的取决于你要缓存什么样的数据?
对于您所尝试的,可能根本不需要cookie和网络存储。
我宁愿说,这将是愚蠢的缓存整个页面,Cookie或网络存储两者。出于这些目的,服务器端缓存选项可能是更好的方法。
更新:
引用:
关于用户活动的数据在某些社交网络(FB,VK,谷歌+)
检测所述网络的存储功能,使用库如mordernizr,如果不存在则回退到cookie方法。 一个简单的例子
if (Modernizr.localstorage) {
// browser supports local storage
// Use this method
} else {
// browser doesn't support local storage
// Use Cookie Method
}
我写这个LIB解决同样的问题:
Cache your data with Javascript using cacheJS
下面是一些基本用法
// just add new cache using array as key
cacheJS.set({blogId:1,type:'view'},'<h1>Blog 1</h1>');
cacheJS.set({blogId:1,type:'json'}, jsonData);
// remove cache using key
cacheJS.removeByKey({blogId:1,type:'json'});
// add cache with ttl and contextual key
cacheJS.set({blogId:2,type:'view'},'<h1>Blog 2</h1>', 3600, {author:'hoangnd'});
cacheJS.set({blogId:3,type:'view'},'<h1>Blog 3</h1>', 3600, {author:'hoangnd'});
// remove cache with con textual key
// cache for blog 2 and 3 will be removed
cacheJS.removeByContext({author:'hoangnd'})
什么样的数据......嗯。例如,有关某些社交网络(fb,vk,google +)中用户活动的数据。在服务器端缓存数据对我来说非常糟糕,因为这么多的数据=)这个应用程序的跨浏览器兼容性不是主要任务,这个应用程序为现代浏览器。 – artzub
@artzub,检查我的更新 – Starx
Thx,我看它! – artzub