Rev 17002 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Cache;class CacheImpl implements CacheInterface{/**** @var \LeadersLinked\Cache\CacheInterface*/private static $_instance;/**** @var \Memcached*/private $cache;/**** @var string*/private $namespace;/**** @var int*/private $ttl;/**** @param Array $config*/private function __construct($config){$this->ttl = intval($config['leaderslinked.memcache.ttl'], 10);$this->namespace = strval($config['leaderslinked.memcache.namespace']);$host = strval($config['leaderslinked.memcache.host']);$port = intval($config['leaderslinked.memcache.port'], 10);$this->cache = new \Memcached();$this->cache->addserver($host, $port);}/**** @param array $config* @return \LeadersLinked\Cache\CacheInterface*/public static function getInstance($config){if(self::$_instance == null) {self::$_instance = new CacheImpl($config);}return self::$_instance;}/**** @param string $key* @param mixed $value* @param int $ttl*/public function setItem($key, $value, $ttl = 0){$ttl = $ttl ? $ttl : $this->ttl;$key = $this->namespace ? $this->namespace . '-' . $key : $key;if($this->hasItem($key)) {$this->cache->replace($key, serialize($value), time() + $ttl);} else {$this->cache->add($key, serialize($value), time() + $ttl);}}/**** @param string $key* @return boolean*/public function removeItem($key){if($this->hasItem($key)) {$key = $this->namespace ? $this->namespace . '-' . $key : $key;return $this->cache->delete($key);} else {return true;}}/**** {@inheritDoc}* @see \LeadersLinked\Cache\CacheInterface::getAll()*/public function getAll(){return $this->cache->fetchAll();}/**** {@inheritDoc}* @see \LeadersLinked\Cache\CacheInterface::hasItem()*/public function hasItem($key){$key = $this->namespace ? $this->namespace . '-' . $key : $key;$keys = $this->cache->getAllKeys();if ($keys === false) {return false;}return in_array($key, $keys, true);}/**** {@inheritDoc}* @see \LeadersLinked\Cache\CacheInterface::getItem()*/public function getItem($key){if($this->hasItem($key)) {$key = $this->namespace ? $this->namespace . '-' . $key : $key;$value = $this->cache->get($key);if(is_string($value)) {return unserialize($value);} else {return $value;}} else {return null;}}}