利用浏览器缓存和 CakePHP [英] Leverage Browser Caching and CakePHP

查看:28
本文介绍了利用浏览器缓存和 CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问我的主人他们是否支持 mod_expires,他们告诉我支持.我使用 CakePHP 并在 webroot/.htaccess 和 `app_root/.htaccess 中尝试了以下代码,但考虑到 css、js、png、jpg 等缓存存在问题,Google 的页面速度很慢.它找到了他们 24 小时!如下图所示

<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">标头集缓存控制max-age=31449600, public"</FilesMatch>

以上代码摘自.在 cakePHP 1.3+ 中,主题视图资产已从 app/webroot/themed/themeTitle 移动到 app/views/themed/themeTitle/webroot.

根据 CakePHP 1.3 文档 您可以通过创建 app/webroot/theme/themeTitle 来恢复主题静态资​​产的旧位置(即在主 webroot 中)

<块引用>

链接到静态资产与 1.2 略有不同.你还可以使用现有的 app/webroot/themed 并直接链接到那些静态文件.应该注意的是,您需要使用完整路径链接到 app/webroot/themed 中的资产.如果你想保留你的主题在 app/webroot 中的 assets 建议您 重命名app/webroot/themed 到 app/webroot/theme.这将使您能够利用核心帮助程序路径查找.以及保持性能不通过 PHP 提供资产的好处.

所以,我为解决这个问题所做的是将 app/views/themed/slate/webroot 中的所有文件复制到 app/webroot/theme/slate

以下屏幕截图来自 FireFox Firebug,显示图像文件在一年后以秒为单位过期:

I asked my host if they supports mod_expires or not, they told me that it is supported. I use CakePHP and I tried the following code in webroot/.htaccess and `app_root/.htaccess but Google's page speed is stile considering there is issue in caching of css, js, png, jpg, etc. it found them 24 hour! as the screenshot shows below

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

The above code is adopted from this article.

Also I used the following:

<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType image/jpg "access 1 year"
 ExpiresByType image/jpeg "access 1 year"
 ExpiresByType image/gif "access 1 year"
 ExpiresByType image/png "access 1 year"
 ExpiresByType text/css "access 1 year"
 ExpiresByType text/html "access 1 month"
# ExpiresByType application/pdf "access 1 month"
 ExpiresByType text/x-javascript "access 1 year"
 ExpiresByType text/javascript "access 1 year"
 ExpiresByType application/javascript "access 1 year"
# ExpiresByType application/x-shockwave-flash "access 1 month"
# ExpiresByType image/x-icon "access 1 year"
 ExpiresDefault "access 1 year"
</IfModule>

Also I tried to use both codes with each other but the Google Speed tell me the same. Look at the following screen shot:

I need to know how could I solve this issue?!!

Edit##

Using firebug in Firefox I noticed that only resources in the app/views/themed/slate/webroot are not be affected with the cache settings regarded above, but those found directly in app/webroot , i.e without, theming are affected well.

解决方案

It seems to be a bug in cakephp. In cakePHP 1.3+ the themed view assets have been moved from app/webroot/themed/themeTitle to app/views/themed/themeTitle/webroot.

According to CakePHP 1.3 documentation you are able to restore the old place (i.e. in the main webroot) of the themed static assets by creating app/webroot/theme/themeTitle

Linking to static assets is slightly different from 1.2. You can still use the existing app/webroot/themed and directly link to those static files. It should be noted that you will need to use the full path to link to assets in app/webroot/themed. If you want to keep your theme assets inside app/webroot it is recommended that you rename app/webroot/themed to app/webroot/theme. This will allow you to leverage the core helper path finding. As well as keep the performance benefits of not serving assets through PHP.

So, what I have done for solve this issue is to copying all files in app/views/themed/slate/webroot to app/webroot/theme/slate

The following screen shot is from FireFox Firebug that shows an image file get the expire after one year in seconds:

这篇关于利用浏览器缓存和 CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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