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
|
|
|
/**
|
|
|
|
* Cache based on SQLite 3 <https://www.sqlite.org>
|
|
|
|
*/
|
|
|
|
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)");
|
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) {
|
2022-08-06 23:46:28 +03:00
|
|
|
$data = $result->fetchArray(\SQLITE3_ASSOC);
|
2019-02-24 14:04:27 +03:00
|
|
|
if (isset($data['value'])) {
|
|
|
|
return unserialize($data['value']);
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
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-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());
|
|
|
|
$stmt->bindValue(':value', serialize($data));
|
|
|
|
$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) {
|
|
|
|
$data = $result->fetchArray(\SQLITE3_ASSOC);
|
2019-02-24 14:04:27 +03:00
|
|
|
if (isset($data['updated'])) {
|
|
|
|
return $data['updated'];
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +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
|
|
|
}
|
|
|
|
}
|