lulco/redis-proxy

Library for Redis driver abstraction

Maintainers

Package info

github.com/lulco/redis-proxy

pkg:composer/lulco/redis-proxy

Statistics

Installs: 158 063

Dependents: 1

Suggesters: 0

Stars: 3

Open Issues: 2

1.9.0 2026-03-08 14:18 UTC

README

Library for creating redis instance depends on application / server possibilities

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight Latest Stable Version Total Downloads PHP 7 ready

Installation

Composer

The fastest way to install Redis proxy is to add it to your project using Composer (http://getcomposer.org/).

  1. Install Composer:
    curl -sS https://getcomposer.org/installer | php
    
  2. Require Redis proxy as a dependency using Composer:
    php composer.phar require lulco/redis-proxy
    
  3. Install Redis proxy:
    php composer.phar update
    

Usage

Single redis node

$database = 0;
$timeout = 0.0;
$retryWait = null;
$maxFails = null;
$optSerializer = \RedisProxy\ConnectionFactory\Serializers::NONE;
$operationTimeout = null;
$connectMode = \RedisProxy\RedisProxy::CONNECT_MODE_CONNECT; // or CONNECT_MODE_PCONNECT

$redis = new \RedisProxy\RedisProxy(
    $host,
    $port,
    $database,
    $timeout,
    $retryWait,
    $maxFails,
    $optSerializer,
    $operationTimeout,
    $connectMode
);

// Call redis methods
$redis->select($database);
$redis->hset($key, $field, $value);
$redis->hlen($key);
$redis->hget($key, $field);
$redis->hgetall($key);
...

Connection parameters

  • timeout: timeout for establishing connection in seconds (0.0 = unlimited)
  • operationTimeout: read/write timeout in seconds (null = default driver behavior)
  • retryWait: delay before retry in milliseconds
  • maxFails: number of attempts (1 = one attempt, no retry)
  • optSerializer: none, php, json, msgpack, igbinary
  • connectMode: connect or pconnect

Sentinel

$sentinels = [
    ['host' => '172.19.0.5', 'port' => 26379],
    ['host' => '172.19.0.6', 'port' => 26379],
    ['host' => '172.19.0.7', 'port' => 26379],
];
$clusterId = 'mymaster';
$database = 0;
$timeout = 0.0;
$retryWait = null;
$maxFails = null;
$writeToReplicas = true;
$operationTimeout = null;
$connectMode = \RedisProxy\RedisProxy::CONNECT_MODE_CONNECT; // or CONNECT_MODE_PCONNECT

$redis = new \RedisProxy\RedisProxy();
$redis->setSentinelConnectionPool(
    $sentinels,
    $clusterId,
    $database,
    $timeout,
    $retryWait,
    $maxFails,
    $writeToReplicas,
    $operationTimeout,
    $connectMode
);

// Call redis methods
$redis->hset($key, $field, $value);
$redis->hlen($key);
$redis->hget($key, $field);
$redis->hgetall($key);

Multi read connection

Read from multiple redis nodes Write to one master redis node

$master = ['host' => '172.19.0.5', 'port' => 26379];
$slaves = [
    ['host' => '172.19.0.5', 'port' => 26379],
    ['host' => '172.19.0.6', 'port' => 26379],
    ['host' => '172.19.0.7', 'port' => 26379],
];
$database = 0;
$timeout = 0.0;
$retryWait = null;
$maxFails = null;
$writeToReplicas = true;
$operationTimeout = null;
$connectMode = \RedisProxy\RedisProxy::CONNECT_MODE_CONNECT; // or CONNECT_MODE_PCONNECT

$redis = new \RedisProxy\RedisProxy();
$redis->setMultiConnectionPool(
    $master,
    $slaves,
    $database,
    $timeout,
    $retryWait,
    $maxFails,
    $writeToReplicas,
    $operationTimeout,
    $connectMode
);

Multi write connection

Write to multiple master redis nodes Optionally read from multiple redis nodes

$masters = [
    ['host' => '172.19.0.5', 'port' => 26379],
    ['host' => '172.19.0.6', 'port' => 26379],
    ['host' => '172.19.0.7', 'port' => 26379],
];
$slaves = [
    ['host' => '172.19.0.5', 'port' => 26379],
    ['host' => '172.19.0.6', 'port' => 26379],
    ['host' => '172.19.0.7', 'port' => 26379],
];
$database = 0;
$timeout = 0.0;
$retryWait = null;
$maxFails = null;
$writeToReplicas = true;
$strategy = \RedisProxy\ConnectionPool\MultiWriteConnectionPool::STRATEGY_RANDOM;
$operationTimeout = null;
$connectMode = \RedisProxy\RedisProxy::CONNECT_MODE_CONNECT; // or CONNECT_MODE_PCONNECT

$redis = new \RedisProxy\RedisProxy();
$redis->setMultiWriteConnectionPool(
    $masters,
    $slaves,
    $database,
    $timeout,
    $retryWait,
    $maxFails,
    $writeToReplicas,
    $strategy,
    $operationTimeout,
    $connectMode
);

RedisProxyFactory config

Single node:

$config = [
    'host' => '127.0.0.1',
    'port' => 6379,
    'database' => 0,
    'timeout' => 0.0,
    'retryWait' => null,
    'maxFails' => null,
    'operationTimeout' => null,
    'connectMode' => 'connect', // or 'pconnect'
    'optSerializer' => 'none', // or 'php', 'json', 'msgpack', 'igbinary'
];

$redis = (new \RedisProxy\RedisProxyFactory())->createFromConfig($config);

Sentinel:

$config = [
    'sentinel' => [
        'sentinels' => [
            ['host' => '172.19.0.5', 'port' => 26379],
            ['host' => '172.19.0.6', 'port' => 26379],
        ],
        'clusterId' => 'mymaster',
        'database' => 0,
        'timeout' => 0.0,
        'retryWait' => null,
        'maxFails' => null,
        'writeToReplicas' => true,
        'operationTimeout' => null,
        'connectMode' => 'connect', // or 'pconnect'
    ],
];

$redis = (new \RedisProxy\RedisProxyFactory())->createFromConfig($config);