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