Source of file Cache.php
Size: 4,619 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Proxy/Cache.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Proxy; use Bluz\Common\Exception\ComponentException; use Cache\Hierarchy\HierarchicalPoolInterface; use Cache\TagInterop\TaggableCacheItemPoolInterface as Instance; use Psr\Cache\InvalidArgumentException; /** * Proxy to Cache * * Example of usage * use Bluz\Proxy\Cache; * * if (!$result = Cache::get('some unique id')) { * $result = 2*2; * Cache::set('some unique id', $result); * } * * @package Bluz\Proxy * @author Anton Shevchuk * * @method static Instance|false getInstance() * * @method static bool delete($key) * @see Instance::deleteItem() * * @method static bool clear() * @see Instance::clear() */ final class Cache { use ProxyTrait; /** * No expiry TTL value */ const TTL_NO_EXPIRY = 0; /** * @var array */ private static $pools = []; /** * Init cache instance * * @return Instance|false * @throws ComponentException */ private static function initInstance() { $adapter = Config::getData('cache', 'adapter'); return self::getAdapter($adapter); } /** * Get Cache Adapter * * @param string $adapter * * @return Instance|false * @throws ComponentException */ public static function getAdapter($adapter) { $config = Config::getData('cache'); if ($config && $adapter && isset($config['enabled']) && $config['enabled']) { if (!isset($config['pools'][$adapter])) { throw new ComponentException("Class `Proxy\\Cache` required configuration for `$adapter` adapter"); } if (!isset(static::$pools[$adapter])) { static::$pools[$adapter] = $config['pools'][$adapter](); } return static::$pools[$adapter]; } return false; } /** * Get value of cache item * * @param string $key * * @return mixed */ public static function get($key) { if (!$cache = self::getInstance()) { return false; } $key = self::prepare($key); try { if ($cache->hasItem($key)) { $item = $cache->getItem($key); if ($item->isHit()) { return $item->get(); } } } catch (InvalidArgumentException $e) { // something going wrong Logger::error($e->getMessage()); } return false; } /** * Set value of cache item * * @param string $key * @param mixed $data * @param int $ttl * @param string[] $tags * * @return bool */ public static function set($key, $data, $ttl = self::TTL_NO_EXPIRY, $tags = []) { if (!$cache = self::getInstance()) { return false; } $key = self::prepare($key); try { $item = $cache->getItem($key); $item->set($data); if (self::TTL_NO_EXPIRY !== $ttl) { $item->expiresAfter($ttl); } if (!empty($tags)) { $item->setTags($tags); } return $cache->save($item); } catch (InvalidArgumentException $e) { // something going wrong Logger::error($e->getMessage()); } return false; } /** * Prepare key * * @param string $key * * @return string */ public static function prepare($key) { return str_replace(['-', '/', '\\', '@', ':'], '_', $key); } /** * Clear cache items by tag * * @see TaggableCacheItemPoolInterface::invalidateTag() * * @param string $tag * * @return bool */ public static function clearTag($tag) { if (self::getInstance() instanceof HierarchicalPoolInterface) { return self::getInstance()->invalidateTag($tag); } return false; } /** * Clear cache items by tags * * @see TaggableCacheItemPoolInterface::invalidateTags() * * @param array $tags * * @return bool */ public static function clearTags(array $tags) { if (self::getInstance() instanceof HierarchicalPoolInterface) { return self::getInstance()->invalidateTags($tags); } return false; } } |