[core] parse CACHE_DSN using parse_url, and support username / password for redis, fixes #1000

This commit is contained in:
Shish
2024-01-07 23:28:15 +00:00
parent 8a82a3348f
commit 63b479592a

View File

@ -102,26 +102,28 @@ class EventTracingCache implements CacheInterface
function loadCache(?string $dsn): CacheInterface function loadCache(?string $dsn): CacheInterface
{ {
$matches = [];
$c = null; $c = null;
if ($dsn && preg_match("#(.*)://(.*)#", $dsn, $matches) && !isset($_GET['DISABLE_CACHE'])) { if ($dsn && !isset($_GET['DISABLE_CACHE'])) {
if ($matches[1] == "memcached" || $matches[1] == "memcache") { $url = parse_url($dsn);
$hp = explode(":", $matches[2]); if($url) {
if ($url['scheme'] == "memcached" || $url['scheme'] == "memcache") {
$memcache = new \Memcached(); $memcache = new \Memcached();
$memcache->addServer($hp[0], (int)$hp[1]); $memcache->addServer($url['host'], $url['port']);
$c = new \Sabre\Cache\Memcached($memcache); $c = new \Sabre\Cache\Memcached($memcache);
} elseif ($matches[1] == "apc") { } elseif ($url['scheme'] == "apc") {
$c = new \Sabre\Cache\Apcu(); $c = new \Sabre\Cache\Apcu();
} elseif ($matches[1] == "redis") { } elseif ($url['scheme'] == "redis") {
$hp = explode(":", $matches[2]);
$redis = new \Predis\Client([ $redis = new \Predis\Client([
'scheme' => 'tcp', 'scheme' => 'tcp',
'host' => $hp[0], 'host' => $url['host'],
'port' => (int)$hp[1] 'port' => $url['port'],
'username' => $url['user'],
'password' => $url['pass'],
], ['prefix' => 'shm:']); ], ['prefix' => 'shm:']);
$c = new \Naroga\RedisCache\Redis($redis); $c = new \Naroga\RedisCache\Redis($redis);
} }
} }
}
if(is_null($c)) { if(is_null($c)) {
$c = new \Sabre\Cache\Memory(); $c = new \Sabre\Cache\Memory();
} }