mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-28 14:44:41 +02:00
fix: rewrite and improve caching (#3594)
This commit is contained in:
parent
a786bbd4e0
commit
4b9f6f7e53
45 changed files with 993 additions and 1169 deletions
52
caches/ArrayCache.php
Normal file
52
caches/ArrayCache.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
class ArrayCache implements CacheInterface
|
||||
{
|
||||
private array $data = [];
|
||||
|
||||
public function get(string $key, $default = null)
|
||||
{
|
||||
$item = $this->data[$key] ?? null;
|
||||
if (!$item) {
|
||||
return $default;
|
||||
}
|
||||
$expiration = $item['expiration'];
|
||||
if ($expiration === 0 || $expiration > time()) {
|
||||
return $item['value'];
|
||||
}
|
||||
$this->delete($key);
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function set(string $key, $value, int $ttl = null): void
|
||||
{
|
||||
$this->data[$key] = [
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
'expiration' => $ttl === null ? 0 : time() + $ttl,
|
||||
];
|
||||
}
|
||||
|
||||
public function delete(string $key): void
|
||||
{
|
||||
unset($this->data[$key]);
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
public function prune(): void
|
||||
{
|
||||
foreach ($this->data as $key => $item) {
|
||||
$expiration = $item['expiration'];
|
||||
if ($expiration === 0 || $expiration > time()) {
|
||||
continue;
|
||||
}
|
||||
$this->delete($key);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue