Ian Hunter Ian Hunter - 1 year ago 145
PHP Question

How can I disable caching via configuration in 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
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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download