From bf327cdb24c8ed44fa304d030722a725568f6092 Mon Sep 17 00:00:00 2001 From: Szijarto Tamas Date: Mon, 11 May 2015 21:35:14 +0200 Subject: [PATCH] APC cache improvement - add cache key prefix - use success param at fetch --- src/Handlebars/Cache/APC.php | 42 ++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Handlebars/Cache/APC.php b/src/Handlebars/Cache/APC.php index 8671791..5c4469f 100755 --- a/src/Handlebars/Cache/APC.php +++ b/src/Handlebars/Cache/APC.php @@ -20,7 +20,7 @@ namespace Handlebars\Cache; use Handlebars\Cache; /** - * A dummy array cache + * A APC cache * * @category Xamin * @package Handlebars @@ -34,6 +34,21 @@ use Handlebars\Cache; class APC implements Cache { + /** + * @var string + */ + private $_prefix; + + /** + * Construct the APC cache. + * + * @param string|null $prefix optional key prefix, defaults to null + */ + public function __construct( $prefix = null ) + { + $this->_prefix = (string)$prefix; + } + /** * Get cache for $name if exist. * @@ -43,10 +58,11 @@ class APC implements Cache */ public function get($name) { - if (apc_exists($name)) { - return apc_fetch($name); - } - return false; + $success = null; + $result = apc_fetch($this->_getKey($name), $success); + + return $success ? $result : false; + } /** @@ -59,7 +75,7 @@ class APC implements Cache */ public function set($name, $value) { - apc_store($name, $value); + apc_store($this->_getKey($name), $value); } /** @@ -71,7 +87,19 @@ class APC implements Cache */ public function remove($name) { - apc_delete($name); + apc_delete($this->_getKey($name)); + } + + /** + * Gets the full cache key for a given cache item's + * + * @param string $name Name of the cache item + * + * @return string full cache key of cached item + */ + private function _getKey($name) + { + return $this->_prefix . ':' . $name; } }