简体   繁体   English

PHP缓存控制的概念

[英]concept of PHP Cache Control

I have read about article about PHP Cache Control. 我读过有关PHP缓存控制的文章。

They told about the concept and coding, but I still can't get the idea of : 他们讲述了概念和编码,但我仍然无法理解:

Where to put those code ? 在哪里放这些代码? put it at all page ? 把它放在所有页面? and at the very beginning of my php file ? 在我的PHP文件的最开始? I wonder browser will cache the whole HTML code ? 我想知道浏览器会缓存整个HTML代码吗?

I saw lots for example talks about Cache is using on image file, css file. 我看到很多关于Cache正在使用图像文件,css文件的例子。 But if i want to add header on image file, does it mean I need to do a url rewrite to guide the image file request into a php file first ? 但是,如果我想在图像文件上添加标题,这是否意味着我需要做一个url重写来首先将图像文件请求引导到php文件中?

I'm assuming you mean cache regarding the browser cache. 我假设你的意思是关于浏览器缓存的缓存。 If so, what you need to know is that browsers keep tracking of every file you download. 如果是这样,您需要知道的是浏览器会跟踪您下载的每个文件。 Once you visit a page for a second time, your browser first check if the file you're trying to download (ie, an image or CSS file) is already on your computer and hasn't been edited on the website in the time between your first and second visit. 一旦您第二次访问某个页面,您的浏览器会首先检查您尝试下载的文件(即图像或CSS文件)是否已在您的计算机上,并且尚未在网站上进行过编辑第一次和第二次访问。

If you want to achieve a browser cache, you could use some HTTP headers in order to control how the user's browser will perform the validation and cache of the given files. 如果要实现浏览器缓存,可以使用一些HTTP标头来控制用户浏览器如何执行给定文件的验证和缓存。 You could use something like this: 你可以使用这样的东西:

header("Expires: Mon, 1 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache"); 

This will force the browser to NOT cache the file. 这将强制浏览器不缓存文件。 I'm showing an opposite version of what you achieve to understand why the browser behaves given a file with certain headers. 我正在展示您所获得的相反版本,以了解为什么浏览器在给定具有特定标头的文件时的行为。 The first line will tell the browser that the file you're currently visit has expired already by putting an older date. 第一行将通过设置较旧的日期告诉浏览器您当前访问的文件已经过期。 If you change the date to one in the future, you'll get the opposite functionality. 如果您将来将日期更改为一个,您将获得相反的功能。

Next, the second line allows the script to tell the browser when the file the user's trying to access was edited for the last time. 接下来,第二行允许脚本告诉浏览器上次编辑用户尝试访问的文件的时间。 If the last modified date from your file on the web is older than the one on your computer (from the file you downloaded in your first visit), then your browser will download the file again, assuming it has changed since your last visit. 如果您在网络上的文件中的最后修改日期早于您计算机上的文件(来自您第一次访问时下载的文件),那么您的浏览器将再次下载该文件,假设自上次访问后该文件已更改。

The third line helps explain some browsers (and also to some proxies) how they will behave when they download the file. 第三行有助于解释一些浏览器(以及某些代理)在下载文件时的行为方式。 There's different options here, you can see all of them here . 这里有不同的选择,你可以看到他们都在这里

And the last one is kind of similar to the previous one, but for older systems. 最后一个类似于前一个,但对于旧系统。 Here's a proper explanation . 这是一个正确的解释

Otherwise, there's also another option but nothing has to do with PHP: you can configure your server application to handle the cache control headers for you (if you're using PHP I'm assuming Apache or Nginx) so you don't need to rewrite everything or pass it to a PHP file either. 否则,还有另一种选择,但与PHP没有任何关系:您可以配置您的服务器应用程序来为您处理缓存控制标头(如果您使用PHP我假设Apache或Nginx)所以您不需要重写所有内容或将其传递给PHP文件。

If you're using Apache, you could use something like this in a .htaccess file: 如果你正在使用Apache,你可以在.htaccess文件中使用这样的东西:

# 480 weeks 
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> 
Header set Cache-Control "max-age=290304000, public" 
</FilesMatch>   

Or, if you're using Nginx, in the part of the configuration who handles your site you could add: 或者,如果您使用的是Nginx,则在处理您网站的配置部分中,您可以添加:

location ~* \.(css|js|gif|jpe?g|png)$ { 
expires 168h; 
add_header Pragma public; 
add_header Cache-Control "public, must-revalidate, proxy-revalidate"; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM