[CACHE] Fix usage of the redis extension

This commit is contained in:
Hugo Sales 2020-07-17 22:58:00 +00:00 committed by Hugo Sales
parent c0a17af062
commit 09c2a762ef
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -22,6 +22,9 @@
namespace App\Core; namespace App\Core;
use Functional as F; use Functional as F;
use Redis;
use RedisCluster;
use Symfony\Component\Cache\Adapter;
use Symfony\Component\Cache\Adapter\ChainAdapter; use Symfony\Component\Cache\Adapter\ChainAdapter;
abstract class Cache abstract class Cache
@ -56,20 +59,22 @@ abstract class Cache
case 'redis': case 'redis':
// Redis can have multiple servers, but we want to take proper advantage of // Redis can have multiple servers, but we want to take proper advantage of
// redis, not just as a key value store, but using it's datastructures // redis, not just as a key value store, but using it's datastructures
$dsns = F\map(explode(',', $rest), $dsns = explode(',', $rest);
function ($r) use ($partial_to_dsn) {
// May be a Unix socket
return $r[0] == '/' ? $r : $partial_to_dsn($r);
});
if (count($dsns) === 1) { if (count($dsns) === 1) {
$class = \Redis; $class = Redis::class;
$r = new \Redis(array_pop($dsns)); $r = new Redis();
if ($rest[0] != '/' && strstr($rest, ':') != false) {
list($host, $port) = explode(':', $rest);
$r->pconnect($host, $port);
} else {
$r->pconnect($rest);
}
} else { } else {
$class = \RedisCluster; $class = RedisCluster::class; // true for persistent connection
$r = new \RedisCluster($dsns); $r = new RedisCluster(null, $dsns, null, null, true);
// Distribute reads randomly
$r->setOption($class::OPT_SLAVE_FAILOVER, $class::FAILOVER_DISTRIBUTE);
} }
// Distribute reads randomly
$r->setOption($class::OPT_SLAVE_FAILOVER, $class::FAILOVER_DISTRIBUTE);
// Improved serializer // Improved serializer
$r->setOption($class::OPT_SERIALIZER, $class::SERIALIZER_MSGPACK); $r->setOption($class::OPT_SERIALIZER, $class::SERIALIZER_MSGPACK);
// Persistent connection // Persistent connection