Javascript强制文件缓存 [英] Javascript force file caching

查看:129
本文介绍了Javascript强制文件缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站每天有200万活跃用户

我不久前读过一篇关于强制javascript和PHP缓存文件的文章。我以前从未需要缓存我的文件,但现在我正在处理从服务器传输的大量数据,我想在客户端本地存储一些这些数据。

I read an article not to long ago about forcing javascript and PHP to cache files. I have never needed to have my files cached before, but now that i am dealing with a massive amount of data being transferred to and from the server i would like to store some of this data locally on the client side.

我不知道是否有更好的方法可以做到这一点但基本上,我正在考虑使用

I don't know if there are any better ways on doing this but essentially, i am considering writing a library using

    $写一个库b $ b
  • HTML5本地存储,如果可用/ 清单

  • 如果有可用的

  • ,则返回java,如果可用,则返回Silverlight。

  • HTML5 local storage if its available / manifest
  • with a fallback of java if its available
  • with a fallback of silverlight if its available.

我非常有兴趣追求这一点,最好是用JavaScript。

I am very interested in pursuing this, preferably in JavaScript.


我想知道如何使用JavaScript缓存文件

I would like to know how to cache files using JavaScript






在有人认为我重新发明之前方向盘

示例
我有几个Javascript文件如果更新,浏览器将不会重新加载脚本,因为它是缓存的。通过版本控制,我可以管理用户何时需要重新加载缓存数据。

(example) I have several Javascript files which if updated, the browser will not reload the script because it is cached. With version control, i can manage when a user needs to reload cached data.

推荐答案

参见在HTTP中缓存。基本上,对于每个请求,您应在响应中指定 cache-control 标头字段,指示何时可以获得新内容。 缓存控制标题字段的正式定义是如下所示:

See caching in HTTP. Basically, for every request you should specify the cache-control header field in the response, indicating when a fresh content will be available. The formal definition of the cache-control header field is as follows:


Cache-Control通用标头字段用于指定所有缓存机制必须遵守的指令
沿
请求/响应链。指令指定了旨在
的行为,以防止缓存对请求或
响应产生负面干扰。这些指令通常会覆盖默认缓存
算法。缓存指令是单向的,因为请求中指令的存在
并不意味着响应中给出了相同的指令

The Cache-Control general-header field is used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain. The directives specify behavior intended to prevent caches from adversely interfering with the request or response. These directives typically override the default caching algorithms. Cache directives are unidirectional in that the presence of a directive in a request does not imply that the same directive is to be given in the response.

该字段通常按照

cache-control:private | public,max-的方式指定。 age = [,no-cache]


public

表示任何缓存都可以缓存响应,即使
通常只能在非
共享缓存中缓存或缓存。 (另请参阅授权,第14.8节,了解额外的
详情。)

Indicates that the response MAY be cached by any cache, even if it would normally be non-cacheable or cacheable only within a non- shared cache. (See also Authorization, section 14.8, for additional details.)

私人

表示全部或部分响应消息
仅供单个用户使用,不得由共享的
缓存缓存。这允许源服务器声明响应的指定部分
仅针对一个用户,并且对于其他用户的请求不是有效的
响应。私有(非共享)缓存可以
缓存响应。注意:私有一词的使用仅控制
,其中可以缓存响应,并且无法确保
邮件内容的隐私。

Indicates that all or part of the response message is intended for a single user and MUST NOT be cached by a shared cache. This allows an origin server to state that the specified parts of the response are intended for only one user and are not a valid response for requests by other users. A private (non-shared) cache MAY cache the response. Note: This usage of the word private only controls where the response may be cached, and cannot ensure the privacy of the message content.

no-cache

如果no-cache指令没有指定字段名,那么
a cache绝不能使用响应在没有成功重新验证原始服务器的情况下满足后续请求
。这允许
源服务器防止缓存,即使是已配置为
的缓存,也可以返回对客户端请求的陈旧响应。如果
no-cache指令确实指定了一个或多个字段名,那么缓存
可以使用该响应来满足后续请求,但受限于缓存的任何
其他限制。但是,在没有
成功重新验证原始服务器的情况下,不得在对后续请求的响应中发送指定的字段名称
。这允许原始
服务器阻止在响应中重复使用某些头字段,
,同时仍允许缓存响应的其余部分。

If the no-cache directive does not specify a field-name, then a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server. This allows an origin server to prevent caching even by caches that have been configured to return stale responses to client requests. If the no-cache directive does specify one or more field-names, then a cache MAY use the response to satisfy a subsequent request, subject to any other restrictions on caching. However, the specified field-name(s) MUST NOT be sent in the response to a subsequent request without successful revalidation with the origin server. This allows an origin server to prevent the re-use of certain header fields in a response, while still allowing caching of the rest of the response.

例如, cache-control:private,max-age = 86400,no-cache 指示客户端缓存响应和重复使用,直到 86400 (24小时)过去。但是,在此时间过去之前,事情可能会发生变化。 no-cache 指令每次都会导致重新验证。这就像浏览器每次询问 我是否真的会向您的用户展示缓存的内容? ETag 标题,您将能够推动重要更改在之前缓存的内容过期之前,您的用户将被使用。

For example, cache-control: private, max-age=86400, no-cache directs the client to cache a response and reuse it until 86400 seconds (24 hours) have elapsed. However, things may change before that time elapses. no-cache directive causes a revalidation each time. It is like the browser asking each time may I really present your user with the cached content? Together with the ETag header, you will be able to push important changes to your user before previously cached content expires.

在重新验证期间,响应中存在的Etag将与之前在相同资源请求中提供的Etag进行比较。如果它们相同,则会确保资源没有更改,因此缓存确实有效。否则,如果它们不同,则资源内容已更改,新内容将作为对用户的响应给出。

During revalidation, an Etag present in a response is compared with the one provided previously in a request for same resource. If they are same, it reassures that the resource has not changed, thus, cache is really valid. Else if they differ, then the resource content has changed, and the new content will be given as response to the user.

了解有关HTTP缓存的更多信息:

Read more about HTTP caching:


  1. https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=en#validating -cached-responses-with-etags

  2. http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

  1. https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=en#validating-cached-responses-with-etags
  2. http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

同时,请注意使用应用程序缓存主要适用于您希望向用户提供离线内容的情况。

Meanwhile, note that the use of the Application Cache is mainly applicable if you wish to provide your users with offline content.

这篇关于Javascript强制文件缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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