如果redis关闭,则默认为Laravel文件缓存

eva*_*ont 7 php caching redis laravel

本着" 乱猴 " 的精神,我正在努力确保laravel应用程序即使在它所依赖的服务已经停止时也会继续运行.

它使用DB进行主存储,并使用redis缓存.我想做的是,如果redis失败,它会自动回退到文件缓存.

我找不到一个明确的例子.

Jan*_* P. 12

解决此问题的一种方法是覆盖Laravel的默认Illuminate\Cache\CacheManager类并更改ioc绑定

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');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在某些ServiceProvider中

$this->app->singleton('cache', function($app)
{
    return new MyCacheManager($app);
});
Run Code Online (Sandbox Code Playgroud)

这个解决方案也将保持Cache外观工作:)