2019-02-24 14:04:27 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-24 14:04:27 +03:00
|
|
|
class SQLiteCache implements CacheInterface
|
|
|
|
{
|
2023-07-06 16:59:38 +03:00
|
|
|
private \SQLite3 $db;
|
|
|
|
private string $scope;
|
|
|
|
private string $key;
|
|
|
|
private array $config;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:59:38 +03:00
|
|
|
public function __construct(array $config)
|
2019-02-24 14:04:27 +03:00
|
|
|
{
|
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-07-15 23:12:16 +03:00
|
|
|
$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']);
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-24 14:04:27 +03:00
|
|
|
public function loadData()
|
|
|
|
{
|
2023-07-06 16:59:38 +03:00
|
|
|
$stmt = $this->db->prepare('SELECT value FROM storage WHERE key = :key');
|
|
|
|
$stmt->bindValue(':key', $this->getCacheKey());
|
|
|
|
$result = $stmt->execute();
|
|
|
|
if ($result) {
|
2023-07-15 23:12:16 +03:00
|
|
|
$row = $result->fetchArray(\SQLITE3_ASSOC);
|
2023-07-16 06:29:56 +03:00
|
|
|
if ($row !== false) {
|
|
|
|
$blob = $row['value'];
|
|
|
|
$data = unserialize($blob);
|
|
|
|
if ($data !== false) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
Logger::error(sprintf("Failed to unserialize: '%s'", mb_substr($blob, 0, 100)));
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:10:30 +03:00
|
|
|
public function saveData($data): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-07-15 23:12:16 +03:00
|
|
|
$blob = serialize($data);
|
|
|
|
|
2023-07-06 16:59:38 +03:00
|
|
|
$stmt = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
|
|
|
|
$stmt->bindValue(':key', $this->getCacheKey());
|
2023-07-15 23:12:16 +03:00
|
|
|
$stmt->bindValue(':value', $blob);
|
2023-07-06 16:59:38 +03:00
|
|
|
$stmt->bindValue(':updated', time());
|
|
|
|
$stmt->execute();
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-05 18:37:21 +03:00
|
|
|
public function getTime(): ?int
|
2019-02-24 14:04:27 +03:00
|
|
|
{
|
2023-07-06 16:59:38 +03:00
|
|
|
$stmt = $this->db->prepare('SELECT updated FROM storage WHERE key = :key');
|
|
|
|
$stmt->bindValue(':key', $this->getCacheKey());
|
|
|
|
$result = $stmt->execute();
|
|
|
|
if ($result) {
|
2023-07-15 23:12:16 +03:00
|
|
|
$row = $result->fetchArray(\SQLITE3_ASSOC);
|
2023-07-16 06:29:56 +03:00
|
|
|
if ($row !== false) {
|
|
|
|
return $row['updated'];
|
|
|
|
}
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2019-04-29 21:12:43 +03:00
|
|
|
return null;
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:10:30 +03:00
|
|
|
public function purgeCache(int $seconds): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-07-06 16:59:38 +03:00
|
|
|
if (!$this->config['enable_purge']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$stmt = $this->db->prepare('DELETE FROM storage WHERE updated < :expired');
|
|
|
|
$stmt->bindValue(':expired', time() - $seconds);
|
|
|
|
$stmt->execute();
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:10:30 +03:00
|
|
|
public function setScope(string $scope): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
|
|
|
$this->scope = $scope;
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:10:30 +03:00
|
|
|
public function setKey(array $key): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-07-06 16:10:30 +03:00
|
|
|
$this->key = json_encode($key);
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
private function getCacheKey()
|
|
|
|
{
|
|
|
|
return hash('sha1', $this->scope . $this->key, true);
|
2019-02-24 14:04:27 +03:00
|
|
|
}
|
|
|
|
}
|