How to set and get cache value in yii

Before you move ahead on How to set and get cache value in yii, You need to enable it using yii configuration. You can find more details about same in my first blog “How to enable caching in yii framework”.

Set cache value in yii framework
In following code snippet “DataCache” is variable name used to store value in cache, you can change this as you need.
If you wish you make it dynamic you can do this. 2nd argument is data you wish to cache and 3rd parameter is cache lifetime.

  Yii::app()->cache->set('DataCache', $this->Data, 3600000); //refresh the cache every hour

Get cache value in yii framework
Line mentioned bellow will fetch the data for “DataCache” variable.

  $Data = Yii::app()->cache->get('DataCache');
Share this nice post:

Leave a Reply

Your email address will not be published. Required fields are marked *