From fe57edd9e87832dbd14ea8ffd2dc2f16ac1ceb6f Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 20 Sep 2015 12:38:35 -0400 Subject: Add abstract cache layer --- app/Core/Cache.php | 58 ---------------------------------- app/Core/Cache/Base.php | 38 +++++++++++++++++++++++ app/Core/Cache/CacheInterface.php | 45 +++++++++++++++++++++++++++ app/Core/Cache/MemoryCache.php | 65 +++++++++++++++++++++++++++++++++++++++ app/Core/MemoryCache.php | 32 ------------------- 5 files changed, 148 insertions(+), 90 deletions(-) delete mode 100644 app/Core/Cache.php create mode 100644 app/Core/Cache/Base.php create mode 100644 app/Core/Cache/CacheInterface.php create mode 100644 app/Core/Cache/MemoryCache.php delete mode 100644 app/Core/MemoryCache.php (limited to 'app/Core') diff --git a/app/Core/Cache.php b/app/Core/Cache.php deleted file mode 100644 index 670a76e0..00000000 --- a/app/Core/Cache.php +++ /dev/null @@ -1,58 +0,0 @@ -container = $container; - $this->init(); - } - - /** - * Proxy cache - * - * Note: Arguments must be scalar types - * - * @access public - * @param string $container Container name - * @param string $method Container method - * @return mixed - */ - public function proxy($container, $method) - { - $args = func_get_args(); - $key = 'proxy_'.implode('_', $args); - $result = $this->get($key); - - if ($result === null) { - $result = call_user_func_array(array($this->container[$container], $method), array_splice($args, 2)); - $this->set($key, $result); - } - - return $result; - } -} diff --git a/app/Core/Cache/Base.php b/app/Core/Cache/Base.php new file mode 100644 index 00000000..a16c12f0 --- /dev/null +++ b/app/Core/Cache/Base.php @@ -0,0 +1,38 @@ +get($key); + + if ($result === null) { + $result = call_user_func_array(array($class, $method), array_splice($args, 1)); + $this->set($key, $result); + } + + return $result; + } +} diff --git a/app/Core/Cache/CacheInterface.php b/app/Core/Cache/CacheInterface.php new file mode 100644 index 00000000..8675ef8e --- /dev/null +++ b/app/Core/Cache/CacheInterface.php @@ -0,0 +1,45 @@ +storage[$key] = $value; + } + + /** + * Fetch value from cache + * + * @access public + * @param string $key + * @return mixed Null when not found, cached value otherwise + */ + public function get($key) + { + return isset($this->storage[$key]) ? $this->storage[$key] : null; + } + + /** + * Clear all cache + * + * @access public + */ + public function flush() + { + $this->storage = array(); + } + + /** + * Remove cached value + * + * @access public + * @param string $key + */ + public function remove($key) + { + unset($this->storage[$key]); + } +} diff --git a/app/Core/MemoryCache.php b/app/Core/MemoryCache.php deleted file mode 100644 index f80a66ef..00000000 --- a/app/Core/MemoryCache.php +++ /dev/null @@ -1,32 +0,0 @@ -storage[$key] = $value; - } - - public function get($key) - { - return isset($this->storage[$key]) ? $this->storage[$key] : null; - } - - public function flush() - { - $this->storage = array(); - } - - public function remove($key) - { - unset($this->storage[$key]); - } -} -- cgit v1.2.3