A
A
Alexander2021-10-05 14:43:22
symfony
Alexander, 2021-10-05 14:43:22

How to check if Redis is running in Symfony Component Cache?

The project uses Redis as a key:value storage (authorization information is stored)

More precisely, the Symfony Component Cache and RedisAdapter component is used.

I encounter a situation where sometimes a client's radish falls or they forget to pick it up :)
I can, but they start pushing bugs, but in fact the problem is that the connection to the radish is not available.

You need to somehow check that the radish is working,
write some kind of decorator in which to implement the isConnected check.

But I don’t understand at all in which direction to dig
, it should turn out something like

Decorator
Get
   Client->connect
    If notConnected
         Throw exception
    Else
          Return client.get


But the problem is that I don’t understand how to get access to the Client
. People say that you don’t need to check anything, you need to make sure that the radish works, etc.
I can do it myself, but I can’t influence the client side in any way.

Here is an example:
#config/packages/cache.yaml
framework:
    cache:
        # Redis
        app: cache.adapter.redis
        default_redis_provider: '%env(resolve:REDIS_URL)%'


<?php
use Symfony\Component\Cache\Adapter\AdapterInterface;

class ContextProcessor {
    /**
    *  @var Symfony\Component\Cache\Adapter\TraceableAdapter
    */
    protected $cache;

    public function __construct( AdapterInterface $cache) {
        $this->cache = $cache;
    }

    private function getContext(string $token): array
    {
        // здесь хотелось бы в случае неудачи проверить работает ли редис
        $item = $this->cache->getItem($token);

        if ($item->isHit()) {
            return $this->serializer->decode($item->get());
        } else {
            throw new HttpException(Response::HTTP_UNAUTHORIZED);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question