使用缓存的问题" AddHandler的应用程序/ X的httpd - PHP的" [英] Caching problem using "AddHandler application/x-httpd-php"

查看:275
本文介绍了使用缓存的问题" AddHandler的应用程序/ X的httpd - PHP的"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的.htaccess,我为了设置PHP处理我所有的.css和,JS输出用户代理基于code:

Using .htaccess, I'm setting PHP handler to all my .css and ,js in order to output user-agent based code:

 AddHandler application/x-httpd-php .css .js

例如:

<?PHP if ($CurrentBrowser == 'msie') { ?>
.bind('selectstart', function(event) { ... })
<?PHP } ?>

所以,其实,是动态创建我的code文件,但可以考虑静态文件。这是因为,一旦他们被编为第一次,浏览器可以让他们从缓存中回和重用他们,直到我改变自己的内容。 这就是为什么我使用指纹/版本和很长一段时间届满他们:

So, in fact, my code files are dynamically created but can be considered static files. That's because, once they have been compiled for the first time, browsers can get them back from cache and reuse them until I change their content. That's why I'm using fingerprinting/versioning and long time expiration on them:

[INDEX.PHP]
<script type="application/javascript" src="<?PHP echo GetVersionedFile('/script.js'); ?>"></script>
<script type="application/javascript" src="/script.1316108341.js"></script>

[.HTACCESS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^(.+)\.(\d+)\.(css|js)$" $1.$3 [L]

问题在于这些文件,即使我给他们一个适当的标题,永远不会被缓存通过任何浏览器(我从来没有得到一个304 code,总200)。这是一个日志我的服务器响应:

The problem is that those files, even if I send them with a proper header, are never being cached by any browser (I never get a 304 code, always 200). This is a log of my server responses:

[CHROME]
Request URL:http://127.0.0.1:8888/script.1316108341.js
Request Method:GET
Status Code:200 OK
-----
Cache-Control:max-age=31536000, public
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:6150
Content-Type:application/javascript
Date:Thu, 15 Sep 2011 21:41:25 GMT
Expires:Fri, 14 Sep 2012 21:41:25 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.17 (Win32) PHP/5.3.6
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.6

[MOZILLA]
Request URL:http://127.0.0.1:8888/script.1316108341.js
Request Method:GET
Status Code:200 OK
-----
Date Thu, 15 Sep 2011 21:43:26 GMT
Server Apache/2.2.17 (Win32) PHP/5.3.6
X-Powered-By PHP/5.3.6
Content-Encoding gzip
Vary Accept-Encoding
Cache-Control max-age=31536000, public
Expires Fri, 14 Sep 2012 21:43:26 GMT
Content-Type application/javascript
Content-Length 6335
Keep-Alive timeout=5, max=100
Connection Keep-Alive
-----
Last Modified Thu Sep 15 2011 23:43:26 GMT+0200 (= time i loaded the page) (???)
Last Fetched Thu Sep 15 2011 23:43:26 GMT+0200 (= time i loaded the page) (???)
Expires Fri Sep 14 2012 23:43:26 GMT+0200
Data Size 6335
Fetch Count 10
Device disk

可能是什么问题?我怎么能强制缓存这些文件? 很多很多的感谢!

What could be the problem? How can I force caching on these files? Many, many thanks!

推荐答案

由于PHP和CSS文件的请求被PHP处理,你的PHP code,其条件语句被每次执行。

Since the requests for PHP and CSS files are being handled by PHP, your PHP code with its conditionals is being executed each time.

阿帕奇/ PHP不知道如果内容缓存,还是应进行再生,因此执行c各自时候你的PHP $ C $。

Apache/PHP have no idea if the content is cacheable or if it should be regenerated so it executes your PHP code each time.

如果您发送的最后一个修改后的头,或使用你的版本/指纹识别方法,那么它是在你的PHP脚本,你有责任检查指纹或版本,并确定它是否仍然有效。如果是这样,那么你就可以发送304未修改头和终止任何进一步的处理。你也可以检查请求标头一个Last-Modified标记和使用该方法。

If you send the last modified header, or use your versioning/fingerprinting method, then it is your responsibility in your PHP script to check the fingerprint or version and determine if it is still valid. If so, then you can send a 304 Not Modified header and terminate any further processing. You can also check the request headers for a Last-Modified tag and use that method.

另一种方法是将缓存的各种浏览器和日期响应文件,这样你就可以成为该文件是针对首次用户,而不是用PHP进行再生。然后,您可以检查该文件的修改时间,以确定是否可以发送304头。

Another approach would be to cache the response for various browsers and dates to a file so you can serve that file up for first time users rather than regenerating it with php. Then you can check the modification time of that file to determine if you can send a 304 header.

介绍如何使用PHP缓存的几种方法这 SitePoint文章。希望有所帮助。

This SitePoint article explains several methods of using PHP to cache. Hope that helps.

这篇关于使用缓存的问题&QUOT; AddHandler的应用程序/ X的httpd - PHP的&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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