Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable caching via configuration in Yii?

Tags:

php

caching

yii

In Yii, I have enabled APC caching through the config/main.php file:

'cache' => array(
    'class' => 'system.caching.CApcCache',
),

and it works just fine when I use Yii's built-in caching methods:

Yii::app()->cache->set('key', $value);

However, is there a way to temporarily turn this off based on configuration? I don't want to have it enabled while YII_DEBUG is set to true, for example, and would like $votes = Yii::app()->cache->get("key"); to always return false as it does when it's empty.

I've tried turning this off by just commenting-out the configuration setting, but it gives (not unreasonable) errors: Call to a member function get() on a non-object

like image 799
Ian Hunter Avatar asked Dec 22 '11 16:12

Ian Hunter


People also ask

How to clear Yii cache?

When you need to invalidate all the stored cache data, you can call yii\caching\Cache::flush(). You can flush the cache from the console by calling yii cache/flush as well. Info: Console application uses a separate configuration file by default.

How to use caching in Yii2?

You can register several cache application components. yii\caching\DbCache − Uses a database table to store cached data. Uou must create a table as specified in yii\caching\DbCache::$cacheTable. yii\caching\ApcCache − Uses PHP APC extension.


1 Answers

You could configure a cache class that does not cache at all (so it won't store anything and get() will always return FALSE).

Probably Yii already ships with a no-cache? Yes it does, it's called CDummyCache and it does no caching at all.

It has been written for the problem you outline in your question that Yii::app()->cache is NULL.

See CachingDocs.

like image 185
hakre Avatar answered Sep 21 '22 01:09

hakre