By default for Laravel file cache if redis does not work

In the ā€œ monkey of chaos ā€ spirit, I’m trying to ensure that the laravel application continues to run even when the services on which it depends do not work.

It uses a database for primary storage and redis cache. I would like it to automatically return to the file cache if and when redis fails.

I could not find a clear example.

+6
source share
3 answers

One way to solve this problem is to overwrite the default Laravel class Illuminate\Cache\CacheManager and change the ioc binding

 class MyCacheManager extends Illuminate\Cache\CacheManager { protected function createRedisDriver(array $config) { try { return parent::createRedisDriver($config); } catch (\Exception $e) { //Error with redis //Maybe there is a more explicit exception ;) return $this->resolve('file'); } } } 

In some ServiceProvider

 $this->app->singleton('cache', function($app) { return new MyCacheManager($app); }); 

This solution will also support the Cache facade :)

+9
source

There seems to be a package for this: https://github.com/xtcat/laravel-redis-fallback

I have not yet found similar packages for sessions or queues. Please share this if anyone has.

0
source

Can you enable anonymous function instead of configuring cnfig cache driver?

which may try to use Redis, and if it fails to return the file as a parameter, and Redis if it succeeds

0
source

Source: https://habr.com/ru/post/981285/


All Articles