talleu / php-redis-om
A PHP object mapper library for Redis
Requires
- php: >=8.2
- ext-json: *
Requires (Dev)
- api-platform/core: ^4.1
- friendsofphp/php-cs-fixer: ^3.75
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11.0
- predis/predis: ^2.0 || ^3.0
- symfony/browser-kit: ^7.2
- symfony/framework-bundle: ^7.2
- symfony/http-client: ^7.2
- symfony/var-dumper: ^7.0
- symfony/yaml: ^7.2
Suggests
- ext-redis: To use the php extension for Redis (phpredis)
- predis/predis: To use Predis as a Redis PHP client
- dev-main
- v1.0.0
- v0.7.1
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- dev-chore/v1
- dev-rm-duplicate-in-redis-client
- dev-chore/api_p_extension_typos
- dev-chore/php_84
- dev-chore/support_api_p
- dev-chore/fix_expire_doc
- dev-chore/suggest_sf_framework
- dev-chore/get_expiration_time
- dev-chore/php_8_4_support
- dev-feat/add_phpdoc
This package is auto-updated.
Last update: 2026-04-26 07:53:51 UTC
README
php-redis-om 🗄️
A PHP object mapper for Redis.
An Object Mapper for Redis®, designed to providing an intuitive and familiar interface for PHP developers to interact with Redis.
Features 🛠️
- Doctrine-like methods and architecture
- Symfony bundle integration
- Easy integration with existing PHP applications
- High performance and scalability with Redis®
- Support for Redis JSON module
- Automatic schema generation
- Search and query capabilities with range filters
- Auto-expiration of your objects
- PHP enum support (backed enums)
- Identity map and dirty tracking with partial updates
- Atomic transactions (MULTI/EXEC)
- Pagination with total count
- GEO queries (radius search)
- Pipeline batch reads
- API Platform support (beta)
Requirements ⚙️
- PHP 8.2 or higher
- Redis 4.0 or higher
- Redisearch module (available by default with Redis >8 or in redis-stack distribution) (installation)
- php-redis extension OR Predis library
- Redis JSON module (optional, include in redis-stack)
- Composer
Supported types ✅
- scalar (string, int, float, bool, double)
- PHP backed enums (string and int)
- timestamp
- json
- null
- DateTimeImmutable
- DateTime
- array and nested arrays
- object and nested objects
- stdClass
Installation 📝
Install the library via Composer:
composer require talleu/php-redis-om
Depending on your configuration, use phpredis or Predis
Symfony bundle 🎵
In a Symfony application, you may need to add this line to config/bundles.php
Talleu\RedisOm\Bundle\TalleuRedisOmBundle::class => ['all' => true],
And that's it, your installation is complete ! 🚀
API Platform support 🕷️
For API Platform users, a basic implementation is provided here: API Platfom X Redis
Basic Usage 🎯
Add the RedisOm attribute to your class to map it to a Redis schema:
<?php use Talleu\RedisOm\Om\Mapping as RedisOm; #[RedisOm\Entity] class User { #[RedisOm\Id] #[RedisOm\Property] public int $id; #[RedisOm\Property(index:true)] public string $name; #[RedisOm\Property] public \DateTimeImmutable $createdAt; }
After add the RedisOm attribute to your class,
you have to run the following command to create the Redis schema for your classes (default path is ./src):
For Symfony users:
bin/console redis-om:migrate
For others PHP applications:
vendor/bin/redisMigration <YOUR DIRECTORY PATH>
Then you can use the ObjectManager to persist your objects from Redis ! 💪
For Symfony users, just inject the RedisObjectManagerInterface in the constructor:
<?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Talleu\RedisOm\Om\RedisObjectManagerInterface; use App\Entity\Book; class MySymfonyController extends AbstractController { public function __construct(private RedisObjectManagerInterface $redisObjectManager) {} #[Route('/', name: 'app_home')] public function index(): Response { $book = new Book(); $book->name = 'Martin Eden'; $this->redisObjectManager->persist($book); $this->redisObjectManager->flush(); //.. } }
For others PHP applications:
<?php use Talleu\RedisOm\Om\RedisObjectManager; $user = new User() $user->id = 1; $user->name = 'John Doe'; // Persist the object in redis $objectManager = new RedisObjectManager(); $objectManager->persist($user); $objectManager->flush();
🥳 Congratulations, your PHP object is now registered in Redis !
You can now retrieve your user wherever you like using the repository provided by the Object Manager (or the object manager directly):
// Retrieve the object from redis $user = $this->redisObjectManager->find(User::class, 1); $user = $this->redisObjectManager->getRepository(User::class)->find(1); $user = $this->redisObjectManager->getRepository(User::class)->findOneBy(['name' => 'John Doe']); // Retrieve a collection of objects $users = $this->redisObjectManager->getRepository(User::class)->findAll(); $users = $this->redisObjectManager->getRepository(User::class)->findBy(['name' => 'John Doe'], ['createdAt' => 'DESC'], 10);
Enum Support 🏷️
PHP backed enums are natively supported:
enum Status: string { case ACTIVE = 'active'; case INACTIVE = 'inactive'; } #[RedisOm\Entity] class Task { #[RedisOm\Id] #[RedisOm\Property] public int $id; #[RedisOm\Property(index: true)] public Status $status; }
Search by enum value:
$activeTasks = $repository->findBy(['status' => 'active']);
Range Queries 🔢
Use MongoDB-style operators for numeric range searches:
// Age between 18 and 65 $users = $repository->findBy(['age' => ['$gte' => 18, '$lte' => 65]]); // Price greater than 100 $products = $repository->findBy(['price' => ['$gt' => 100]]); // Score less than 50 $results = $repository->findBy(['score' => ['$lt' => 50]]); // Combine with exact match $results = $repository->findBy(['name' => 'John', 'age' => ['$gte' => 18]]);
Supported operators: $gte (>=), $gt (>), $lte (<=), $lt (<).
Note: Range queries work automatically with HASH format (NUMERIC index is auto-generated for int/float). For JSON format, you must explicitly declare a NUMERIC index:
#[Property(index: ['age' => 'NUMERIC'])].
Pagination 📄
$paginator = $repository->paginate( criteria: ['status' => 'active'], page: 2, itemsPerPage: 20, orderBy: ['createdAt' => 'DESC'] ); $paginator->getItems(); // Current page items $paginator->getTotalItems(); // Total matching count $paginator->getTotalPages(); // Total number of pages $paginator->getCurrentPage(); // Current page number $paginator->hasNextPage(); // bool $paginator->hasPreviousPage(); // bool // Iterable foreach ($paginator as $item) { // ... }
Partial Updates (Merge) ⚡
Instead of re-persisting the entire object, use merge() to only update changed fields:
$user = $objectManager->find(User::class, 1); $user->name = 'New Name'; // Only this field changed $objectManager->merge($user); // Detects change, updates only 'name' $objectManager->flush();
For new objects (not loaded via find()), merge() falls back to a full persist().
Batch Reads (Pipeline) 🚀
Load multiple objects by ID in a single Redis pipeline call:
$users = $repository->findMultiple([1, 2, 3, 4, 5]);
GEO Queries 🌍
Search objects within a geographic radius (requires a GEO-indexed property):
#[RedisOm\Property(index: ['location' => 'GEO'])] public string $location; // Format: "longitude,latitude" $nearby = $repository->findByGeoRadius('location', 2.3522, 48.8566, 10, 'km');