Caching

Caching is a cheap and effective way to improve the performance of a Web application. By storing relatively static data in cache and serving it from cache when requested, we save the time needed to generate the data.

Using cache in Yii mainly involves configuring and accessing a cache application component. The following application configuration specifies a cache component that uses memcache with two 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 can be accessed via Yii::app()->cache.

Yii provides various cache components that can store cached data in different media. For example, the CMemCache component encapsulates the PHP memcache extension and uses memory as the medium of cache storage; the CApcCache component encapsulates the PHP APC extension; and the CDbCache component stores cached data in database. The following is a summary of the available cache components:

  • CMemCache: uses PHP memcache extension.

  • CApcCache: uses PHP APC extension.

  • CXCache: uses PHP XCache extension.

  • CEAcceleratorCache: uses PHP EAccelerator extension.

  • CRedisCache: uses redis key value-store as storage backend.

  • CDbCache: uses a database table to store cached data. By default, it will create and use a SQLite3 database under the runtime directory. You can explicitly specify a database for it to use by setting its connectionID property.

  • CZendDataCache: uses Zend Data Cache as the underlying caching medium.

  • CWinCache: uses PHP WinCache (see also) extension.

  • CFileCache: uses files to store cached data. This is particularly suitable to cache large chunk of data (such as pages).

  • CDummyCache: presents dummy cache that does no caching at all. The purpose of this component is to simplify the code that needs to check the availability of cache. For example, during development or if the server doesn't have actual cache support, we can use this cache component. When an actual cache support is enabled, we can switch to use the corresponding cache component. In both cases, we can use the same code Yii::app()->cache->get($key) to attempt retrieving a piece of data without worrying that Yii::app()->cache might be null.

Tip: Because all these cache components extend from the same base class CCache, one can switch to use a different type of cache without modifying the code that uses cache.

Caching can be used at different levels. At the lowest level, we use cache to store a single piece of data, such as a variable, and we call this data caching. At the next level, we store in cache a page fragment which is generated by a portion of a view script. And at the highest level, we store a whole page in cache and serve it from cache as needed.

In the next few subsections, we elaborate how to use cache at these levels.

Note: By definition, cache is a volatile storage medium. It does not ensure the existence of the cached data even if it does not expire. Therefore, do not use cache as a persistent storage (e.g. do not use cache to store session data).

Total 2 comments

#14455 report it
Emile Bons at 2013/08/13 02:40am
Disable during development

When developing you might not want to have caching to make improvements visible, though at the same time you want to know what the impact (both positive and negative) could be of using caching. That's why I disable caching dynamically in my 'BaseController' using the following lines of code:

function init() {
    $this->updateCache();
}
 
private function updateCache() {
    if(Yii::app()->request->getParam('cache', 'true') === 'false')
        Yii::app()->setComponent('cache', new CDummyCache());
}

This allows you (and front-end dev's) to disable caching by putting '?cache=false" at the end of the URL you're working on.

#7152 report it
robregonm at 2012/02/28 11:56am
Fastest cache component

Based on my previous experience, APC is the fastest when you have one server only and MemCache is the fastest (and better) when you have several servers for your application (eg. Load balancing servers)

Leave a comment

Please to leave your comment.