fix: rewrite and improve caching (#3594)

This commit is contained in:
Dag 2023-09-10 21:50:15 +02:00 committed by GitHub
parent a786bbd4e0
commit 4b9f6f7e53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 993 additions and 1169 deletions

52
caches/ArrayCache.php Normal file
View 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);
}
}
}

View file

@ -1,13 +1,10 @@
<?php
/**
* @link https://www.php.net/manual/en/function.clearstatcache.php
*/
declare(strict_types=1);
class FileCache implements CacheInterface
{
private array $config;
protected string $scope;
protected string $key;
public function __construct(array $config = [])
{
@ -23,125 +20,89 @@ class FileCache implements CacheInterface
$this->config['path'] = rtrim($this->config['path'], '/') . '/';
}
public function getConfig()
public function get(string $key, $default = null)
{
return $this->config;
$cacheFile = $this->createCacheFile($key);
if (!file_exists($cacheFile)) {
return $default;
}
$item = unserialize(file_get_contents($cacheFile));
if ($item === false) {
Logger::warning(sprintf('Failed to unserialize: %s', $cacheFile));
$this->delete($key);
return $default;
}
$expiration = $item['expiration'];
if ($expiration === 0 || $expiration > time()) {
return $item['value'];
}
$this->delete($key);
return $default;
}
public function loadData(int $timeout = 86400)
public function set($key, $value, int $ttl = null): void
{
clearstatcache();
if (!file_exists($this->getCacheFile())) {
return null;
}
$modificationTime = filemtime($this->getCacheFile());
if (time() - $timeout < $modificationTime) {
$data = unserialize(file_get_contents($this->getCacheFile()));
if ($data === false) {
Logger::warning(sprintf('Failed to unserialize: %s', $this->getCacheFile()));
// Intentionally not throwing an exception
return null;
}
return $data;
}
// It's a good idea to delete the expired item here, but commented out atm
// unlink($this->getCacheFile());
return null;
}
public function saveData($data): void
{
$bytes = file_put_contents($this->getCacheFile(), serialize($data), LOCK_EX);
$item = [
'key' => $key,
'value' => $value,
'expiration' => $ttl === null ? 0 : time() + $ttl,
];
$cacheFile = $this->createCacheFile($key);
$bytes = file_put_contents($cacheFile, serialize($item), LOCK_EX);
if ($bytes === false) {
throw new \Exception(sprintf('Failed to write to: %s', $this->getCacheFile()));
// Consider just logging the error here
throw new \Exception(sprintf('Failed to write to: %s', $cacheFile));
}
}
public function getTime(): ?int
public function delete(string $key): void
{
clearstatcache();
$cacheFile = $this->getCacheFile();
if (file_exists($cacheFile)) {
$time = filemtime($cacheFile);
if ($time !== false) {
return $time;
}
return null;
}
return null;
unlink($this->createCacheFile($key));
}
public function purgeCache(int $timeout = 86400): void
public function clear(): void
{
foreach (scandir($this->config['path']) as $filename) {
$cacheFile = $this->config['path'] . $filename;
$excluded = ['.' => true, '..' => true, '.gitkeep' => true];
if (isset($excluded[$filename]) || !is_file($cacheFile)) {
continue;
}
unlink($cacheFile);
}
}
public function prune(): void
{
if (! $this->config['enable_purge']) {
return;
}
$cachePath = $this->getScope();
if (!file_exists($cachePath)) {
return;
}
$cacheIterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($cachePath),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($cacheIterator as $cacheFile) {
$basename = $cacheFile->getBasename();
$excluded = [
'.' => true,
'..' => true,
'.gitkeep' => true,
];
if (isset($excluded[$basename])) {
foreach (scandir($this->config['path']) as $filename) {
$cacheFile = $this->config['path'] . $filename;
$excluded = ['.' => true, '..' => true, '.gitkeep' => true];
if (isset($excluded[$filename]) || !is_file($cacheFile)) {
continue;
} elseif ($cacheFile->isFile()) {
$filepath = $cacheFile->getPathname();
if (filemtime($filepath) < time() - $timeout) {
// todo: sometimes this file doesn't exists
unlink($filepath);
}
}
}
}
public function setScope(string $scope): void
{
$this->scope = $this->config['path'] . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
}
public function setKey(array $key): void
{
$this->key = json_encode($key);
}
private function getScope()
{
if (is_null($this->scope)) {
throw new \Exception('Call "setScope" first!');
}
if (!is_dir($this->scope)) {
if (mkdir($this->scope, 0755, true) !== true) {
throw new \Exception('mkdir: Unable to create file cache folder');
$item = unserialize(file_get_contents($cacheFile));
if ($item === false) {
unlink($cacheFile);
continue;
}
$expiration = $item['expiration'];
if ($expiration === 0 || $expiration > time()) {
continue;
}
unlink($cacheFile);
}
return $this->scope;
}
private function getCacheFile()
private function createCacheFile(string $key): string
{
return $this->getScope() . $this->getCacheName();
return $this->config['path'] . hash('md5', $key) . '.cache';
}
private function getCacheName()
public function getConfig()
{
if (is_null($this->key)) {
throw new \Exception('Call "setKey" first!');
}
return hash('md5', $this->key) . '.cache';
return $this->config;
}
}

View file

@ -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
}
}

View file

@ -4,28 +4,24 @@ declare(strict_types=1);
class NullCache implements CacheInterface
{
public function setScope(string $scope): void
public function get(string $key, $default = null)
{
return $default;
}
public function set(string $key, $value, int $ttl = null): void
{
}
public function setKey(array $key): void
public function delete(string $key): void
{
}
public function loadData(int $timeout = 86400)
public function clear(): void
{
}
public function saveData($data): void
{
}
public function getTime(): ?int
{
return null;
}
public function purgeCache(int $timeout = 86400): void
public function prune(): void
{
}
}

View file

@ -1,10 +1,10 @@
<?php
declare(strict_types=1);
class SQLiteCache implements CacheInterface
{
private \SQLite3 $db;
private string $scope;
private string $key;
private array $config;
public function __construct(array $config)
@ -31,85 +31,77 @@ class SQLiteCache implements CacheInterface
$this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
}
$this->db->busyTimeout($config['timeout']);
// https://www.sqlite.org/pragma.html#pragma_journal_mode
$this->db->exec('PRAGMA journal_mode = wal');
// https://www.sqlite.org/pragma.html#pragma_synchronous
$this->db->exec('PRAGMA synchronous = NORMAL');
}
public function loadData(int $timeout = 86400)
public function get(string $key, $default = null)
{
$cacheKey = $this->createCacheKey($key);
$stmt = $this->db->prepare('SELECT value, updated FROM storage WHERE key = :key');
$stmt->bindValue(':key', $this->getCacheKey());
$stmt->bindValue(':key', $cacheKey);
$result = $stmt->execute();
if (!$result) {
return null;
return $default;
}
$row = $result->fetchArray(\SQLITE3_ASSOC);
if ($row === false) {
return null;
return $default;
}
$value = $row['value'];
$modificationTime = $row['updated'];
if (time() - $timeout < $modificationTime) {
$data = unserialize($value);
if ($data === false) {
Logger::error(sprintf("Failed to unserialize: '%s'", mb_substr($value, 0, 100)));
return null;
$expiration = $row['updated'];
if ($expiration === 0 || $expiration > time()) {
$blob = $row['value'];
$value = unserialize($blob);
if ($value === false) {
Logger::error(sprintf("Failed to unserialize: '%s'", mb_substr($blob, 0, 100)));
// delete?
return $default;
}
return $data;
return $value;
}
// It's a good idea to delete expired cache items.
// However I'm seeing lots of SQLITE_BUSY errors so commented out for now
// $stmt = $this->db->prepare('DELETE FROM storage WHERE key = :key');
// $stmt->bindValue(':key', $this->getCacheKey());
// $stmt->execute();
return null;
// delete?
return $default;
}
public function saveData($data): void
public function set(string $key, $value, int $ttl = null): void
{
$blob = serialize($data);
$cacheKey = $this->createCacheKey($key);
$blob = serialize($value);
$expiration = $ttl === null ? 0 : time() + $ttl;
$stmt = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
$stmt->bindValue(':key', $this->getCacheKey());
$stmt->bindValue(':key', $cacheKey);
$stmt->bindValue(':value', $blob, \SQLITE3_BLOB);
$stmt->bindValue(':updated', time());
$stmt->execute();
}
public function getTime(): ?int
{
$stmt = $this->db->prepare('SELECT updated FROM storage WHERE key = :key');
$stmt->bindValue(':key', $this->getCacheKey());
$stmt->bindValue(':updated', $expiration);
$result = $stmt->execute();
if ($result) {
$row = $result->fetchArray(\SQLITE3_ASSOC);
if ($row !== false) {
return $row['updated'];
}
}
return null;
// Unclear whether we should $result->finalize(); here?
}
public function purgeCache(int $timeout = 86400): void
public function delete(string $key): void
{
$key = $this->createCacheKey($key);
$stmt = $this->db->prepare('DELETE FROM storage WHERE key = :key');
$stmt->bindValue(':key', $key);
$result = $stmt->execute();
}
public function prune(): void
{
if (!$this->config['enable_purge']) {
return;
}
$stmt = $this->db->prepare('DELETE FROM storage WHERE updated < :expired');
$stmt->bindValue(':expired', time() - $timeout);
$stmt->execute();
$stmt = $this->db->prepare('DELETE FROM storage WHERE updated <= :now');
$stmt->bindValue(':now', time());
$result = $stmt->execute();
}
public function setScope(string $scope): void
public function clear(): void
{
$this->scope = $scope;
$this->db->query('DELETE FROM storage');
}
public function setKey(array $key): void
private function createCacheKey($key)
{
$this->key = json_encode($key);
}
private function getCacheKey()
{
return hash('sha1', $this->scope . $this->key, true);
return hash('sha1', $key, true);
}
}