Library for creating redis instance depends on application / server possibilities
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
$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);
...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
$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);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
);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
);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);

