简体   繁体   English

如何在Yii2中打开HTTP缓存?

[英]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 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. 我发现Yii没有输出所有标题的原因,但我仍然不知道如何打开缓存。 In fact, now it is actively turning caching off with Cache-Control: no-cache , even though I requested it to be on. 事实上,现在它正在积极地通过Cache-Control: no-cache关闭Cache-Control: no-cache ,即使我要求它启用。

Even with a test action, it sets no-cache . 即使使用测试操作,它也会设置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. 我发现调用session_cache_limiter手动让它输出Cache-Control ,但不是我设置的值。 This is probably a bug because sessionCacheLimiter specifically says that's what it's for. 这可能是一个错误,因为sessionCacheLimiter专门说明了它的用途。

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

Gives

Cache-Control: public, max-age=10800

And it still sets cookies when no session is used. 并且在没有使用会话时仍然设置cookie。 This prevents caching for the CDN we are using. 这可以防止我们正在使用的CDN的缓存。

Yii version 2.0.6. Yii版本2.0.6。

If you want to use \\yii\\filters\\HttpCache you should set at least lastModified or etagSeed : 如果你想使用\\yii\\filters\\HttpCache你应该至少设置lastModifiedetagSeed

[
    '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 看看这里: https//github.com/yiisoft/yii2/blob/2.0.6/framework/filters/HttpCache.php#L111

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

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