标签:
上面的图解释了 Last-Modified, If-Modified-Since, Cache-Control,以及ETag 头域的用法。
在Yii中,可以在控制器中配置行为来生成这些Last-Modified, ETag, Cache-Control。方法是在behaviors中指定HttpCache类,并设定header的函数。
public function behaviors() { return [ [ ‘class‘ => ‘yii\filters\HttpCache‘, ‘only‘ => [‘view‘], ‘etagSeed‘ => function ($action, $params) { $post = $this->findModel(\Yii::$app->request->get(‘id‘)); return serialize([$post->title, $post->content]); }, ], ]; }
public function behaviors() { return [ [ ‘class‘ => ‘yii\filters\HttpCache‘, ‘only‘ => [‘view‘], ‘cacheControlHeader‘ => function ($action, $params) { return ‘public, max-age=3600‘; }, ], ]; }
public function behaviors() { return [ [ ‘class‘ => ‘yii\filters\HttpCache‘, ‘only‘ => [‘index‘], ‘lastModified‘ => function ($action, $params) { $q = new \yii\db\Query(); return $q->from(‘post‘)->max(‘updated_at‘); }, ], ]; }
标签:
原文地址:http://www.cnblogs.com/restart/p/4438190.html