DoctrineCacheBundle使用redis Supress Errors如果无法连接

时间:2017-11-13 11:43:55

标签: php symfony redis

由于某些原因,DoctrineCacheBundle无法连接到redis(例如redis服务器已被终止)服务器,我需要以某种方式压制被抛出的ContextErrorException

  

Redis服务器消失了

用例如下:我使用API​​获取一些数据并使用DoctrineCacheBundle将它们缓存到redis中。

但是如果redis已关闭,那么我想跳过缓存并继续从api获取数据。

示例代码解释了我想要做的事情:

use Doctrine\Common\Cache\Cache as RedisClient;

class DataFetchingStrategy 
{

  private $redisClient=null;

  public function __construct(RedisClient $redisClient)
  {
      $this->redisClient=$redisClient;
  }

  public function get($item)
  {
    try {
      if($this->redisClient->contains($item)) {
        return $this->redisClient->fetch($item);
      }
    } catch(\Exception $e) {
      //Log the error here but do not break
    }

    return $this->callApi($item);
  }

  private function callApi(item)
  {
    //API Call stuff here
  }
}

你有什么想法我会如何压制当我通过redis连接并强制执行wh api呼叫时发生的错误?

编辑1:

进一步调查证明,当我尝试从控制器中检索DataFetchingStrategy时,redis服务器已关闭(在本地开发机器上),例如:

class MyController extends Controller
{
  public function someThingAction()
  {
    $dataFetchingStrategy=$this->get(DataFetchingStrategy:class)//Here I get the problem
    //Render view
  }

}

我收到一条带有消息的RedisException

  

Redis服务器消失了

此外,我的问题是,当我的redis服务器关闭但是继续从API获取时,不会出现异常,就像没有发生任何事情一样。

0 个答案:

没有答案