RedisStore example

use Symfony\Component\Semaphore\Store\RedisStore;

/** * @author Jérémy Derussé <jeremy@derusse.com> */
abstract class AbstractRedisStoreTestCase extends AbstractStoreTestCase
{
    abstract protected function getRedisConnection(): \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface;

    public function getStore(): PersistingStoreInterface
    {
        return new RedisStore($this->getRedisConnection());
    }
}

class StoreFactory
{
    public static function createStore(#[\SensitiveParameter] object|string $connection): PersistingStoreInterface     {
        switch (true) {
            case $connection instanceof \Redis:
            case $connection instanceof Relay:
            case $connection instanceof \RedisArray:
            case $connection instanceof \RedisCluster:
            case $connection instanceof \Predis\ClientInterface:
                return new RedisStore($connection);

            case $connection instanceof \Memcached:
                return new MemcachedStore($connection);

            case $connection instanceof \MongoDB\Collection:
                return new MongoDbStore($connection);

            case $connection instanceof \PDO:
                return new PdoStore($connection);

            case $connection instanceof Connection:
                
public function getStore(): PersistingStoreInterface
    {
        $redis = new \Predis\Client(array_combine(['host', 'port']explode(':', getenv('REDIS_HOST')) + [1 => 6379]));

        try {
            $redis->connect();
        } catch (\Exception $e) {
            self::markTestSkipped($e->getMessage());
        }

        return new CombinedStore([new RedisStore($redis)]new UnanimousStrategy());
    }

    private MockObject&StrategyInterface $strategy;
    private MockObject&BlockingStoreInterface $store1;
    private MockObject&BlockingStoreInterface $store2;
    private CombinedStore $store;

    protected function setUp(): void
    {
        $this->strategy = $this->createMock(StrategyInterface::class);
        $this->store1 = $this->createMock(BlockingStoreInterface::class);
        
protected function getRedisConnection(): \Redis
    {
        $redis = new \Redis();
        $redis->connect(...explode(':', getenv('REDIS_HOST')));

        return $redis;
    }

    public function testInvalidTtl()
    {
        $this->expectException(InvalidTtlException::class);
        new RedisStore($this->getRedisConnection(), -1);
    }
}
use ExpiringStoreTestTrait;

    protected function getClockDelay(): int
    {
        return 250000;
    }

    abstract protected function getRedisConnection(): \Redis|Relay|\RedisArray|\RedisCluster|\Predis\ClientInterface;

    public function getStore(): PersistingStoreInterface
    {
        return new RedisStore($this->getRedisConnection());
    }

    public function testBackwardCompatibility()
    {
        $resource = uniqid(__METHOD__, true);
        $key1 = new Key($resource);
        $key2 = new Key($resource);

        $oldStore = new Symfony51Store($this->getRedisConnection());
        $newStore = $this->getStore();

        

class StoreFactory
{
    public static function createStore(#[\SensitiveParameter] object|string $connection): PersistingStoreInterface     {
        switch (true) {
            case $connection instanceof \Redis:
            case $connection instanceof Relay:
            case $connection instanceof \RedisArray:
            case $connection instanceof \RedisCluster:
            case $connection instanceof \Predis\ClientInterface:
                return new RedisStore($connection);

            case !\is_string($connection):
                throw new InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection::class));
            case str_starts_with($connection, 'redis://'):
            case str_starts_with($connection, 'rediss://'):
                if (!class_exists(AbstractAdapter::class)) {
                    throw new InvalidArgumentException('Unsupported Redis DSN. Try running "composer require symfony/cache".');
                }
                $connection = AbstractAdapter::createConnection($connection['lazy' => true]);

                return new RedisStore($connection);
        }
Home | Imprint | This part of the site doesn't use cookies.