如何设置我的网站缓存,但仍确保用户拥有最新版本? [英] How do I set my web site to cache but still make sure the user has the most recent version?

查看:179
本文介绍了如何设置我的网站缓存,但仍确保用户拥有最新版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的网站被缓存中受益,但该网站经常更新pretty的。我怎样才能确保用户是获得最新版本?

目前我的的.htaccess 文件看起来是这样的:

 < IfModule mod_expires.c>
#启用过期
ExpiresActive在
#默认指令
ExpiresDefault进入加1月
#我的图标
ExpiresByType图像/ X-图标进入加1年
#图片
ExpiresByType图像/ GIF进入加1月
ExpiresByType图像/ PNG进入加1月
ExpiresByType图像/ JPG进入加1月
ExpiresByType为image / jpeg进入加1月
#CSS
ExpiresByType文本/ CSS进入加1月
#使用Javascript
ExpiresByType应用程序/ JavaScript的进入加1年
< / IfModule>
 

解决方案

您可以使用基地为改性为你预测根据你预期经常改变,给访问时间的应用程序资源。下面是如何修改作品:

假设你正期待你的CSS文件进行修改,每天那么你可以使用 ExpiresByType文本/ CSS修改加1天那么这是会发生的(考虑什么您上传CSS文件的 25-09-2015,23点55分):

  • 一个客户访问你的页面上的 25-09-2015,23:56 和下载所有的CSS文件。现在,由于阿帕奇发送最后修改时间的响应头这样的客户一定会得到的Last-Modified:周五,2015年9月25日23时55分零零秒GMT 因为你使用的是 ExpiresByType 指令使用修改的基础上,将让阿帕奇计算其到期时间为 - > [最后修改时间(时间资源的最后修订在服务器上,在此情况下这将是25个到2015年9月的23点55分的)+用修饰碱基(指定时间的在这种情况下,1天的)],所以在到期日期将被计算为 2015年9月26日23时55 ,因此Apache将发送到期日:星期六,2015年9月26日23点55分零零秒GMT 。因此,二级缓存相关的响应头Apache将发送到客户端将是:

     的Last-Modified:周五,2015年9月25日23点55分00秒格林尼治标准​​时间
    到期日:星期六,2015年9月26日23点55分00秒格林尼治标准​​时间
     

  • 现在,让我们说客户端再次访问该页面 2015年9月26日3时55 则可以看到该文件未过期,因此将使用高速缓存版本文档。这会按你的预期。

  • 现在,让我们看看吧客户端再次访问该页面 2015年9月26日23:56 则可以看到该文件已经过期,因此将请求发送到服务器进行新文件。

所以,现在客户端将发送一个请求到服务器每次发现该文件已过期。当你做一个新的上载,然后Apache将重新计算的到期时间,并会在发送相同过期响应头。

您可以看一下这个问题的答案,它是很不错的。

有关,你不预期被频繁修改的资源,您可以继续使用访问为基础,就像你已经在做正确的网站图标不能预期在一年内改变。


不幸的是阿帕奇不能完全解决这个问题的目的 - 的从服务器获取新鲜的文件,但必须修改的版本可以在服务器上其他人继续使用缓存的文档的,因为直到请求被发送到服务器,客户端不能真正知道文件是否已经改变或没有。或推技术需要实现,其中服务器推出了一些基于它的信息和客户的行为。

其他的一些信息:
HTML5提供了一个功能叫做应用程序缓存/离线缓存使离线导航,但
它被视为打破,将DIS-持续。你可以看一下,如果你想服务工作者,以寻找实现用户线下体验。

I want my site to benefit from being cached but the site is updated pretty often. How can I make sure the user is getting the most recent version?

Currently my .htaccess file looks like this:

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>

解决方案

You can use base as "modification" for the app resources which you anticipate to change often and give access time based on your anticipation. Below is how "modification" works:

Suppose you are anticipating your CSS files to be modified every day then you can use ExpiresByType text/css "modification plus 1 day" then this is what will happen (considering you uploaded CSS files on 25-09-2015, 23:55):

  • A client access your page on 25-09-2015, 23:56 and downloads all the CSS files. Now, since Apache sends last modified time in response header so client will certainly get Last-Modified:Fri, 25 Sep 2015 23:55:00 GMT and since you are using ExpiresByType directive with "modification" base, so Apache will calculate its expiration time as --> [last modified time (time when resource was last modified in server, in this case it will be 25 Sep 2015 23:55) + time specified with "modification" base (in this case, 1 day)], so the expiration date will be calculated as 26 Sep 2015 23:55 and hence Apache will send Expires: Sat, 26 Sep 2015 23:55:00 GMT. So, the 2 cache related response headers Apache will send to client will be:

    Last-Modified:Fri, 25 Sep 2015 23:55:00 GMT
    Expires: Sat, 26 Sep 2015 23:55:00 GMT
    

  • Now, lets say client again visits the page on 26 Sep 2015 03:55 then it will see that document is not expired and hence will use cache version of the document. This will go as per your anticipation.

  • Now, lets say client again visits the page on 26 Sep 2015 23:56 then it will see that document has expired and hence will send a request to server for new document.

So, now client will send a request to server everytime it finds that document has expired. When you do a fresh upload then Apache will recalculate the expiration time and will send the same in Expires response header.

You can have a look at answer of this question, it is very good.

For the resources which you do not anticipate to be modified often, you can continue using "access" as base, like you are already correctly doing for Favicon which cannot be anticipated to change in a year.


Unfortunately Apache cannot fully solve this purpose - "Get fresh document from server only if modified version is available on server else continue using cached document" because until a request is sent to server, client cannot truly know whether document has changed or not. Or PUSH technology needs to be implemented where server pushes some information and client acts based on it.

Some other info:
HTML5 has provided a feature called app-cache/offline-cache which enables offline navigation but it is regarded as broken and will be dis-continued. You can look Service worker if you want to looking to implement user offline experience.

这篇关于如何设置我的网站缓存,但仍确保用户拥有最新版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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