简体   繁体   English

如何在PHP中实现此缓存控制策略?

[英]How to implement this cache control policy in PHP?

Serve the new content only if the file has changed since last visit. 仅当文件自上次访问以来已更改时,才提供新内容。

How do I implement that? 我该如何实现?

UPDATE UPDATE

Sorry not to mention it earlier,but the requested resource is web page which is requested directly,not an image. 抱歉,前面没有提及,但是请求的资源是直接请求的网页,而不是图像。

You can use a trick borrowed from rails and append the last file modification time to the include: 您可以使用从rails借来的技巧,并将最后文件修改时间附加到include:

$fileName = 'image.jpg';
$httpLink = $fileName . '?' . filemtime( $fileName );
echo '<img src="', $fileName, '" alt="blah" />';

This will output a link like 这将输出一个链接,例如

<img src="image.jpg?1002412" alt="blah" />   

Then when the file changes, the query string will also change and the browser will request the "new" file ie 然后,当文件更改时,查询字符串也将更改,浏览器将请求“新”文件,即

<img src="image.jpg?1003622" alt="blah" />

Alternatively you could keep a local log of file revisions and read the revision number from a database rather than the filesystem, which may be marginally faster (and save filesystem reads, although it's not significant difference - dependant on db vs web server load). 或者,您可以保留文件修订的本地日志,并从数据库而不是文件系统中读取修订号,这可能会稍快一些(并保存文件系统读取,尽管差别不大-取决于数据库负载与Web服务器负载)。

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

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