


Yii Framework Official Guide Series 30 - Caching: Fragment Caching
Fragment caching refers to caching a certain fragment of a web page. For example, if a page displays annual sales summaries in a table, we can store this table in the cache, reducing the time it needs to be regenerated for each request.
To use fragment cache, call CController::beginCache() and CController::endCache() in the controller view script. Both methods start and end including the page content that will be cached. Similar to data caching, we need a number to identify the cached fragment.
...别的HTML内容... <?php if($this->beginCache($id)) { ?> ...被缓存的内容... <?php $this->endCache(); } ?> ...别的HTML内容...
In the above, if beginCache() returns false, the cached content is automatically inserted in this place; otherwise, The content within the if
statement will be executed and cached when endCache() is triggered.
1. Caching Options
When calling beginCache(), you can provide an array consisting of caching options as the second parameter to customize the fragment cache. In fact, for convenience, the beginCache() and endCache() methods are wrappers of the [COutputCache] widget. Therefore all properties of COutputCache can be initialized in cache options.
Validity period (Duration)
Perhaps the most common option is duration, which specifies how long the content will be valid in the cache. It is somewhat similar to CCache::set() expiration parameter. The code below caches content snippets for up to one hour:
...其他HTML内容... <?php if($this->beginCache($id, array('duration'=>3600))) { ?> ...被缓存的内容... <?php $this->endCache(); } ?> ...其他HTML内容...
If we don’t set a period, it will default to 60 , which means that cached content will be invalid after 60 seconds.
Dependency
Like data caching, content fragments can also have dependencies when they are cached. For example, the content of an article is displayed depending on whether the article has been modified.
To specify a dependency, we create the dependency option, which can be an object that implements ICacheDependency or a configuration array that can be used to generate dependency objects. The following code specifies that the fragment content depends on whether the value of the lastModified
column changes:
...其他HTML内容... <?php if($this->beginCache($id, array('dependency'=>array( 'class'=>'system.caching.dependencies.CDbCacheDependency', 'sql'=>'SELECT MAX(lastModified) FROM Post')))) { ?> ...被缓存的内容... <?php $this->endCache(); } ?> ...其他HTML内容...
changes (Variation)
The cached content can vary according to some parameters. For example, everyone’s profile is different. The cached profile content will change based on each person ID. This means that a different ID will be used when beginCache() is called.
COutputCache has this feature built in, and programmers do not need to write a pattern that changes content based on ID. Below is a summary.
varyByRoute: Set this option to true, and the cached content will change according to the route. Therefore, each controller and action combination will have a separate cached content.
varyBySession: Set this option to true and the cached content will change based on the session ID. Therefore, each user session may see different content served by the cache.
varyByParam: Set the name in the array of this option. The cached content will change according to the value of the GET parameter. For example, if a page displays article content based on the
id
GET parameter, we can specify varyByParam asarray('id')
to enable us to cache each article content. Without such changes, we would only be able to cache a certain article.varyByExpression: by setting this option to a PHP expression, we can make the cached content to be variated according to the result of this PHP expression. This option has been available since version 1.0. 4.
Request Type
Sometimes, we want fragment caching to be enabled only for certain types of requests. For example, for a form to be displayed on a certain web page, we only want to cache the initially requested form (via a GET request). Any form that is subsequently displayed (via a POST request) will not be cached, as the form may contain user input. To do this, we can specify the requestTypes option:
...其他HTML内容... <?php if($this->beginCache($id, array('requestTypes'=>array('GET')))) { ?> ...被缓存的内容... <?php $this->endCache(); } ?> ...其他HTML内容...
2. Nested Caching
Fragment caches can be nested. That is, a cache fragment is attached to a larger fragment cache. For example, comments are cached in the internal snippet cache, and they are cached together in the external cache with article content.
...其他HTML内容... <?php if($this->beginCache($id1)) { ?> ...外部被缓存内容... <?php if($this->beginCache($id2)) { ?> ...内部被缓存内容... <?php $this->endCache(); } ?> ...外部被缓存内容... <?php $this->endCache(); } ?> ...其他HTML内容...
Nested caching can set different caching options. For example, in the example above, the internal cache and the external cache can be set to different duration values. When data stored in the external cache is invalidated, the internal cache can still serve valid internal fragments. However, the reverse is not possible. If the external cache contains valid data, it will keep a cached copy forever, even if the content in the internal cache has expired.
The above is the content of Yii Framework Official Guide Series 30 - Caching: Fragment Caching. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Yii is a high-performance MVC framework based on PHP. It provides a very rich set of tools and functions to support the rapid and efficient development of web applications. Among them, the RESTful API function of the Yii framework has attracted more and more attention and love from developers, because using the Yii framework can easily build high-performance and easily scalable RESTful interfaces, providing strong support for the development of web applications. . Introduction to RESTfulAPI RESTfulAPI is a

