Using Redis cache with Symfony

Yozhef Hisem
3 min readFeb 22, 2021

History

Redis was created in 2009 by Salvatore Sanfilippo, who still remains a lead developer of the project. Redis is sometimes described as “Memcached on steroids,” which is hardly surprising considering that parts of Redis were built in response to lessons learned from using Memcached. Redis has more features than Memcached and is,more powerful and flexible.

How to configure Redis

Create a blank Symfony project:

# run this if you are building a traditional web application
composer create-project symfony/website-skeleton my_project_name

# building a microservice, console application or API
composer create-project symfony/skeleton my_project_name

Add Redis-Bundle:

composer require symfony-bundles/redis-bundle

After installing you see in your AppKernel class adding the Symfony Bundle Redis Bundle class in the bundle’s array, and create a new config file

#projectName/config/bundles.php
SymfonyBundles\RedisBundle\SymfonyBundlesRedisBundle::class => ['all' => true],
#projectName/config/packages/sb_redis.yaml:
sb_redis:
clients:
default:
$options: []
$parameters: ['tcp://127.0.0.1:6379?database=3']

How to use

Let’s look at a simple example of creating a RedisStorageManager

<?php

declare(strict_types=1);

namespace App\Infrastructure\Service\Redis;

use SymfonyBundles\RedisBundle\Redis\ClientInterface;

class RedisStorageManager
{
private ClientInterface $redis;

public function __construct(ClientInterface $redis)
{
$this->redis = $redis;
}
}

Now, let’s imagine that we need to create a function that is looking for something

public function find(string $key): ?string
{
return $this->redis->get($this->getRedisKey($key));
}

You may be wondering what this new getRedisKey method is. It is a key that allows us to reduce the possibility of crossing similar keys for different user flows.

private function getRedisKey(string $key): string
{
return sprintf('%s.%s', $this->prefix, $key);
}

Next, we’ll try to save something in Redis. In this case, automatic cleaning of the so-called lifetime can be very helpful

public function set(string $key, string $value): void
{
$key = $this->getRedisKey($key);

$this->redis->set($key, $value);
$this->redis->expire($key, $this->ttl);
}

If we consider cases (search, store, delete), then our Redis Storage Manager -> will increase the functionality that will easily allow us to manage data

<?php

declare(strict_types=1);

namespace App\Infrastructure\Service\Redis;

use SymfonyBundles\RedisBundle\Redis\ClientInterface;

class RedisStorageManager
{
private string $prefix;
private int $ttl;
private ClientInterface $redis;

public function __construct(string $prefix, int $ttl, ClientInterface $redis)
{
$this->prefix = $prefix;
$this->ttl = $ttl;
$this->redis = $redis;
}

public function find(string $key): ?string
{
return $this->redis->get($this->getRedisKey($key));
}

public function findArrayValue(string $key): ?array
{
return $this->redis->hgetall($this->getRedisKey($key));
}

public function set(string $key, string $value): void
{
$key = $this->getRedisKey($key);

$this->redis->set($key, $value);
$this->redis->expire($key, $this->ttl);
}

public function setArray(string $key, array $value): void
{
$key = $this->getRedisKey($key);

$this->redis->hmset($key, $value);
$this->redis->expire($key, $this->ttl);
}

public function remove(string $key): void
{
$this->redis->remove($this->getRedisKey($key));
}

public function has(string $key): bool
{
return (bool) $this->find($this->getRedisKey($key));
}

private function getRedisKey(string $key): string
{
return sprintf('%s.%s', $this->prefix, $key);
}
}

That’s it! You can use the Redis Store anywhere in your project and manipulate the data easily.

--

--

Yozhef Hisem

Senior Software Engineer from Kyiv, in @MacPaw company.