1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-23 05:49:44 +01:00

Merge pull request #843 from Zankaria/redis-unix

Better unix socket support for redis
This commit is contained in:
Lorenzo Yario 2024-11-26 16:27:09 -08:00 committed by GitHub
commit 1e47667b9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 2 deletions

View File

@ -8,9 +8,21 @@ class RedisCacheDriver implements CacheDriver {
private string $prefix;
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->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) {
$this->inner->auth($password);
}

10
inc/polyfill.php Normal file
View 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;
}
}