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

时间:2016-05-13 09:13:11

标签: php google-app-engine yii2

我从谷歌得到了下一个通知:

 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)

为解决此问题,我找到了下一个行为:

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
//            }
      ],
    ];
  }

文档之后(不是针对所有请求,仅针对 / )会出现另外三个标题:

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

尽管设置时间()+ 3600 上次修改等于数据标题

如何为所有请求设置不仅仅为/ 据我所知谷歌想要我设置Expire标题?

1 个答案:

答案 0 :(得分:2)

过期进入网络文件夹中的.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>

此外,在yii2中,您可以设置版本(查询字符串):

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

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

版本或上次修改?

这里有深入解释File Caching: Query string vs Last-Modified?