Skip to content

Commit

Permalink
Merge pull request #671 from bakaphp/0.2-feature-redis-cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken authored May 25, 2021
2 parents 8736855 + a7b4297 commit f0eaa11
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Providers/RedisProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
use Redis;
use RedisCluster;

class RedisProvider implements ServiceProviderInterface
{
Expand All @@ -19,8 +20,18 @@ public function register(DiInterface $container) : void
$container->setShared(
'redis',
function (bool $prefix = true) use ($app) {
$redis = new Redis();
$redis->connect(envValue('REDIS_HOST', '127.0.0.1'), (int) envValue('REDIS_PORT', 6379));
if (!envValue('REDIS_CLUSTER', 0)) {
$redis = new Redis();
$redis->connect(
envValue('REDIS_HOST', '127.0.0.1'),
(int) envValue('REDIS_PORT', 6379)
);
} else {
$clusters = explode(',', envValue('REDIS_HOST', '127.0.0.1'));
$clusters = array_map('trim', $clusters);

$redis = new RedisCluster(null, $clusters, 1.5, 1.5);
}
if ($prefix) {
$redis->setOption(Redis::OPT_PREFIX, $app . ':'); // use custom prefix on all keys
}
Expand All @@ -45,8 +56,19 @@ function (bool $prefix = true) use ($app) {
$container->setShared(
'redisUnSerialize',
function (bool $prefix = true) use ($app) {
$redis = new Redis();
$redis->connect(envValue('REDIS_HOST', '127.0.0.1'), (int) envValue('REDIS_PORT', 6379));
if (!envValue('REDIS_CLUSTER', 0)) {
$redis = new Redis();
$redis->connect(
envValue('REDIS_HOST', '127.0.0.1'),
(int) envValue('REDIS_PORT', 6379)
);
} else {
$clusters = explode(',', envValue('REDIS_HOST', '127.0.0.1'));
$clusters = array_map('trim', $clusters);

$redis = new RedisCluster(null, $clusters, 1.5, 1.5);
}

if ($prefix) {
$redis->setOption(Redis::OPT_PREFIX, $app . ':'); // use custom prefix on all keys
}
Expand Down

0 comments on commit f0eaa11

Please sign in to comment.