Yii framework middleware: providing multiple data storage support for applications Introduction Middleware (middleware) is an important concept in the Yii framework, which provides multiple data storage support for applications. Middleware acts like a filter, inserting custom code between an application's requests and responses. Through middleware, we can process, verify, filter requests, and then pass the processed results to the next middleware or final handler. Middleware in the Yii framework is very easy to use

With the rapid development of web applications, modern web development has become an important skill. Many frameworks and tools are available for developing efficient web applications, among which the Yii framework is a very popular framework. Yii is a high-performance, component-based PHP framework that uses the latest design patterns and technologies, provides powerful tools and components, and is ideal for building complex web applications. In this article, we will discuss how to use Yii framework to build web applications. Install Yii framework first,

Steps to implement web page caching and page chunking using the Yii framework Introduction: During the web development process, in order to improve the performance and user experience of the website, it is often necessary to cache and chunk the page. The Yii framework provides powerful caching and layout functions, which can help developers quickly implement web page caching and page chunking. This article will introduce how to use the Yii framework to implement web page caching and page chunking. 1. Turn on web page caching. In the Yii framework, web page caching can be turned on through the configuration file. Open the main configuration file co

In recent years, with the rapid development of the game industry, more and more players have begun to look for game strategies to help them pass the game. Therefore, creating a game guide website can make it easier for players to obtain game guides, and at the same time, it can also provide players with a better gaming experience. When creating such a website, we can use the Yii framework for development. The Yii framework is a web application development framework based on the PHP programming language. It has the characteristics of high efficiency, security, and strong scalability, and can help us create a game guide more quickly and efficiently.

Yii framework middleware: Add logging and debugging capabilities to applications [Introduction] When developing web applications, we usually need to add some additional features to improve the performance and stability of the application. The Yii framework provides the concept of middleware that enables us to perform some additional tasks before and after the application handles the request. This article will introduce how to use the middleware function of the Yii framework to implement logging and debugging functions. [What is middleware] Middleware refers to the processing of requests and responses before and after the application processes the request.

In the Yii framework, controllers play an important role in processing requests. In addition to handling regular page requests, controllers can also be used to handle Ajax requests. This article will introduce how to handle Ajax requests in the Yii framework and provide code examples. In the Yii framework, processing Ajax requests can be carried out through the following steps: The first step is to create a controller (Controller) class. You can inherit the basic controller class yiiwebCo provided by the Yii framework

Encrypting and decrypting sensitive data using Yii framework middleware Introduction: In modern Internet applications, privacy and data security are very important issues. To ensure that users' sensitive data is not accessible to unauthorized visitors, we need to encrypt this data. The Yii framework provides us with a simple and effective way to implement the functions of encrypting and decrypting sensitive data. In this article, we’ll cover how to achieve this using the Yii framework’s middleware. Introduction to Yii framework Yii framework is a high-performance PHP framework.
