Cache is a simple and effective way to improve the performance of Web applications. By storing relatively static data to the cache and retrieving the cache when receiving the request, we save the time required to generate the data. Using cache in Yii mainly includes configuration...
Cache is a simple and effective way to improve the performance of Web applications. By storing relatively static data to the cache and retrieving the cache when receiving the request, we save the time required to generate the data.
Using cache in Yii mainly includes configuring and accessing an application component. The following application configuration sets a cache component that uses two memcache cache servers.
array( ...... 'components'=>array( ...... 'cache'=>array( 'class'=>'system.caching.CMemCache', 'servers'=>array( array('host'=>'server1', 'port'=>11211, 'weight'=>60), array('host'=>'server2', 'port'=>11211, 'weight'=>40), ), ), ),);
When the application is running, the cache component canYii::app()->cacheAccess.
Yii provides different cache components to store cached data in different media. For example, the CMemCache component encapsulates the memcache extension of PHP and uses the memory as the cache storage medium. The CApcCache component encapsulates php apc extensions, while the CDbCache component saves cached data to the database. The following is a list of available cache components:
CMemCache: use PHP memcache extension.
CApcCache: php apc extension.
CXCache: use PHP XCache extension. Note that this is supported from version 1.0.1.
CEAcceleratorCache: use PHP EAccelerator extension.
CDbCache: uses a data table to store cached data. By default, it creates and uses a SQLite3 database under the runtime directory. You can also specify a database for it by setting its connectionID attribute.
CZendDataCache: use Zend Data Cache as the backend Cache medium. Note that this is supported from version 1.0.4.
CFileCache: uses files to store cached data. This is especially suitable for storing large volumes of data (such as pages ). Note that this is supported from Version 1.0.6.
CDummyCache: currently, dummy cache does not support caching. The purpose of this component is to simplify the code that needs to check the cache availability. For example, you can use this cache component when the development stage or the server does not yet support the actual cache function. After the actual cache support is enabled, we can switch to the corresponding cache component. In both cases, we can use the same codeYii::app()->cache->get($key)Get data fragments without worryingYii::app()->cacheIt may benull. This component is supported from version 1.0.5.
Tip:Since all these cache components inherit from the same base class CCache, you can switch to another caching method without changing the code that uses the cache.
Cache can be used at different levels. At the lowest level, we use the cache to store a single data segment, such as a variable.Data caching). In the next level, we store a page segment generated by a part of the view script in the cache. At the highest level, we store the entire page in the cache and retrieve it as needed.
In the following sections, we will explain in detail how to use caching at these levels.
Note:As defined, cache is an unstable storage medium. Even if no timeout occurs, it does not ensure that the cached data exists. Therefore, do not use the cache as persistent storage. (For example, do not use the cache to store Session data ).
Cache series of articles:
Yii Framework official guide series 29-cache: data cache
Yii Framework official guide series 30-cache: Fragment Caching)
Yii Framework official guide series 31-cache: page cache
Yii Framework official guide series 32-cache: Dynamic Content)
The above is the Yii Framework official guide series 28-cache: overview content. For more information, see PHP Chinese network (www.php1.cn )!