diff options
author | 2019-04-29 20:12:43 +0200 | |
---|---|---|
committer | 2019-04-29 20:12:43 +0200 | |
commit | 21d3bf3b60bcf0e88165ef79c718b2efb1f5364c (patch) | |
tree | 21b49491bbf4a43d736877328c5100114ff18821 /caches/SQLiteCache.php | |
parent | 3b8f3da09d4d9ada1d78ad0826bcc867848127c6 (diff) | |
download | rss-bridge-21d3bf3b60bcf0e88165ef79c718b2efb1f5364c.tar.gz rss-bridge-21d3bf3b60bcf0e88165ef79c718b2efb1f5364c.tar.zst rss-bridge-21d3bf3b60bcf0e88165ef79c718b2efb1f5364c.zip |
caches: Refactor the API (#1060)
- For consistency, functions should always return null on non-existing data.
- WordPressPluginUpdateBridge appears to have used its own cache instance in the past. Obviously not used anymore.
- Since $key can be anything, the cache implementation must ensure to assign the related data reliably; most commonly by serializing and hashing the key in an appropriate way.
- Even though the default path for storage is perfectly fine, some people may want to use a different location. This is an example how a cache implementation is responsible for its requirements.
Diffstat (limited to 'caches/SQLiteCache.php')
-rw-r--r-- | caches/SQLiteCache.php | 60 |
1 files changed, 41 insertions, 19 deletions
diff --git a/caches/SQLiteCache.php b/caches/SQLiteCache.php index 5cbb3772..7d0f584f 100644 --- a/caches/SQLiteCache.php +++ b/caches/SQLiteCache.php @@ -3,16 +3,25 @@ * Cache based on SQLite 3 <https://www.sqlite.org> */ class SQLiteCache implements CacheInterface { - protected $path; - protected $param; + protected $scope; + protected $key; private $db = null; public function __construct() { - if (!extension_loaded('sqlite3')) + if (!extension_loaded('sqlite3')) { die('"sqlite3" extension not loaded. Please check "php.ini"'); + } - $file = PATH_CACHE . 'cache.sqlite'; + $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'); + } if (!is_file($file)) { $this->db = new SQLite3($file); @@ -39,10 +48,10 @@ class SQLiteCache implements CacheInterface { return null; } - public function saveData($datas){ + public function saveData($data){ $Qupdate = $this->db->prepare('INSERT OR REPLACE INTO storage (key, value, updated) VALUES (:key, :value, :updated)'); $Qupdate->bindValue(':key', $this->getCacheKey()); - $Qupdate->bindValue(':value', serialize($datas)); + $Qupdate->bindValue(':value', serialize($data)); $Qupdate->bindValue(':updated', time()); $Qupdate->execute(); @@ -60,40 +69,53 @@ class SQLiteCache implements CacheInterface { } } - return false; + return null; } - public function purgeCache($duration){ + public function purgeCache($seconds){ $Qdelete = $this->db->prepare('DELETE FROM storage WHERE updated < :expired'); - $Qdelete->bindValue(':expired', time() - $duration); + $Qdelete->bindValue(':expired', time() - $seconds); $Qdelete->execute(); } /** - * Set cache path + * Set scope * @return self */ - public function setPath($path){ - $this->path = $path; + public function setScope($scope){ + if(is_null($scope) || !is_string($scope)) { + throw new \Exception('The given scope is invalid!'); + } + + $this->scope = $scope; return $this; } /** - * Set HTTP GET parameters + * Set key * @return self */ - public function setParameters(array $param){ - $this->param = array_map('strtolower', $param); + 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; return $this; } //////////////////////////////////////////////////////////////////////////// - protected function getCacheKey(){ - if(is_null($this->param)) { - throw new \Exception('Call "setParameters" first!'); + private function getCacheKey(){ + if(is_null($this->key)) { + throw new \Exception('Call "setKey" first!'); } - return hash('sha1', $this->path . http_build_query($this->param), true); + return hash('sha1', $this->scope . $this->key, true); } } |