Ian Hunter
Ian Hunter

Reputation: 9774

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 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

Upvotes: 9

Views: 14124

Answers (3)

Roman Podlinov
Roman Podlinov

Reputation: 24954

If you need to disable cache only locally add the following code into main-local.php. It will override cache configuration in the main.php

'components' => [
... 
    'cache'=> [
        'class'=>'CDummyCache',
    ],
...
]

CDummyCache is a placeholder cache component.

CDummyCache does not cache anything. It is provided so that one can always configure a 'cache' application component and he does not need to check if Yii::app()->cache is null or not. By replacing CDummyCache with some other cache component, one can quickly switch from non-caching mode to caching mode.

Yii 1.x: CDummyCache doc

Yii 2.x: DummyCache doc

Upvotes: 4

user1744812
user1744812

Reputation: 1

Try this code:

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

Upvotes: -4

hakre
hakre

Reputation: 198115

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.

Upvotes: 15

Related Questions