How to temporarily turn off cache

Notice: This thread is very old.
Morfeo21
Member | 35
+
0
-

Hi!

I'd like to ask how can I temporarily turn off a cache for a special script – XML import in concrete.
I have XML import which uses repositories' methods for insert and update of products. These methods contains some required funcionalities. One of them is invalidating cache. It's required bnecasuse these methods are used in administration as well for update of single products.

But when I'm importing about 10 000 products the application starts to fall down on lack of memory. In case of the import I'd like to turn off the cache (set storage to DevNullStorage). But I realized that Cache class has no method setStorage() except he constructor.

Is there a way I can turn off the cache for the special script? From config.neon, on the beginnig of the import … I don't care, I just need the way how to do it :))

Thanks! :)

sKopheK
Member | 207
+
0
-

I use this in BasePresenter:

public function startup()
{
    parent::startup();

    //disable cache
    $cache = \Nette\Environment::getCache();
    $cache->clean(array($cache::ALL => TRUE));
}
Morfeo21
Member | 35
+
0
-

This is not what I need. I don't want to invalidate whole cache. I just want to switch cache storages.

I sorted it out with these lines:

$container->removeService('cache');
$container->addService('cache', new Nette\Caching\Cache(new Nette\Caching\Storages\DevNullStorage()));

Do you guys have any other solution?

Last edited by Morfeo21 (2014-03-24 10:47)

Vojtěch Dobeš
Gold Partner | 1316
+
0
-

I ussualy instantiate Cache class in my own classes, I don't register it as service. So maybe this approach could give you desired flexibility without hacking DI container like this. I have only Caching\IStorage as service.