Merge pull request 'Fix memecached driver' (#123) from memcached-fix into config

Reviewed-on: leftypol/leftypol#123
This commit is contained in:
Zankaria 2025-05-29 17:07:23 -05:00
commit eed2b2986a
4 changed files with 69 additions and 14 deletions

View file

@ -0,0 +1,20 @@
<?php
namespace Vichan\Data\Driver;
trait CacheDriverTrait {
/**
* Tries to interpret the uri as a path to a unix socket.
*
* @param string $uri
* @return ?string The path to the socket, null if it cannot be interpreted as such.
*/
private static function asUnixSocketPath(string $uri): ?string {
if (str_starts_with($uri, 'unix:')) {
return \substr($uri, 5);
} elseif (str_starts_with($uri, ':')) {
return \substr($uri, 1);
}
return null;
}
}

View file

@ -5,9 +5,12 @@ defined('TINYBOARD') or exit;
class MemcachedCacheDriver implements CacheDriver {
use CacheDriverTrait;
private \Memcached $inner;
public function __construct(string $prefix, string $memcached_server) {
public function __construct(string $prefix, string $server_uri, int $server_port, int $server_weight) {
$this->inner = new \Memcached();
if (!$this->inner->setOption(\Memcached::OPT_BINARY_PROTOCOL, true)) {
throw new \RuntimeException('Unable to set the memcached protocol!');
@ -15,8 +18,33 @@ class MemcachedCacheDriver implements CacheDriver {
if (!$this->inner->setOption(\Memcached::OPT_PREFIX_KEY, $prefix)) {
throw new \RuntimeException('Unable to set the memcached prefix!');
}
if (!$this->inner->addServers($memcached_server)) {
throw new \RuntimeException('Unable to add the memcached server!');
$maybe_unix_path = self::asUnixSocketPath($server_uri);
$is_unix = $maybe_unix_path !== null;
if ($is_unix) {
$server_uri = $maybe_unix_path;
}
// Memcached keeps the server connections open across requests.
$current_servers = $this->inner->getServerList();
$found_in_curr = false;
foreach ($current_servers as $curr) {
// Ignore the port if the server is connected with a unix socket.
if ($curr['host'] === $server_uri && ($is_unix || $curr['port'] === $server_port)) {
$found_in_curr = true;
}
}
if (!$found_in_curr) {
if (!empty($current_servers)) {
if ($this->inner->resetServerList()) {
throw new \RuntimeException('Unable to reset the memcached server list!');
}
if ($this->inner->addServer($server_uri, $server_port, $server_weight)) {
throw new \RuntimeException('Unable to add memcached servers!');
}
}
}
}

View file

@ -5,18 +5,17 @@ defined('TINYBOARD') or exit;
class RedisCacheDriver implements CacheDriver {
use CacheDriverTrait;
private string $prefix;
private \Redis $inner;
public function __construct(string $prefix, string $host, ?int $port, ?string $password, int $database) {
$this->inner = new \Redis();
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]);
$maybe_unix = self::asUnixSocketPath($host);
if ($maybe_unix !== null) {
$this->inner->connect($maybe_unix);
} elseif ($port === null) {
$this->inner->connect($host);
} else {

View file

@ -15,10 +15,18 @@ class Cache {
switch ($config['cache']['enabled']) {
case 'memcached':
return new MemcachedCacheDriver(
$config['cache']['prefix'],
$config['cache']['memcached']
);
$prefix = $config['cache']['prefix'];
$uri = $config['cache']['memcached'][0];
$port = 0;
$weight = 0;
if (isset($config['cache']['memcached'][1]) && $config['cache']['memcached'][1] !== null) {
$port = \intval($config['cache']['memcached'][1]);
}
if (isset($config['cache']['memcached'][2]) && $config['cache']['memcached'][2] !== null) {
$weight = \intval($config['cache']['memcached'][2]);
}
return new MemcachedCacheDriver($prefix, $uri, $port, $weight);
case 'redis':
$port = $config['cache']['redis'][1];
$port = empty($port) ? null : intval($port);