简体   繁体   中英

How do I turn on HTTP caching in Yii2?

I set these values in the controller, yet it's not working.

public function behaviors()
{
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
        'httpCache' => [
          'class' => 'yii\filters\HttpCache',
          'sessionCacheLimiter' => 'public',
          'cacheControlHeader' => 'public, max-age=3600',
        ],
    ];
}

http://www.yiiframework.com/doc-2.0/guide-caching-http.html#cache-control

$ curl -I http://localhost:81/xxxx/web/shopping/search?q=toaster
HTTP/1.1 200 OK
Date: Wed, 11 Nov 2015 08:58:57 GMT
Server: Apache/2.4.16 (Unix) OpenSSL/1.0.2d PHP/5.6.12
X-Powered-By: PHP/5.6.12
Set-Cookie: PHPSESSID=t07qapiiv7crdkva14ojn6cvg5; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: search=4ef489ad7fa4567884eebc22279836f85acec05395053c863ed86c2679be9477a%3A2%3A%7Bi%3A0%3Bs%3A6%3A%22search%22%3Bi%3A1%3Bs%3A38%3A%22%2Fxxxx%2Fweb%2Fshopping%2Fsearch%3Fq%3Dtoaster%22%3B%7D; path=/; httponly
Set-Cookie: _csrf=72e0104d312d81ddde455cff7566d3d186e3b25f8f41fc03a1f4a533d9b739ada%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22R1HklhizymwcXPVxJkBCvNR2gBwInqdw%22%3B%7D; path=/; httponly
Content-Type: text/html; charset=UTF-8

Since there were no answers to the question, I just updated the question. I found a cause for Yii not outputting all the headers, but I still don't know how to turn caching on. In fact, now it is actively turning caching off with Cache-Control: no-cache , even though I requested it to be on.

Even with a test action, it sets no-cache .

$ curl -I http://localhost:81/xxxx/web/shopping/test
HTTP/1.1 200 OK
Set-Cookie: PHPSESSID=bvdnd33uu8qj0s88q2sr7n7696; path=/; HttpOnly
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
[...]
php.ini
$ grep cache_limiter  /etc/php5/php.ini
session.cache_limiter = nocache

I discovered that calling session_cache_limiter manually got it to output Cache-Control , but not the value that I set. This is probably a bug because sessionCacheLimiter specifically says that's what it's for.

public function behaviors() {
    session_cache_limiter('public');

Gives

Cache-Control: public, max-age=10800

And it still sets cookies when no session is used. This prevents caching for the CDN we are using.

Yii version 2.0.6.

If you want to use \\yii\\filters\\HttpCache you should set at least lastModified or etagSeed :

[
    'class' => 'yii\filters\HttpCache',
    'lastModified' => function ($action, $params) {
        return time();
    },
    'sessionCacheLimiter' => 'public',
    //'cacheControlHeader' => 'public, max-age=3600', // not needed since it is the default value
],

Take a look here : https://github.com/yiisoft/yii2/blob/2.0.6/framework/filters/HttpCache.php#L111

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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