forked from GithubBackups/vichan
Merge pull request #843 from Zankaria/redis-unix
Better unix socket support for redis
This commit is contained in:
commit
1e47667b9a
@ -8,9 +8,21 @@ class RedisCacheDriver implements CacheDriver {
|
|||||||
private string $prefix;
|
private string $prefix;
|
||||||
private \Redis $inner;
|
private \Redis $inner;
|
||||||
|
|
||||||
public function __construct(string $prefix, string $host, int $port, ?string $password, string $database) {
|
public function __construct(string $prefix, string $host, ?int $port, ?string $password, string $database) {
|
||||||
$this->inner = new \Redis();
|
$this->inner = new \Redis();
|
||||||
$this->inner->connect($host, $port);
|
if (str_starts_with($host, 'unix:') || str_starts_with($host, ':')) {
|
||||||
|
$ret = \explode(':', $host);
|
||||||
|
if (count($ret) < 2) {
|
||||||
|
throw new \RuntimeException("Invalid unix socket path $host");
|
||||||
|
}
|
||||||
|
// Unix socket.
|
||||||
|
$this->inner->connect($ret[1]);
|
||||||
|
} elseif ($port === null) {
|
||||||
|
$this->inner->connect($host);
|
||||||
|
} else {
|
||||||
|
// IP + port.
|
||||||
|
$this->inner->connect($host, $port);
|
||||||
|
}
|
||||||
if ($password) {
|
if ($password) {
|
||||||
$this->inner->auth($password);
|
$this->inner->auth($password);
|
||||||
}
|
}
|
||||||
|
10
inc/polyfill.php
Normal file
10
inc/polyfill.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// PHP 8.0
|
||||||
|
|
||||||
|
if (!function_exists('str_starts_with')) {
|
||||||
|
function str_starts_with(string $haystack, string $needle): bool {
|
||||||
|
// https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions#str_starts_with
|
||||||
|
return \strncmp($haystack, $needle, \strlen($needle)) === 0;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user