2019-02-24 12:04:27 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Cache based on SQLite 3 <https://www.sqlite.org>
|
|
|
|
*/
|
|
|
|
class SQLiteCache implements CacheInterface {
|
2019-04-29 20:12:43 +02:00
|
|
|
protected $scope;
|
|
|
|
protected $key;
|
2019-02-24 12:04:27 +01:00
|
|
|
|
|
|
|
private $db = null;
|
|
|
|
|
|
|
|
public function __construct() {
|
2019-04-29 20:12:43 +02:00
|
|
|
if (!extension_loaded('sqlite3')) {
|
2019-03-02 19:33:33 +01:00
|
|
|
die('"sqlite3" extension not loaded. Please check "php.ini"');
|
2019-04-29 20:12:43 +02:00
|
|
|
}
|
2019-03-02 19:33:33 +01:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
$file = Configuration::getConfig(get_called_class(), 'file');
|
|
|
|
if (empty($file)) {
|
|
|
|
die('Configuration for ' . get_called_class() . ' missing. Please check your config.ini.php');
|
|
|
|
}
|
|
|
|
if (dirname($file) == '.') {
|
|
|
|
$file = PATH_CACHE . $file;
|
|
|
|
} elseif (!is_dir(dirname($file))) {
|
|
|
|
die('Invalid configuration for ' . get_called_class() . '. Please check your config.ini.php');
|
|
|
|
}
|
2019-02-24 12:04:27 +01:00
|
|
|
|
|
|
|
if (!is_file($file)) {
|
|
|
|
$this->db = new SQLite3($file);
|
|
|
|
$this->db->enableExceptions(true);
|
|
|
|
$this->db->exec("CREATE TABLE storage ('key' BLOB PRIMARY KEY, 'value' BLOB, 'updated' INTEGER)");
|
|
|
|
} else {
|
|
|
|
$this->db = new SQLite3($file);
|
|
|
|
$this->db->enableExceptions(true);
|
|
|
|
}
|
|
|
|
$this->db->busyTimeout(5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadData(){
|
|
|
|
$Qselect = $this->db->prepare('SELECT value FROM storage WHERE key = :key');
|
|
|
|
$Qselect->bindValue(':key', $this->getCacheKey());
|
|
|
|
$result = $Qselect->execute();
|
|
|
|
if ($result instanceof SQLite3Result) {
|
|
|
|
$data = $result->fetchArray(SQLITE3_ASSOC);
|
|
|
|
if (isset($data['value'])) {
|
|
|
|
return unserialize($data['value']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
public function saveData($data){
|
2019-02-24 12:04:27 +01:00
|
|
|
$Qupdate = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)');
|
|
|
|
$Qupdate->bindValue(':key', $this->getCacheKey());
|
2019-04-29 20:12:43 +02:00
|
|
|
$Qupdate->bindValue(':value', serialize($data));
|
2019-02-24 12:04:27 +01:00
|
|
|
$Qupdate->bindValue(':updated', time());
|
|
|
|
$Qupdate->execute();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTime(){
|
|
|
|
$Qselect = $this->db->prepare('SELECT updated FROM storage WHERE key = :key');
|
|
|
|
$Qselect->bindValue(':key', $this->getCacheKey());
|
|
|
|
$result = $Qselect->execute();
|
|
|
|
if ($result instanceof SQLite3Result) {
|
|
|
|
$data = $result->fetchArray(SQLITE3_ASSOC);
|
|
|
|
if (isset($data['updated'])) {
|
|
|
|
return $data['updated'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
return null;
|
2019-02-24 12:04:27 +01:00
|
|
|
}
|
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
public function purgeCache($seconds){
|
2019-02-24 12:04:27 +01:00
|
|
|
$Qdelete = $this->db->prepare('DELETE FROM storage WHERE updated < :expired');
|
2019-04-29 20:12:43 +02:00
|
|
|
$Qdelete->bindValue(':expired', time() - $seconds);
|
2019-02-24 12:04:27 +01:00
|
|
|
$Qdelete->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-29 20:12:43 +02:00
|
|
|
* Set scope
|
2019-02-24 12:04:27 +01:00
|
|
|
* @return self
|
|
|
|
*/
|
2019-04-29 20:12:43 +02:00
|
|
|
public function setScope($scope){
|
|
|
|
if(is_null($scope) || !is_string($scope)) {
|
|
|
|
throw new \Exception('The given scope is invalid!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->scope = $scope;
|
2019-02-24 12:04:27 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-29 20:12:43 +02:00
|
|
|
* Set key
|
2019-02-24 12:04:27 +01:00
|
|
|
* @return self
|
|
|
|
*/
|
2019-04-29 20:12:43 +02:00
|
|
|
public function setKey($key){
|
|
|
|
if (!empty($key) && is_array($key)) {
|
|
|
|
$key = array_map('strtolower', $key);
|
|
|
|
}
|
|
|
|
$key = json_encode($key);
|
|
|
|
|
|
|
|
if (!is_string($key)) {
|
|
|
|
throw new \Exception('The given key is invalid!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->key = $key;
|
2019-02-24 12:04:27 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
private function getCacheKey(){
|
|
|
|
if(is_null($this->key)) {
|
|
|
|
throw new \Exception('Call "setKey" first!');
|
2019-02-24 12:04:27 +01:00
|
|
|
}
|
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
return hash('sha1', $this->scope . $this->key, true);
|
2019-02-24 12:04:27 +01:00
|
|
|
}
|
|
|
|
}
|