This article mainly introduces some advanced cache usage in PHP Yii Framework, including page cache and session cache limiters. For more information, see
Page cache
Page caching refers to caching the content of the entire page on the server side. When the same page is requested, the content will be retrieved from the cache instead of being regenerated.
The page cache is supported by the yii \ filters \ PageCache class, which is a filter. It can be used in the controller class as follows:
public function behaviors(){ return [ [ 'class' => 'yii\filters\PageCache', 'only' => ['index'], 'duration' => 60, 'variations' => [ \Yii::$app->language, ], 'dependency' => [ 'class' => 'yii\caching\DbDependency', 'sql' => 'SELECT COUNT(*) FROM post', ], ], ];}
The code above indicates that the page cache is only enabled when the index operation is performed. the page content is cached for up to 60 seconds and will change with the current language change. If the total number of articles changes, the cached page will become invalid.
As you can see, the page cache and fragment cache are extremely similar. They all support the duration, dependencies, variations, and enabled configuration options. The main difference between them is that the page cache is implemented by the filter, while the fragment cache is a small part.
You can use the fragment cache and dynamic content while using the page cache.
HTTP cache
In addition to the server cache, Web applications can also use the client cache to save the generation and transmission time of the same page content.
By configuring the yii \ filters \ HttpCache filter, the controller can cache the rendered content on the client. The yii \ filters \ HttpCache filter takes effect only for GET and HEAD requests. it can set three cache-related HTTP headers for these requests.
- Yii \ filters \ HttpCache: lastModified
- Yii \ filters \ HttpCache: etagSeed
- Yii \ filters \ HttpCache: cacheControlHeader
Last-Modified header
The Last-Modified header uses a timestamp to indicate whether the page has been Modified since the Last client cache.
Send the Last-Modified header to the client by configuring the yii \ filters \ HttpCache: lastModified attribute. The value of this attribute should be of the PHP callable type, and the Unix timestamp at page modification is returned. The callable parameters and return values should be as follows:
/*** @ Param Action $ action the operation object currently being processed * @ param array $ value of the params "params" attribute * @ return Unix Timestamp when the integer page is modified */function ($ action, $ params)
The following is an example of using the Last-Modified header:
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'); }, ], ];}
The above code indicates that the HTTP cache is only enabled during the index operation. It generates a Last-Modified HTTP header based on the Last modification time of the page. When the browser visits the index page for the first time, the server will generate a page and send it to the client browser. After that, the client browser accesses the page when the page is not modified, the server will not generate the page again, and the browser will use the content cached by the client. Therefore, server rendering and content transmission will be omitted.
ETag header
The Entity Tag (ETag) uses a hash value to represent the page content. If the page is modified, the hash value also changes. By comparing the hash value of the client and the hash value generated by the server, the browser can determine whether the page has been modified and decide whether to re-transmit the content.
You can configure yii \ filters \ HttpCache: etagSeed to send the ETag header to the client. The value of this attribute should be of the PHP callable type, and a seed character is returned to generate the ETag hash value. The callable parameters and return values should be as follows:
/*** @ Param Action $ action the operation object currently being processed * @ param array $ value of the params "params" attribute * @ return string a seed character to generate the ETag hash value */ function ($ action, $ params)
The following is an example of using the ETag 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]); }, ], ];}
The above code indicates that the HTTP cache is only enabled during the view operation. It generates an ETag HTTP header based on the title and content of the user request. When the browser visits the view page for the first time, the server will generate a page and send it to the client browser. After the client browser title and content are not modified, the server will not regenerate the page and the browser will use the content cached by the client. Therefore, server rendering and content transmission will be omitted.
Compared with Last-Modified, ETag can implement more complex and precise cache policies. For example, ETag can be invalidated when the site switches to another topic.
Complex Etag generation seed may result in unnecessary performance overhead because every request must be recalculated. Try to find a simple expression to trigger Etag failure.
Note: To comply with RFC 7232 (HTTP 1.1 Protocol), if the ETag and Last-Modified headers are configured at the same time, HttpCache will send them at the same time. If the client sends both the If-None-Match header and If-Modified-Since header, only the former will be accepted.
Cache-Control header
The Cache-Control header specifies the regular page Cache policy. You can configure yii \ filters \ HttpCache: cacheControlHeader to send the corresponding header information. The following headers are sent by default:
Cache-Control: public, max-age=3600
Session cache limiters
When session is enabled on the page, PHP will automatically send cache-related HTTP headers according to the session. cache_limiter value set in PHP. INI. These HTTP headers may interfere with the HttpCache you set or invalidate it. To avoid this problem, HttpCache disables automatic sending of these headers by default. To change this line, you can configure the yii \ filters \ HttpCache: sessionCacheLimiter attribute. This property accepts a string value, including public, private, private_no_expire, and nocache. Refer to the cache limiters in the PHP Manual to understand the meaning of these values.
SEO impact
Search engines tend to follow the cache headers of websites. Because the crawling frequency of some crawlers is limited, enabling the cache header can reduce the number of repeated requests and increase the crawling efficiency, A good cache policy should be able to add extra points for the user experience ).