簡體   English   中英

如何為yii2中的所有請求啟用緩存?

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

我從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)

為了解決此問題,我發現了下一個行為:

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標頭?

到期進入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>

另外,在yii2中,您可以設置版本(查詢字符串):

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

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

版本或最后修改?

這是一個深入的解釋文件緩存:查詢字符串與最后修改日期?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM