Switching between caches

Hi, pepople!

Howto implement\configure switching between multiple caches. For example, to cache data - use CMemCache and for pages and their fragments - CFileCache.


Hi, first of all you should define all possible Cache components in config. Example:cache (uses CFileCache) and remoteCache (uses CMemCache).

Now for fragments-caching. Let’s assume you want to cache fragments remotely:

if ($this->beginCache($key, array('cacheID' => 'remoteCache')))


   // Generate data...



By default, ‘cacheID’ is set to ‘cache’. So if you want to store in file you can just do:

if ($this->beginCache($key))

// ...

Other components like CCacheHttpSession have also $cacheID attribute. So if you want to store sessions remotely you can do:

'components' => array(


   'session' => array(

      'class' => 'CCacheHttpSession',

      'cacheID' => 'remoteCache',




Ohhh! I missed ‘cacheID’ param in api book. Thank you! It’s working!