From f790de84bc4b6ef288186d08797f4138c1964eeb Mon Sep 17 00:00:00 2001 From: Thomas Alrek Date: Mon, 17 Jun 2019 14:11:26 +0200 Subject: [PATCH] Add docblocks to cache methods --- src/Netflex/Site/Cache.php | 133 +++++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 26 deletions(-) diff --git a/src/Netflex/Site/Cache.php b/src/Netflex/Site/Cache.php index a56cbeb..a5b9176 100644 --- a/src/Netflex/Site/Cache.php +++ b/src/Netflex/Site/Cache.php @@ -3,6 +3,7 @@ use NF; use Exception; +use ReflectionFunction; use Phpfastcache\CacheManager; use Phpfastcache\Drivers\Files\Config as FilesConfig; use Phpfastcache\Drivers\Memcached\Config as MemcachedConfig; @@ -10,12 +11,13 @@ class Cache { + /** @var string */ + private static $key; - private static $key; + /** @var \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface */ private static $cache; public function __construct() { - self::$key = NF::$sitename; $config = null; @@ -23,21 +25,24 @@ public function __construct() { switch ($driver) { case 'memcached': - $memcached_hostname = getenv("MEMCACHED_HOST") ? getenv("MEMCACHED_HOST") : '127.0.0.1'; - $memcached_port = getenv("MEMCACHED_PORT") ? intval(getenv("MEMCACHED_PORT")) : 11211; + $memcached_hostname = getenv('MEMCACHED_HOST') ? getenv('MEMCACHED_HOST') : '127.0.0.1'; + $memcached_port = getenv('MEMCACHED_PORT') ? intval(getenv('MEMCACHED_PORT')) : 11211; $config = new MemcachedConfig([ 'host' => $memcached_hostname, 'port' => $memcached_port, ]); + break; case 'files': if (!file_exists(NF::$cacheDir . 'cache/')) { mkdir(NF::$cacheDir. 'cache/', 0755, true); } + $config = new FilesConfig([ 'path' => NF::$cacheDir . 'cache/' ]); + break; default: throw new Exception('Invalid Cache driver'); @@ -46,68 +51,142 @@ public function __construct() { self::$cache = CacheManager::getInstance($driver, $config); } + /** + * Creates a prefixed cache key + * + * @param string $key + * @return string + */ public function getCacheKey($key) { return md5(self::$key . $key); } + /** + * This function is only an alias for fetch. + * + * @see fetch + * @param string $key + * @return mixed + */ public function get ($key) { return $this->fetch($key); } /** * This function is only an alias for save. + * * @see save - * @return void + * @return bool */ public function set($key, $value, $_=false, $ttl) { return $this->save($key, $value, $ttl, null); } + /** + * This function is only an alias for save. + * + * @see save + * @return bool + */ public function add ($key, $value, $_ = false, $ttl) { return $this->save($key, $value, $ttl, null); } + /** + * Fetches item from cache by $key + * + * @param string $key + * @return mixed + */ public function fetch($key) { $item = self::$cache->getItem(self::getCacheKey($key)); $item = unserialize($item->get()); return $item; } + /** + * Checks is $key exists in the cache + * + * @param string $key + * @return bool + */ public function has($key) { $item = self::$cache->getItem(self::getCacheKey($key)); return !is_null($item->get()); } + /** + * Stores an item in the cache + * + * @param string $key + * @param mixed $value + * @param int $ttl = 0 + * @param string $tag = null + * @return bool + */ public function save($key, $value, $ttl = 0, $tag = null) { $value = serialize($value); $item = self::$cache->getItem(self::getCacheKey($key)); $item->set($value)->expiresAfter($ttl); - self::$cache->save($item); + return self::$cache->save($item); } + /** + * Stores an array of items in the cache + * + * @param array $items + * @return void + */ public function saveMultiple(array $items) { foreach($items as $item) { $this->save($item['key'], $item['value'], $item['ttl'], $item['tag']); } } + /** + * Deletes the item from the cache + * + * @param string $key + * @return bool + */ public function delete($key) { $key = self::getCacheKey($key); - self::$cache->deleteItem($key); + return self::$cache->deleteItem($key); } + /** + * Deletes an array of items from the cache + * + * @param array $keys + * @return void + */ public function deleteMultiple(array $keys) { foreach($keys as $key) { $this->delete($key); } } + /** + * Deletes items by tag + * + * @param string|array $tag + * @return bool + */ public function deleteTag($tag) { - self::$cache->deleteItemsByTags($tags); + if (is_array($tag)) { + return self::$cache->deleteItemsByTags($tag); + } + + return self::$cache->deleteItemsByTag($tag); } + /** + * Purges all items from the cache + * + * @return bool + */ public function purge() { - self::$cache->clear(); + return self::$cache->clear(); } /** @@ -117,26 +196,28 @@ public function purge() { * @param $ttl int Cache time to live * @param $callback function A function that resolves the value if it is not already cached */ - public static function resolve($key, $ttl = 3600, $callback) { - if(self::$cache->has($key)) { + if (self::$cache->has($key)) { return self::$cache->get($key); + } + + $ttlValue = null; + $ttlFunction = function(int $value) use (&$ttlValue) { + $ttlValue = $value; + }; + + $args = (new ReflectionFunction($callback))->getNumberOfParameters(); + + if ($args == 1) { + $response = $callback($ttlFunction); + $ttlValue = is_int($ttlValue) ? max(1, $ttlValue) : NULL; + NF::debug($ttlValue, 'Cache->resolve(' . $key . ') dynamic duration'); } else { - $ttlValue = NULL; - $ttlFunction = function(int $value) use (&$ttlValue) { - $ttlValue = $value; - }; - $args = (new \ReflectionFunction($callback))->getNumberOfParameters(); - if($args == 1) { - $response = $callback($ttlFunction); - $ttlValue = is_int($ttlValue) ? max(1, $ttlValue) : NULL; - nf::debug($ttlValue, "Cache->resolve($key) dynamic duration"); - } else { - $response = $callback(); - } - self::$cache->set($key, $response, "_", $ttlValue ?? $ttl); - return self::$cache->get($key); + $response = $callback(); } + + self::$cache->set($key, $response, '_', $ttlValue ?? $ttl); + + return self::$cache->get($key); } } -}