Introduction to some advanced usage of caching in the YII framework of PHP, PHPYII framework Cache _php Tutorial

Source: Internet
Author: User

Introduction to some advanced usage of caching in PHP's YII framework, PHPYII framework Cache


Page Caching
Page caching refers to caching the contents of the entire page on the server side. Then, when the same page is requested, the content is removed from the cache instead of regenerated.

Page caching is supported by the Yii\filters\pagecache class, which is a filter. It can be used in the Controller class like this:

Public Function behaviors () {return [  '   class ' = ' Yii\filters\pagecache ',   ' only ' = ' = ' index ', '   duration ' and '   variations ' = [    \yii:: $app->language,   ],   ' dependency ' = [    ' Class ' = ' yii\caching\dbdependency ',    ' sql ' = = ' SELECT COUNT (*) from post ',   ],  ],];}

The code above indicates that the page cache is enabled only when the index operation, the page content is cached for up to 60 seconds, and changes as the language of the current app changes. If the total number of articles changes, the cached pages are invalidated.

As you can see, the page cache is very similar to the fragment cache. They all support the Duration,dependencies,variations and enabled configuration options. The main difference is that the page cache is implemented by a filter, while the fragment cache is a widget.

You can use the fragment cache and dynamic content while using the page cache.

HTTP Caching

In addition to server-side caching, Web apps can leverage the client cache to save the generation and transfer time of the same page content.

By configuring the Yii\filters\httpcache filter, the controller operation renders the content to be slow to exist on the client. The Yii\filters\httpcache filter only takes effect on GET and HEAD requests, and it can set up three cache-related HTTP headers for these requests.

    • Yii\filters\httpcache::lastmodified
    • Yii\filters\httpcache::etagseed
    • Yii\filters\httpcache::cachecontrolheader

Last-modified Head

The last-modified header uses timestamps to indicate whether the page has been modified since the last client cache.

Sends the last-modified header to the client by configuring the Yii\filters\httpcache::lastmodified property. The value of this property should be the PHP callable type, which returns the Unix timestamp when the page was modified. The parameters and return values of the callable should be as follows:

/** * @param action $action the currently processed action object * @param array $params The value of the params property * @return The Unix timestamp when the integer page was 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 enabled only when the index operation. It generates a last-modified HTTP header based on the last modified time of the page. When the browser accesses the index page for the first time, the server generates the page and sends it to the client browser. After the client browser accesses the page while the page is not modified, the server will not regenerate the page, and the browser will use the content previously cached by the client. As a result, service-side rendering and content transfer are eliminated.

ETag Header

Entity tag, or ETAG, uses a hash value to represent the content of the page. If the page has been modified, the hash value will change as well. By comparing the hash value of the client with the hash value generated by the server side, the browser can determine whether the page has been modified, and then decide whether the content should be retransmitted.

Sends an ETAG header to the client by configuring the Yii\filters\httpcache::etagseed property. The value of this property should be the PHP callable type, which returns a seed character used to generate the ETAG hash value. The parameters and return values of the callable should be as follows:

/** * @param action $action The Action object currently being processed * @param array $params The value of the params property * @return A string of seed characters used to generate the ETag hash value */function ($action, $params)

The following is an example of using an ETAG header:

Public Function behaviors () {return [  '   class ' = ' Yii\filters\httpcache ', ' Only ' and ' [   ' view '],   ' Etagseed ' = function ($action, $params) {    $post = $this->findmodel (\yii:: $app->request->get (' id '));    return serialize ([$post->title, $post->content]);   },  ],];}

The code above indicates that the HTTP cache is enabled only when the view operation is in use. It generates an ETAG HTTP header based on the title and content of the user request. When the browser accesses the view page for the first time, the server generates the page and sends it to the client browser. After the client browser title and content has not been modified during access to the page, the server will not regenerate the page, and the browser will use the content previously cached by the client. As a result, service-side rendering and content transfer are eliminated.

The ETAG enables more complex and accurate caching strategies than last-modified. For example, the ETAG can be invalidated when the site switches to another topic.

A complex etag generation seed may violate the original intent of using httpcache and cause unnecessary performance overhead because the etag needs to be recalculated in response to each request. Try to find the simplest expression to trigger an Etag failure.

Note: In order to comply with RFC 7232 (HTTP 1.1 protocol), if both the ETag and last-modified headers are configured, HttpCache will send them at the same time. And if the client sends both the If-none-match header and the if-modified-since header, only the former will be accepted.
Cache-control Head

The Cache-control header specifies the general caching policy for the page. You can send the appropriate header information by configuring the Yii\filters\httpcache::cachecontrolheader property. The following headers are sent by default:

Cache-control:public, max-age=3600

Session cache Limiter

When the page makes the session, PHP will follow PHP. The Session.cache_limiter value set in INI automatically sends some cache-related HTTP headers. These HTTP headers may interfere with the httpcache you originally set or invalidate them. To avoid this problem, HttpCache disables the automatic sending of these headers by default. To change this behavior, you can configure the Yii\filters\httpcache::sessioncachelimiter property. This property accepts a string value, including Public,private,private_no_expire, and NoCache. Please refer to the cache limiter in the PHP manual for the meanings of these values.

SEO Impact

Search engines tend to follow the site's cache header. Because some crawler crawl frequency limit, enable the cache header can reduce the number of repeated requests, increase crawler efficiency (translator: the main idea, but the search engine ranking rules do not understand, a good caching strategy should be able to add points for the user experience).

Articles you may be interested in:

    • A detailed description of the use of the front-end resource bundle in PHP's YII framework
    • In-depth parsing of PHP's caching capabilities in the YII framework
    • The use of view view in the Yii framework of PHP is advanced
    • A detailed approach to creating views and rendering views in the PHP yii framework
    • A tutorial on model models in the YII framework of PHP
    • Controller controllers in the YII framework of PHP
    • The method of removing the binding behavior of a component in PHP's YII framework
    • The definition and binding methods of behavior in the YII framework of PHP
    • In-depth explanation of properties in the Yii framework of PHP
    • A detailed description of the installation and use of PHP's YII framework Extension

http://www.bkjia.com/PHPjc/1117068.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117068.html techarticle introducing some advanced uses of caching in the YII framework of PHP, the PHPYII Framework cache page Cache page cache refers to caching the contents of the entire page on the server side. Then when the same page is requested ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.