Search by

owner888 / php-redis

owner888

Pure-PHP asynchronous Redis client (RESP2, Fiber connection pool, pipelines, transactions, Streams, Pub/Sub), powered by owner888/php-async.

Package info

github.com/owner888/php-redis

pkg:composer/owner888/php-redis

Statistics

Installs: 25

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.1.0 2026-11-30 21:51 UTC

This package is auto-updated.

Last update: 2026-11-30 21:51:53 UTC


README

Pure-PHP 8.3+ asynchronous Redis client powered by owner888/php-async.

  • Non-blocking TCP and TLS (rediss://) over Fibers — no ext-redis C extension required.
  • Fiber-safe connection pool with backpressure (Deferred waiter queue) and idle expiry.
  • Binary-safe streaming RESP2 encoder/decoder.
  • Pipelining, MULTI/EXEC transactions, Lua EVAL, Streams (XREADGROUP BLOCK on dedicated connections), and Pub/Sub.
  • Shares the host's Scheduler (such as Revolt/Workerman) automatically via owner888/php-async, with no secondary event loop.

Installation

composer require owner888/php-redis

Quick Start

<?php
use Owner888\Async\Async;
use Owner888\Redis\RedisClient;

Async::run(static function (): void {
    $redis = new RedisClient('redis://127.0.0.1:6379/0');

    $redis->set('greeting', 'hello', ttlSeconds: 60);
    echo $redis->get('greeting'), "\n";

    // Concurrent Fiber requests share the connection pool without blocking each other:
    $a = Async::spawn(static fn () => $redis->incr('hits'));
    $b = Async::spawn(static fn () => $redis->incr('hits'));
    echo $a->await(), ' / ', $b->await(), "\n";

    $redis->close();
});

Streams (XREADGROUP BLOCK)

Blocking commands run on a dedicated connection so they never starve the shared pool:

$messages = $redis->xReadGroup(
    group: 'workers',
    consumer: 'worker-1',
    streams: ['stream:jobs' => '>'],
    count: 1,
    blockMs: 5000,
);

Pipelines & Transactions

$replies = $redis->pipeline(static function ($pipe): void {
    $pipe->set('a', '1');
    $pipe->incr('a');
    $pipe->get('a');
});

Pub/Sub

$sub = $redis->subscriber();
$sub->subscribe('events');
$sub->listen(static function (string $channel, string $payload): bool {
    echo "[{$channel}] {$payload}\n";
    return false; // return false to stop listening
});
$sub->close();

Embedding in Workerman / Revolt

Install the host Scheduler once via Owner888\Async\Loop::useScheduler(...) (see php-async), then call RedisClient inside any worker Fiber.

Development

composer install
composer test

To also run the live Redis integration tests:

PHP_REDIS_TEST_HOST=127.0.0.1 PHP_REDIS_TEST_PORT=16379 composer test

License

MIT