aboutsummaryrefslogtreecommitdiff
path: root/caches
diff options
context:
space:
mode:
Diffstat (limited to 'caches')
-rw-r--r--caches/FileCache.php72
-rw-r--r--caches/SQLiteCache.php60
2 files changed, 80 insertions, 52 deletions
diff --git a/caches/FileCache.php b/caches/FileCache.php
index 04d08a25..166ecdb5 100644
--- a/caches/FileCache.php
+++ b/caches/FileCache.php
@@ -3,20 +3,21 @@
* Cache with file system
*/
class FileCache implements CacheInterface {
-
protected $path;
- protected $param;
+ protected $key;
public function loadData(){
if(file_exists($this->getCacheFile())) {
return unserialize(file_get_contents($this->getCacheFile()));
}
+
+ return null;
}
- public function saveData($datas){
+ public function saveData($data){
// Notice: We use plain serialize() here to reduce memory footprint on
// large input data.
- $writeStream = file_put_contents($this->getCacheFile(), serialize($datas));
+ $writeStream = file_put_contents($this->getCacheFile(), serialize($data));
if($writeStream === false) {
throw new \Exception('Cannot write the cache... Do you have the right permissions ?');
@@ -29,13 +30,14 @@ class FileCache implements CacheInterface {
$cacheFile = $this->getCacheFile();
clearstatcache(false, $cacheFile);
if(file_exists($cacheFile)) {
- return filemtime($cacheFile);
+ $time = filemtime($cacheFile);
+ return ($time !== false) ? $time : null;
}
- return false;
+ return null;
}
- public function purgeCache($duration){
+ public function purgeCache($seconds){
$cachePath = $this->getPath();
if(file_exists($cachePath)) {
$cacheIterator = new RecursiveIteratorIterator(
@@ -47,7 +49,7 @@ class FileCache implements CacheInterface {
if(in_array($cacheFile->getBasename(), array('.', '..', '.gitkeep')))
continue;
elseif($cacheFile->isFile()) {
- if(filemtime($cacheFile->getPathname()) < time() - $duration)
+ if(filemtime($cacheFile->getPathname()) < time() - $seconds)
unlink($cacheFile->getPathname());
}
}
@@ -55,34 +57,34 @@ class FileCache implements CacheInterface {
}
/**
- * Set cache path
+ * Set scope
* @return self
*/
- public function setPath($path){
- if(is_null($path) || !is_string($path)) {
- throw new \Exception('The given path is invalid!');
+ public function setScope($scope){
+ if(is_null($scope) || !is_string($scope)) {
+ throw new \Exception('The given scope is invalid!');
}
- $this->path = $path;
-
- // Make sure path ends with '/' or '\'
- $lastchar = substr($this->path, -1, 1);
- if($lastchar !== '/' && $lastchar !== '\\')
- $this->path .= '/';
-
- if(!is_dir($this->path))
- mkdir($this->path, 0755, true);
+ $this->path = PATH_CACHE . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
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;
}
@@ -90,9 +92,15 @@ class FileCache implements CacheInterface {
* Return cache path (and create if not exist)
* @return string Cache path
*/
- protected function getPath(){
+ private function getPath(){
if(is_null($this->path)) {
- throw new \Exception('Call "setPath" first!');
+ throw new \Exception('Call "setScope" first!');
+ }
+
+ if(!is_dir($this->path)) {
+ if (mkdir($this->path, 0755, true) !== true) {
+ throw new \Exception('Unable to create ' . $this->path);
+ }
}
return $this->path;
@@ -102,7 +110,7 @@ class FileCache implements CacheInterface {
* Get the file name use for cache store
* @return string Path to the file cache
*/
- protected function getCacheFile(){
+ private function getCacheFile(){
return $this->getPath() . $this->getCacheName();
}
@@ -110,13 +118,11 @@ class FileCache implements CacheInterface {
* Determines file name for store the cache
* return string
*/
- protected function getCacheName(){
- if(is_null($this->param)) {
- throw new \Exception('Call "setParameters" first!');
+ private function getCacheName(){
+ if(is_null($this->key)) {
+ throw new \Exception('Call "setKey" first!');
}
- // Change character when making incompatible changes to prevent loading
- // errors due to incompatible file contents \|/
- return hash('md5', http_build_query($this->param) . 'A') . '.cache';
+ return hash('md5', $this->key) . '.cache';
}
}
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);
}
}