利用浏览器缓存和CakePHP缓存、浏览器、CakePHP

2023-09-02 09:38:21 作者:清辞

我问我的主人,如果他们支持mod_expires与否,他们告诉我,这是支持的。我使用CakePHP,我尝试了以下code。在 Web根目录/的.htaccess 和`APP_ROOT / htaccess的,但谷歌的网页速度是阶梯考虑到有问题的CSS缓存,JS,PNG,JPG,等它发现他们24小时! 为下面的屏幕截图显示了的

 < FilesMatch(ICO | PDF | FLV | JPG | JPEG | PNG | GIF | JS | CSS | SWF)$>
页眉设置缓存控制最大年龄= 31449600,公开
< / FilesMatch>
 

以上code是从这篇文章。

另外我用下面的:

 < IfModule mod_expires.c>
 ExpiresActive在
 ExpiresByType图像/ JPG的访问权限1年
 ExpiresByType为image / jpeg的访问权限1年
 ExpiresByType图像/ GIF的访问权限1年
 ExpiresByType图像/ PNG的访问权限1年
 ExpiresByType文本/ CSS的访问权限1年
 ExpiresByType text / html的访问权限1月
#ExpiresByType应用程序/ PDF的访问权限1月
 ExpiresByType文/ X-的javascript访问权限1年
 ExpiresByType文/ JavaScript的访问权限1年
 ExpiresByType应用程序/ JavaScript的访问权限1年
#ExpiresByType应用程序/ x-冲击波闪光的访问权限1月
#ExpiresByType图像/ X-图标的访问权限1年
 ExpiresDefault接入1年
< / IfModule>
 

此外,我试图用两个codeS彼此,但谷歌的速度告诉我的一样。请看下面的屏幕截图:

我需要知道我怎么能解决这个问题呢?!

  

编辑##

     

在Firefox使用萤火虫,我注意到,在应用程序/视图/主题/平板/ Webroot公司只有资源没有受到影响与缓存   以上算是设置,而是直接在应用程序/ Web根目录的发现,   即无,主题是影响很好。

解决方案

这似乎是在CakePHP中的错误。在CakePHP 1.3+的主题视图资产已经从应用程序/ Web根目录/主题/ themeTitle 应用程序/视图/主题/ themeTitle / Webroot公司

根据 CakePHP的1.3文件< /一>您可以通过创建恢复为主题的静态资产的老地方(即在主Web根目录)应用程序/ Web根目录/主题/ themeTitle

  

链接到静态的资产为1.2略有不同。你仍然可以   使用现有的应用程序/ Web根目录/主题,并直接链接到那些静态   文件。应当指出的是,你将需要使用的完整路径   在app / webroot的/主题的链接资产。如果你想保持你的主题   资产内的应用程序/ webroot的,建议您重命名   应用程序/ Web根目录/主题为应用程序/ Web根目录/主题。这将允许你   利用核心助手路径的发现。以及保持性能   通过PHP不提供资产收益。

所以,我已经做了解决这个问题是要复制在应用程序/视图/主题/平板/ Webroot公司所有文件应用程序/ Webroot公司/主题/石板

下面的屏幕截图是从Firefox的Firebug,显示图像文件获得在几秒钟内一年后到期:

IE浏览器视频缓存文件在哪 ie浏览器视频缓存位置

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: