2019-02-24 14:04:27 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-09-11 00:35:40 +03:00
|
|
|
/**
|
|
|
|
* The storage table has a column `updated` which is incorrectly named.
|
|
|
|
* It should have been named `expiration` and the code treats it as an expiration date (in unix timestamp)
|
|
|
|
*/
|
2019-02-24 14:04:27 +03:00
|
|
|
class SQLiteCache implements CacheInterface
|
|
|
|
{
|
2023-09-21 23:05:55 +03:00
|
|
|
private Logger $logger;
|
2023-07-06 16:59:38 +03:00
|
|
|
private array $config;
|
2023-09-21 23:05:55 +03:00
|
|
|
private \SQLite3 $db;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-21 23:05:55 +03:00
|
|
|
public function __construct(
|
|
|
|
Logger $logger,
|
|
|
|
array $config
|
|
|
|
) {
|
|
|
|
$this->logger = $logger;
|
2023-07-06 16:59:38 +03:00
|
|
|
$default = [
|
|
|
|
'file' => null,
|
|
|
|
'timeout' => 5000,
|
|
|
|
'enable_purge' => true,
|
|
|
|
];
|
|
|
|
$config = array_merge($default, $config);
|
|
|
|
$this->config = $config;
|
|
|
|
|
|
|
|
if (!$config['file']) {
|
|
|
|
throw new \Exception('sqlite cache needs a file');
|
2019-04-29 21:12:43 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:59:38 +03:00
|
|
|
if (is_file($config['file'])) {
|
|
|
|
$this->db = new \SQLite3($config['file']);
|
2019-02-24 14:04:27 +03:00
|
|
|
$this->db->enableExceptions(true);
|
|
|
|
} else {
|
2023-07-06 16:59:38 +03:00
|
|
|
// Create the file and create sql schema
|
|
|
|
$this->db = new \SQLite3($config['file']);
|
2019-02-24 14:04:27 +03:00
|
|
|
$this->db->enableExceptions(true);
|
2023-07-06 16:59:38 +03:00
|
|
|
$this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
|
2023-09-24 21:53:07 +03:00
|
|
|
// Consider uncommenting this to add an index on expiration
|
|
|
|
//$this->db->exec('CREATE INDEX idx_storage_updated ON storage (updated)');
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2023-07-06 16:59:38 +03:00
|
|
|
$this->db->busyTimeout($config['timeout']);
|
2023-09-24 21:53:07 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
// https://www.sqlite.org/pragma.html#pragma_journal_mode
|
|
|
|
$this->db->exec('PRAGMA journal_mode = wal');
|
2023-09-24 21:53:07 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
// https://www.sqlite.org/pragma.html#pragma_synchronous
|
|
|
|
$this->db->exec('PRAGMA synchronous = NORMAL');
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
public function get(string $key, $default = null)
|
2019-02-24 14:04:27 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
$cacheKey = $this->createCacheKey($key);
|
2023-07-19 06:05:49 +03:00
|
|
|
$stmt = $this->db->prepare('SELECT value, updated FROM storage WHERE key = :key');
|
2023-09-10 22:50:15 +03:00
|
|
|
$stmt->bindValue(':key', $cacheKey);
|
2023-07-06 16:59:38 +03:00
|
|
|
$result = $stmt->execute();
|
2023-07-19 06:05:49 +03:00
|
|
|
if (!$result) {
|
2023-09-10 22:50:15 +03:00
|
|
|
return $default;
|
2023-07-19 06:05:49 +03:00
|
|
|
}
|
|
|
|
$row = $result->fetchArray(\SQLITE3_ASSOC);
|
|
|
|
if ($row === false) {
|
2023-09-10 22:50:15 +03:00
|
|
|
return $default;
|
2023-07-19 06:05:49 +03:00
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
$expiration = $row['updated'];
|
|
|
|
if ($expiration === 0 || $expiration > time()) {
|
|
|
|
$blob = $row['value'];
|
|
|
|
$value = unserialize($blob);
|
|
|
|
if ($value === false) {
|
2023-09-21 23:05:55 +03:00
|
|
|
$this->logger->error(sprintf("Failed to unserialize: '%s'", mb_substr($blob, 0, 100)));
|
2023-09-10 22:50:15 +03:00
|
|
|
// delete?
|
|
|
|
return $default;
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
return $value;
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
// delete?
|
|
|
|
return $default;
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2023-09-21 23:05:55 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
public function set(string $key, $value, int $ttl = null): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
$cacheKey = $this->createCacheKey($key);
|
|
|
|
$blob = serialize($value);
|
|
|
|
$expiration = $ttl === null ? 0 : time() + $ttl;
|
2023-07-06 16:59:38 +03:00
|
|
|
$stmt = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
|
2023-09-10 22:50:15 +03:00
|
|
|
$stmt->bindValue(':key', $cacheKey);
|
2023-07-20 20:11:13 +03:00
|
|
|
$stmt->bindValue(':value', $blob, \SQLITE3_BLOB);
|
2023-09-10 22:50:15 +03:00
|
|
|
$stmt->bindValue(':updated', $expiration);
|
|
|
|
$result = $stmt->execute();
|
|
|
|
// Unclear whether we should $result->finalize(); here?
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
public function delete(string $key): void
|
2019-02-24 14:04:27 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
$key = $this->createCacheKey($key);
|
|
|
|
$stmt = $this->db->prepare('DELETE FROM storage WHERE key = :key');
|
|
|
|
$stmt->bindValue(':key', $key);
|
2023-07-06 16:59:38 +03:00
|
|
|
$result = $stmt->execute();
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
public function prune(): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-07-06 16:59:38 +03:00
|
|
|
if (!$this->config['enable_purge']) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-10 22:50:15 +03:00
|
|
|
$stmt = $this->db->prepare('DELETE FROM storage WHERE updated <= :now');
|
|
|
|
$stmt->bindValue(':now', time());
|
|
|
|
$result = $stmt->execute();
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
public function clear(): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
$this->db->query('DELETE FROM storage');
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-10 22:50:15 +03:00
|
|
|
private function createCacheKey($key)
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
return hash('sha1', $key, true);
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
|
|
|
}
|