php-redis-om available as a Symfony bundle
Published on February 12, 2025
To read the article in French, click here.
Last summer, we published a blog post about php-redis-om, a PHP library that allows you to map your PHP classes directly to Redis. By leveraging the different HASH and JSON formats available in Redis, php-redis-om enables you to persist and query objects following the same model as a traditional ORM in the PHP ecosystem—well known to Symfony developers through Doctrine and the repository pattern. Since December 2024, php-redis-om is now available and can be used as a Symfony bundle.
![php-redis-om bundle Symfony" class="wp-image-10810" style="width:1024px;height:auto"/><figcaption class="wp-element-caption](/wp-content/uploads/2025/02/article-lib-Clement-1-1024x569.webp)
To install it, use Composer:
composer require talleu/php-redis-om
If you are working on a Symfony project, the registration in config/bundles.php
will be done automatically.
Talleu\RedisOm\Bundle\TalleuRedisOmBundle::class => [‘all’ => true],
To update your indexes, run the following Symfony command:
bin/console redis-om:migrate
On the dependency injection side, you can take advantage of Symfony's autowiring by specifying the interface of your ObjectManager
in the constructor of the class where you want to use it.
public function __construct(private RedisObjectManagerInterface $redisObjectManager) {}
public function create()
{
$book = new Book(name: ‘Le talon de fer’);
$this->redisObjectManager->persist($book);
$this->redisObjectManager->flush();
}
And that's it! You can now persist your objects and retrieve them using the repository provided with the ObjectManager
.
Among the other new features of the bundle, you will find the ability to add an expiration time to your objects. Highly appreciated by Redis users, this feature now allows you to set a TTL (Time To Live) on your class's mapping attribute so that it is automatically removed from your Redis instance upon expiration, starting from 3,600 seconds.
#[RedisOm\Entity(ttl: 3600)]
In short, whether it's for persisting your objects or taking advantage of advanced features like TTL, php-redis-om offers an excellent way to leverage the power of Redis while staying within the familiar Symfony ecosystem. Feel free to try out this library and share your feedback on GitHub!