1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-07-30 21:30:14 +02:00

refacor: improve cache interface (#3492)

* fix: proper typehint on setScope

* refactor: type hint setKey()

* typehint
This commit is contained in:
Dag
2023-07-06 15:10:30 +02:00
committed by GitHub
parent f8801d8cb3
commit caac7f572c
13 changed files with 56 additions and 220 deletions

View File

@@ -3,8 +3,8 @@
class FileCache implements CacheInterface
{
private array $config;
protected $scope;
protected $key;
protected string $scope;
protected string $key;
public function __construct(array $config = [])
{
@@ -39,13 +39,12 @@ class FileCache implements CacheInterface
return $data;
}
public function saveData($data)
public function saveData($data): void
{
$writeStream = file_put_contents($this->getCacheFile(), serialize($data));
if ($writeStream === false) {
throw new \Exception('The cache path is not writeable. You probably want: chown www-data:www-data cache');
}
return $this;
}
public function getTime(): ?int
@@ -63,7 +62,7 @@ class FileCache implements CacheInterface
return null;
}
public function purgeCache($seconds)
public function purgeCache(int $seconds): void
{
if (! $this->config['enable_purge']) {
return;
@@ -90,27 +89,14 @@ class FileCache implements CacheInterface
}
}
public function setScope($scope)
public function setScope(string $scope): void
{
if (!is_string($scope)) {
throw new \Exception('The given scope is invalid!');
}
$this->scope = $this->config['path'] . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
return $this;
}
public function setKey($key)
public function setKey(array $key): void
{
$key = json_encode($key);
if (!is_string($key)) {
throw new \Exception('The given key is invalid!');
}
$this->key = $key;
return $this;
$this->key = json_encode($key);
}
private function getScope()

View File

@@ -2,8 +2,8 @@
class MemcachedCache implements CacheInterface
{
private $scope;
private $key;
private string $scope;
private string $key;
private $conn;
private $expiration = 0;
private $time = null;
@@ -58,11 +58,11 @@ class MemcachedCache implements CacheInterface
return $result['data'];
}
public function saveData($datas)
public function saveData($data): void
{
$time = time();
$object_to_save = [
'data' => $datas,
'data' => $data,
'time' => $time,
];
$result = $this->conn->set($this->getCacheKey(), $object_to_save, $this->expiration);
@@ -72,8 +72,6 @@ class MemcachedCache implements CacheInterface
}
$this->time = $time;
return $this;
}
public function getTime(): ?int
@@ -84,32 +82,21 @@ class MemcachedCache implements CacheInterface
return $this->time;
}
public function purgeCache($duration)
public function purgeCache(int $seconds): void
{
// Note: does not purges cache right now
// Just sets cache expiration and leave cache purging for memcached itself
$this->expiration = $duration;
$this->expiration = $seconds;
}
public function setScope($scope)
public function setScope(string $scope): void
{
$this->scope = $scope;
return $this;
}
public function setKey($key)
public function setKey(array $key): void
{
if (!empty($key) && is_array($key)) {
$key = array_map('strtolower', $key);
}
$key = json_encode($key);
if (!is_string($key)) {
throw new \Exception('The given key is invalid!');
}
$this->key = $key;
return $this;
$this->key = json_encode($key);
}
private function getCacheKey()

View File

@@ -4,11 +4,11 @@ declare(strict_types=1);
class NullCache implements CacheInterface
{
public function setScope($scope)
public function setScope(string $scope): void
{
}
public function setKey($key)
public function setKey(array $key): void
{
}
@@ -16,7 +16,7 @@ class NullCache implements CacheInterface
{
}
public function saveData($data)
public function saveData($data): void
{
}
@@ -25,7 +25,7 @@ class NullCache implements CacheInterface
return null;
}
public function purgeCache($seconds)
public function purgeCache(int $seconds): void
{
}
}

View File

@@ -5,8 +5,8 @@
*/
class SQLiteCache implements CacheInterface
{
protected $scope;
protected $key;
protected string $scope;
protected string $key;
private $db = null;
@@ -59,15 +59,13 @@ class SQLiteCache implements CacheInterface
return null;
}
public function saveData($data)
public function saveData($data): void
{
$Qupdate = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
$Qupdate->bindValue(':key', $this->getCacheKey());
$Qupdate->bindValue(':value', serialize($data));
$Qupdate->bindValue(':updated', time());
$Qupdate->execute();
return $this;
}
public function getTime(): ?int
@@ -85,36 +83,21 @@ class SQLiteCache implements CacheInterface
return null;
}
public function purgeCache($seconds)
public function purgeCache(int $seconds): void
{
$Qdelete = $this->db->prepare('DELETE FROM storage WHERE updated < :expired');
$Qdelete->bindValue(':expired', time() - $seconds);
$Qdelete->execute();
}
public function setScope($scope)
public function setScope(string $scope): void
{
if (is_null($scope) || !is_string($scope)) {
throw new \Exception('The given scope is invalid!');
}
$this->scope = $scope;
return $this;
}
public function setKey($key)
public function setKey(array $key): void
{
if (!empty($key) && is_array($key)) {
$key = array_map('strtolower', $key);
}
$key = json_encode($key);
if (!is_string($key)) {
throw new \Exception('The given key is invalid!');
}
$this->key = $key;
return $this;
$this->key = json_encode($key);
}
private function getCacheKey()