Home

Awesome

Build Status PHPStan Packagist Version GitHub codecov.io Code Coverage

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 🛠️

Requirements ⚙️

Supported types ✅

Installation 📝

Install the library via Composer:

composer require talleu/php-redis-om

Depending on your configuration, use phpredis or Predis

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):

vendor/bin/redisMigration <YOUR DIRECTORY PATH>

Then you can use the ObjectManager to persist your objects from Redis:

<?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 = $objectManager->find(User::class, 1);
$user = $objectManager->getRepository(User::class)->find(1);
$user = $objectManager->getRepository(User::class)->findOneBy(['name' => 'John Doe']);

// Retrieve a collection of objects
$users = $objectManager->getRepository(User::class)->findAll();
$users = $objectManager->getRepository(User::class)->findBy(['name' => 'John Doe'], ['createdAt' => 'DESC'], 10);

Advanced documentation 📚