Caching is a simple and effective way to improve the performance of web applications. By storing relatively static data in the cache and retrieving it when a request is received, we save the time required to generate this data.
Using cache in Yii mainly involves configuring and accessing an application component. The following application configuration sets up 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), ), ), ), );
Yii::app()->cache Visit.
Yii::app()->cache->get($key) to get the data fragment without worrying about
Yii:: app()->cache may be
null. This component is supported since version 1.0.5.
Caching can be used at different levels. At the lowest level, we use caches to store individual pieces of data, such as variables. We call thisTips: Since all these cache components inherit from the same base class CCache, you can switch to it without changing the code that uses the cache. Use another caching method.
data caching. At the next level, we store in the cache a page fragment generated as part of the view script. And in the highest level, we store the entire page in cache and retrieve it when needed.
In the next few sections, we will explain in detail how to use caching at these levels.Cache series articles: Yii Framework Official Guide Series 29 - Cache: Data Cache Yii Framework Official Guide Series 30 - Cache: Fragment Caching ( Fragment Caching)Yii Framework Official Guide Series 31 - Caching: Page CachingYii Framework Official Guide Series 32 - Caching: Dynamic Content (Dynamic Content) and above This is the content of Yii Framework Official Guide Series 28 - Caching: Overview. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!Note: By definition, cache is an unstable storage medium. Even if there is no timeout, it does not ensure that the cached data will exist. Therefore, do not use cache as persistent storage. (For example, do not use cache to store Session data).