Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
/ redis-sentinel Public archive

Configuration wrapper for redis-sentinel or plain old redis

License

Notifications You must be signed in to change notification settings

lamoda/redis-sentinel

Repository files navigation

Lamoda redis sentinel

Build Status Scrutinizer Code Quality Code Coverage Build Status

Redis configuration wrapper for use with redis sentinel or plain redis server. Used to get connection settings for sentinel or plain old redis if sentinel is not defined (say for dev/test environment).

Usage example

use Lamoda\RedisSentinel\RedisLocator;

$redisLocator = new RedisLocator(
    // plain redis:
    [
        'protocol' => 'tcp',
        'host' => 'redis-host',
        'port' => 6379,
        'dbindex' => 0,
        'connectionName' => uniqid('client-app', true),
    ],
    // redis sentinel:
    [
        'url' => 'redis-sentinel1:26379; redis-sentinel2:26379',
        'redisName' => 'mastername',
    ]
);

// Discover current sentinel master:
$redisConfig = $redisLocator->getRedisConfig();

$redis = new \Redis();
$redis->connect($redisConfig->getHost(), $redisConfig->getPort());
$redis->client('setname', $redisConfig->getConnectionName());
$redis->select($redisConfig->getDbIndex());

Or if you don't have sentinel:

$redisLocator = new RedisLocator(
    // plain redis:
    [
        'protocol' => 'tcp',
        'host' => 'redis-host',
        'port' => 6379,
        'dbindex' => 0,
        'connectionName' => uniqid('client-app', true),
    ],
    // redis sentinel:
    [
        'url' => '',
        'redisName' => 'mastername',
    ]
);

// Return plain redis config:
$redisConfig = $redisLocator->getRedisConfig();

docker-compose for local usage

You can use docker-compose files to ease local sentinel usage & testing.

To start redis-sentinel containers:

docker-compose -f docker/docker-compose.yml up -d

Or using make:

make up
# ...
make down