PHP code example of lulco / redis-proxy

1. Go to this page and download the library: Download lulco/redis-proxy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lulco / redis-proxy example snippets


$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);
...

$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);

$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
);

$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
);

$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);

$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);

    curl -sS https://getcomposer.org/installer | php
    

    php composer.phar 

    php composer.phar update