简体   繁体   English

如何为yii2中的所有请求启用缓存?

[英]How to enable caching for all requests in yii2?

I got from google the next notice: 我从Google得到了下一条通知:

 example.com/assets/a3f4f38d/jquery.min.js (expiration not specified) example.com/css/main.css (expiration not specified) 
example.com/css/swiper.min.css (expiration not specified) 

example.com/img/clouds/cloud-portfolio_mob.png (не указан срок действия) example.com/img/clouds/min0.png (expiration not specified) 

example.com/img/clouds/min1.png (expiration not specified) example.com/img/clouds/min2.png (expiration not specified) 

example.com/img/heroes/A.jpg (expiration not specified) example.com/img/heroes/B.jpg (expiration not specified) 

example.com/img/heroes/C.jpg (expiration not specified) example.com/img/icons/Dot.gif (expiration not specified) example.com/img/icons/menuSm_min.png (expiration not specified) 

example.com/img/icons/red-heart-border_min.png (не указан срок действия) example.com/img/icons/red-heart_min.png (не указан срок действия) example.com/img/loader/Loader-Advanced.gif (не указан срок действия) 

example.com/img/logo/t.svg (expiration not specified) example.com/img/main_background/bg_mob.jpg (expiration not specified)

For resolve this problem, i found the next behavior: 为了解决此问题,我发现了下一个行为:

public function behaviors()
  {
    return [
      [
        'class' => 'yii\filters\HttpCache',
        'only' => ['index'],
        'lastModified' => function ($action, $params) {
//          $q = new \yii\db\Query();
          return time() + 3600;
//          return $q->from('users')->max('updated_at');
        },
        'sessionCacheLimiter' => 'public',

//            'etagSeed' => function ($action, $params) {
//                return // generate ETag seed here
//            }
      ],
    ];
  }

After this on main document (not for all requests, only for / ) appears three additional headers: 文档上之后(并非针对所有请求,仅针对/ ),出现了三个附加标题:

Cache-Control:public, max-age=3600  Last-Modified:Fri, 13 May 2016 09:03:45 GMT  Pragma:

Althought set time()+3600 , Last-Modified equals Data header 尽管设置了时间()+3600 ,但最后修改等于数据

How to set for all requests not for only / So as I understand google wants that I set Expire header? 如何为所有请求设置不只/因此,据我了解,谷歌希望我设置Expire标头?

Expire goes into .htaccess in web folder: 到期进入Web文件夹中的.htaccess:

<IfModule mod_expires.c>

ExpiresActive On 
ExpiresDefault "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/javascript "access plus 1 year"
</IfModule>

Also, in yii2 you could set the version (query string): 另外,在yii2中,您可以设置版本(查询字符串):

'components' => [
        'assetManager' => [
            'appendTimestamp' => true,
        ],
    ],

Info : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#cache-busting 信息http : //www.yiiframework.com/doc-2.0/guide-structure-assets.html#cache-busting

Versions or last-modified? 版本或最后修改?

Here's a in deep explanation : File Caching: Query string vs Last-Modified? 这是一个深入的解释文件缓存:查询字符串与最后修改日期?

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

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