How to disable caching via configuration in Yii?

In Yii, I turned on APC caching through the config / main.php file:

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

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

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

However, is there a way to temporarily disable this based on configuration? I do not want it to be turned on and YII_DEBUG set to true, and would like $votes = Yii::app()->cache->get("key"); always returned false, as if it was empty.

I tried to disable this by simply commenting out the configuration settings, but it gives (not unreasonable) errors: Call to a member function get() on a non-object

+8
php caching yii
source share
3 answers

You can configure a cache class that does not cache at all (therefore, it will not store anything, and get() will always return FALSE ).

Does Yii probably already come with no-cache? Yes, it is called CDummyCache , and it does not CDummyCache at all.

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

See Caching Documents .

+11
source share

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

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

CDummyCache is a component of the placeholder cache.

CDummyCache does not cache anything. It is provided so that you can always configure the application component "cache", and it does not need to check whether the cache Yii :: app () โ†’ zero or not. By replacing CDummyCache with another cache component, you can quickly switch from non-cached mode to cached mode.

Yii 1.x: CDummyCache document

Yii 2.x: DummyCache doc

+3
source share

Try this code:

 'cache' => array( 'class' => 'system.caching.CFileCache' ), 
-4
source share

All Articles