From caead402048797e7f4557235116b504de8d80e13 Mon Sep 17 00:00:00 2001 From: Avior Date: Wed, 17 Apr 2019 21:14:52 +0200 Subject: [PATCH] Updated Cache to have Abstract method --- src/AdminPanel/Cache/AbstractCache.php | 87 ++++++++++++++++++++++++++ src/AdminPanel/Cache/FileCache.php | 58 +---------------- 2 files changed, 88 insertions(+), 57 deletions(-) create mode 100644 src/AdminPanel/Cache/AbstractCache.php diff --git a/src/AdminPanel/Cache/AbstractCache.php b/src/AdminPanel/Cache/AbstractCache.php new file mode 100644 index 0000000..585b695 --- /dev/null +++ b/src/AdminPanel/Cache/AbstractCache.php @@ -0,0 +1,87 @@ +checkKey($key)) { + throw new InvalidArgumentException("Invalid Key"); + } + $result[$key] = $this->get($key, $default); + } + return $result; + } + + public function setMultiple($values, $ttl = null) + { + if (!is_iterable($values)) { + throw new InvalidArgumentException('values are not iterable'); + } + foreach ($values as $key => $value) { + if (!$this->checkKey($key)) { + throw new InvalidArgumentException("Invalid Key"); + } + $tmp = $this->set($key, $value, $ttl); + if (!$tmp) { + return false; + } + } + return true; + } + + public function deleteMultiple($keys) + { + if (!is_iterable($keys)) { + throw new InvalidArgumentException('Keys are not iterable'); + } + foreach ($keys as $key) { + if (!$this->checkKey($key)) { + throw new InvalidArgumentException("Invalid Key"); + } + $tmp = $this->delete($key); + if (!$tmp) { + return false; + } + } + return true; + } + + + + protected function checkKey($key) + { + return preg_match('/^[A-Za-z0-9_.]{1,64}$/', $key); + } + + protected function getTTL($ttl) + { + if (is_int($ttl)) { + return $ttl; + } else { + return + ((($ttl->y * 365 + $ttl->m * 30 + $ttl->d + ) * 24 + $ttl->h + ) * 60 + $ttl->i + ) * 60 + $ttl->s; + } + } +} diff --git a/src/AdminPanel/Cache/FileCache.php b/src/AdminPanel/Cache/FileCache.php index 1552384..ef231b4 100644 --- a/src/AdminPanel/Cache/FileCache.php +++ b/src/AdminPanel/Cache/FileCache.php @@ -2,31 +2,11 @@ namespace AdminPanel\Cache; -use Psr\SimpleCache\CacheInterface; - -class FileCache implements CacheInterface +class FileCache extends AbstractCache { private $folder; private $ttl; - private function checkKey($key) - { - return preg_match('/^[A-Za-z0-9_.]{1,64}$/', $key); - } - - private function getTTL($ttl) - { - if (is_int($ttl)) { - return $ttl; - } else { - return - ((($ttl->y * 365 + $ttl->m * 30 + $ttl->d - ) * 24 + $ttl->h - ) * 60 + $ttl->i - ) * 60 + $ttl->s; - } - } - /** * Cache Constructor * @@ -88,42 +68,6 @@ class FileCache implements CacheInterface } } - public function getMultiple($keys, $default = null) - { - if (!is_iterable($keys)) { - throw new InvalidArgumentException('$keys isn\'t traversable'); - } - $result = array(); - foreach ($keys as $key) { - if (!$this->checkKey($key)) { - throw new InvalidArgumentException("a key in the array is invalid"); - } - $result[$key] = $this->get($key, $default); - } - return $result; - } - - public function setMultiple($values, $ttl = null) - { - if (!is_iterable($values)) { - throw new InvalidArgumentException('$values isn\'t traversable'); - } - foreach ($values as $key => $value) { - $tmp = $this->set($key, $value, $ttl); - if (!$tmp) { - return false; - } - } - return true; - } - - public function deleteMultiple($keys) - { - foreach ($keys as $key) { - $this->delete($key); - } - } - public function has($key) { return is_file($this->folder . DIRECTORY_SEPARATOR . $key);