CodeIgniter Forums
Cache Dynamic Driver Load - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Cache Dynamic Driver Load (/showthread.php?tid=90520)



Cache Dynamic Driver Load - mywebmanavgat - 03-30-2024

I want to use sometimes file and sometimes redis for cache. What is the way to activate sometimes redis cache and sometimes filecache according to the need in a controls?


RE: Cache Dynamic Driver Load - mywebmanavgat - 03-30-2024

This solved my problem

PHP Code:
$config config('Cache');
 
$config->handler 'file';
 
$cache = \Config\Services::cache($config,false);
 if(! 
$foo $cache->get('foox'))
 {
 
$cache->save('foox', ['1','2'], 300);
 echo 
'cache false';
 }
 else
 {
 
print_r($foo);
 echo 
'cache true';
 } 



RE: Cache Dynamic Driver Load - kenjis - 03-30-2024

The following code changes the value in the shared Config\Cache instance.

PHP Code:
$config config('Cache');
$config->handler 'file'

If you use two Cache service instances at the same time, the following code is better:
PHP Code:
$config = new \Config\Cache;
$config->handler 'file'