問(wèn)題描述
我認(rèn)為我的問(wèn)題似乎很隨意,但請(qǐng)耐心等待,因?yàn)樗兊糜腥?至少對(duì)我而言:)).
I think my question seems pretty casual but bear with me as it gets interesting (at least for me :)).
考慮一個(gè) PHP 頁(yè)面,它的目的是從文件系統(tǒng)讀取請(qǐng)求的文件并將其作為響應(yīng)回顯.現(xiàn)在的問(wèn)題是如何為這個(gè)頁(yè)面啟用緩存?需要指出的是,文件可能非常龐大,啟用緩存是為了避免客戶端一次又一次地下載相同的內(nèi)容.
Consider a PHP page that its purpose is to read a requested file from filesystem and echo it as the response. Now the question is how to enable cache for this page? The thing to point out is that the files can be pretty huge and enabling the cache is to save the client from downloading the same content again and again.
理想的策略是使用If-None-Match"請(qǐng)求頭和ETag"響應(yīng)頭來(lái)實(shí)現(xiàn)反向代理緩存系統(tǒng).盡管我知道這么多,但我不確定這是否可行,或者我應(yīng)該返回什么作為響應(yīng)才能實(shí)現(xiàn)這種技術(shù)!
The ideal strategy would be using the "If-None-Match" request header and "ETag" response header in order to implement a reverse proxy cache system. Even though I know this far, I'm not sure if this is possible or what should I return as response in order to implement this technique!
推薦答案
使用 PHP 處理大量或許多輔助文件并不是它的真正目的.
Serving huge or many auxiliary files with PHP is not exactly what it's made for.
相反,對(duì)于 nginx,請(qǐng)查看 X-accel,X-Sendfile 用于 Lighttpd 或 mod_xsendfile 用于 Apache.
Instead, look at X-accel for nginx, X-Sendfile for Lighttpd or mod_xsendfile for Apache.
初始請(qǐng)求由 PHP 處理,但一旦確定下載文件,它會(huì)設(shè)置一些標(biāo)頭以指示服務(wù)器應(yīng)處理文件發(fā)送,之后 PHP 進(jìn)程將被釋放以提供其他服務(wù).
The initial request gets handled by PHP, but once the download file has been determined it sets a few headers to indicate that the server should handle the file sending, after which the PHP process is freed up to serve something else.
然后您可以使用 Web 服務(wù)器為您配置緩存.
You can then use the web server to configure the caching for you.
靜態(tài)生成的內(nèi)容
如果您的內(nèi)容是由 PHP 生成的并且創(chuàng)建起來(lái)特別昂貴,您可以將輸出寫(xiě)入本地文件并再次應(yīng)用上述方法.
If your content is generated from PHP and particularly expensive to create, you could write the output to a local file and apply the above method again.
如果您不能寫(xiě)入本地文件或不想寫(xiě)入,您可以使用 HTTP 響應(yīng)頭來(lái)控制緩存:
If you can't write to a local file or don't want to, you can use HTTP response headers to control caching:
Expires: <absolute date in the future>
Cache-Control: public, max-age=<relative time in seconds since request>
這將導(dǎo)致客戶端緩存頁(yè)面內(nèi)容,直到它過(guò)期或用戶強(qiáng)制重新加載頁(yè)面(例如按 F5).
This will cause clients to cache the page contents until it expires or when a user forces a page reload (e.g. press F5).
動(dòng)態(tài)生成的內(nèi)容
對(duì)于動(dòng)態(tài)內(nèi)容,您希望瀏覽器每次都 ping 您,但僅在有新內(nèi)容時(shí)才發(fā)送頁(yè)面內(nèi)容.您可以通過(guò)設(shè)置一些其他響應(yīng)標(biāo)頭來(lái)實(shí)現(xiàn)此目的:
For dynamic content you want the browser to ping you every time, but only send the page contents if there's something new. You can accomplish this by setting a few other response headers:
ETag: <hash of the contents>
Last-Modified: <absolute date of last contents change>
當(dāng)瀏覽器再次ping你的腳本時(shí),它們會(huì)分別添加以下請(qǐng)求頭:
When the browser pings your script again, they will add the following request headers respectively:
If-None-Match: <hash of the contents that you sent last time>
If-Modified-Since: <absolute date of last contents change>
ETag
主要用于減少網(wǎng)絡(luò)流量,因?yàn)樵谀承┣闆r下,要知道內(nèi)容哈希,您首先必須計(jì)算它.
The ETag
is mostly used to reduce network traffic as in some cases, to know the contents hash, you first have to calculate it.
如果您有本地文件緩存(文件有修改日期),Last-Modified
是最容易應(yīng)用的.一個(gè)簡(jiǎn)單的條件使它起作用:
The Last-Modified
is the easiest to apply if you have local file caches (files have a modification date). A simple condition makes it work:
if (!file_exists('cache.txt') ||
filemtime('cache.txt') > strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
// update cache file and send back contents as usual (+ cache headers)
} else {
header('HTTP/1.0 304 Not modified');
}
如果你不能做文件緩存,你仍然可以使用ETag
來(lái)判斷內(nèi)容是否同時(shí)發(fā)生了變化.
If you can't do file caches, you can still use ETag
to determine whether the contents have changed meanwhile.
這篇關(guān)于緩存由 PHP 動(dòng)態(tài)創(chuàng)建的 HTTP 響應(yīng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!