在客户端使用 js 缓存数据的最佳方法是什么? [英] What's the best way use caching data in js on client side?

查看:29
本文介绍了在客户端使用 js 缓存数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用请求数量有限的 API 从另一台服务器接收数据.数据更改很少,但即使在刷新页面后也可能需要.

My application receives data from the another server, using API with limited number of requests. Data changing rarely, but may be necessary even after refresh page.

  1. 这个问题的最佳解决方案是什么,使用 cookie 或 HTML5网络存储?
  2. 也许还有其他方法来解决这个任务?

推荐答案

就跨浏览器兼容性而言,cookie 是唯一的选择,而不是 Web 存储.

As much as cross browser compatibility matters, cookie is the only choice rather than web storage.

但问题真的取决于你缓存的是什么类型的数据?

But the question really depends on what kind of data you are caching?

对于您正在尝试的内容,可能根本不需要 cookie 和网络存储.

For what you are trying, cookie and web-storage might not be needed at all.

  • Cookie 用于存储配置相关信息,而不是实际数据本身.
  • Web 存储支持持久性数据存储,类似于 cookie,但容量大大增强,并且 HTTP 请求标头中不存储任何信息.[1]

我宁愿说,将整个页面缓存为 cookie 或 web 存储两者都是愚蠢的.出于这些目的,服务器端缓存选项可能是更好的方法.

I would rather say, it would be stupid to cache the entire page as cookie or web-storage both. For these purposes, server-side caching options might be the better way.

更新:

引用:

有关某些社交网络(fb、vk、google+)中用户活动的数据

data about user activity in some social networks (fb, vk, google+)

使用像 mordernizr 之类的库检测网络存储功能,如果不存在则回退到 cookie 方法.一个简单的例子

Detect the web-storage features, using libraries like mordernizr and if does not exists fall back to cookie method. A simple example

if (Modernizr.localstorage) {
    // browser supports local storage
    // Use this method
} else {
    // browser doesn't support local storage
    // Use Cookie Method
}

<小时>

[1]:http://en.wikipedia.org/wiki/Web_storage

这篇关于在客户端使用 js 缓存数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