lulco / redis-proxy
Library for Redis driver abstraction
1.9.0
2026-03-08 14:18 UTC
Requires
- php: ^7.4 | ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5.20
- predis/predis: ^1.1 | ^2.0
Suggests
- ext-redis: Native PHP extension for Redis connectivity.
- predis/predis: Flexible and feature-complete PHP client library for Redis https://github.com/nrk/predis/wiki
README
Library for creating redis instance depends on application / server possibilities
Installation
Composer
The fastest way to install Redis proxy is to add it to your project using Composer (http://getcomposer.org/).
- Install Composer:
curl -sS https://getcomposer.org/installer | php - Require Redis proxy as a dependency using Composer:
php composer.phar require lulco/redis-proxy - 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 millisecondsmaxFails: number of attempts (1= one attempt, no retry)optSerializer:none,php,json,msgpack,igbinaryconnectMode:connectorpconnect
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);