mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-06 16:46:30 +02:00
fix: rewrite and improve caching (#3594)
This commit is contained in:
@@ -1,70 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
class MemcachedCache implements CacheInterface
|
||||
{
|
||||
private string $scope;
|
||||
private string $key;
|
||||
private $conn;
|
||||
private $expiration = 0;
|
||||
private \Memcached $conn;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(string $host, int $port)
|
||||
{
|
||||
if (!extension_loaded('memcached')) {
|
||||
throw new \Exception('"memcached" extension not loaded. Please check "php.ini"');
|
||||
$this->conn = new \Memcached();
|
||||
// This call does not actually connect to server yet
|
||||
if (!$this->conn->addServer($host, $port)) {
|
||||
throw new \Exception('Unable to add memcached server');
|
||||
}
|
||||
|
||||
$section = 'MemcachedCache';
|
||||
$host = Configuration::getConfig($section, 'host');
|
||||
$port = Configuration::getConfig($section, 'port');
|
||||
|
||||
if (empty($host) && empty($port)) {
|
||||
throw new \Exception('Configuration for ' . $section . ' missing.');
|
||||
}
|
||||
if (empty($host)) {
|
||||
throw new \Exception('"host" param is not set for ' . $section);
|
||||
}
|
||||
if (empty($port)) {
|
||||
throw new \Exception('"port" param is not set for ' . $section);
|
||||
}
|
||||
if (!ctype_digit($port)) {
|
||||
throw new \Exception('"port" param is invalid for ' . $section);
|
||||
}
|
||||
|
||||
$port = intval($port);
|
||||
|
||||
if ($port < 1 || $port > 65535) {
|
||||
throw new \Exception('"port" param is invalid for ' . $section);
|
||||
}
|
||||
|
||||
$conn = new \Memcached();
|
||||
$conn->addServer($host, $port) or returnServerError('Could not connect to memcached server');
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
public function loadData(int $timeout = 86400)
|
||||
public function get(string $key, $default = null)
|
||||
{
|
||||
$value = $this->conn->get($this->getCacheKey());
|
||||
$value = $this->conn->get($key);
|
||||
if ($value === false) {
|
||||
return null;
|
||||
return $default;
|
||||
}
|
||||
if (time() - $timeout < $value['time']) {
|
||||
return $value['data'];
|
||||
}
|
||||
return null;
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function saveData($data): void
|
||||
public function set(string $key, $value, $ttl = null): void
|
||||
{
|
||||
$value = [
|
||||
'data' => $data,
|
||||
'time' => time(),
|
||||
];
|
||||
$result = $this->conn->set($this->getCacheKey(), $value, $this->expiration);
|
||||
$expiration = $ttl === null ? 0 : time() + $ttl;
|
||||
$result = $this->conn->set($key, $value, $expiration);
|
||||
if ($result === false) {
|
||||
Logger::warning('Failed to store an item in memcached', [
|
||||
'scope' => $this->scope,
|
||||
'key' => $this->key,
|
||||
'expiration' => $this->expiration,
|
||||
'key' => $key,
|
||||
'code' => $this->conn->getLastErrorCode(),
|
||||
'message' => $this->conn->getLastErrorMessage(),
|
||||
'number' => $this->conn->getLastErrorErrno(),
|
||||
@@ -73,38 +39,18 @@ class MemcachedCache implements CacheInterface
|
||||
}
|
||||
}
|
||||
|
||||
public function getTime(): ?int
|
||||
public function delete(string $key): void
|
||||
{
|
||||
$value = $this->conn->get($this->getCacheKey());
|
||||
if ($value === false) {
|
||||
return null;
|
||||
}
|
||||
return $value['time'];
|
||||
$this->conn->delete($key);
|
||||
}
|
||||
|
||||
public function purgeCache(int $timeout = 86400): void
|
||||
public function clear(): void
|
||||
{
|
||||
// Note: does not purges cache right now
|
||||
// Just sets cache expiration and leave cache purging for memcached itself
|
||||
$this->expiration = $timeout;
|
||||
$this->conn->flush();
|
||||
}
|
||||
|
||||
public function setScope(string $scope): void
|
||||
public function prune(): void
|
||||
{
|
||||
$this->scope = $scope;
|
||||
}
|
||||
|
||||
public function setKey(array $key): void
|
||||
{
|
||||
$this->key = json_encode($key);
|
||||
}
|
||||
|
||||
private function getCacheKey()
|
||||
{
|
||||
if (is_null($this->key)) {
|
||||
throw new \Exception('Call "setKey" first!');
|
||||
}
|
||||
|
||||
return 'rss_bridge_cache_' . hash('md5', $this->scope . $this->key . 'A');
|
||||
// memcached manages pruning on its own
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user