/** * {@inheritdoc} */ public function has($name) { if ($this->driver->get($name) === false) { return $this->driver->getResultCode() != \Memcached::RES_NOTFOUND; } return true; }
/** * Retrieve an item from the cache by key. * * @param string $key * @return mixed */ public function get($key) { $value = $this->memcached->get($this->prefix . $key); if ($this->memcached->getResultCode() == 0) { return $value; } }
/** * {@inheritdoc} */ public function exists() { if (false === $this->memcached->get($this->storeId)) { return MemcachedClient::RES_SUCCESS === $this->memcached->getResultCode(); } return true; }
/** * @group memcached */ public function testInvalidate() { $mc = new MemcachedCacheBackend(); $mc->addServer(new MemcachedServer(self::TEST_MEMCACHE_SERVER)); $mc->invalidate('test'); $this->assertFalse($this->memcache->get('test')); $this->assertEquals(\Memcached::RES_NOTFOUND, $this->memcache->getResultCode()); }
/** * @param string $key * @return mixed|null */ public function get($key) { $result = $this->memcache->get($key); if ($this->memcache->getResultCode() === \Memcached::RES_NOTFOUND) { return NULL; } return $result; }
/** * Get item * @param string $namespace * @param string $key * @return mixed stored item */ public function get($namespace, $key) { $store_key = $this->getStoreKey($namespace, $key); $value = $this->connect->get($store_key); if ($this->connect->getResultCode() == \Memcached::RES_NOTFOUND) { return null; } return $value; }
public function get($id) { $flags = null; $result = @$this->memcached->get($id); if (false === $result && \Memcached::RES_NOTFOUND == $this->memcached->getResultCode()) { return null; } return $result; }
public function delete($key) { $this->memcachedObj->delete($key); $resultCode = $this->memcachedObj->getResultCode(); if ($resultCode != \Memcached::RES_SUCCESS && $resultCode != \Memcached::RES_NOTFOUND) { throw new StorageException('[STORAGE] "' . $this->getStorageName() . '::delete" failed for key "' . $key . '"!' . ' StorageRespCode: ' . $resultCode); } unset($this->casArray[$key]); return $resultCode; }
/** * Responsible for retrieving the authentication token from which persistent storage is in use. * * @return AccessToken|void */ public function retrieve($hash) { $cached = $this->memcached->get($this->prefix . $hash); if ($this->memcached->getResultCode() === \Memcached::RES_SUCCESS) { $json = json_decode($cached, true); if (isset($json['token'], $json['expires'])) { return new AccessToken($json['token'], DateTime::createFromFormat('U', $json['expires'])); } } }
/** * Checks the cache for the presence of a given key. * * Memcached does not have a built-in method to check if a key is cached, * so internally this method just calls the load() function and checks the * result code via Memcached::getResultCode(). * * @param string $key The key to check. * @return boolean TRUE if the requested key is cached, FALSE otherwise. */ public function isCached($key) { if (isset($this->cache)) { $result = $this->load($key); if ($this->cache->getResultCode() == Memcached::RES_SUCCESS) { return true; } } return false; }
/** * Retrieve multiple items from the cache by key. * * Items not found in the cache will have a null value. * * @param array $keys * @return array */ public function many(array $keys) { $prefixedKeys = array_map(function ($key) { return $this->prefix . $key; }, $keys); $values = $this->memcached->getMulti($prefixedKeys, null, Memcached::GET_PRESERVE_ORDER); if ($this->memcached->getResultCode() != 0) { return array_fill_keys($keys, null); } return array_combine($keys, $values); }
public function collect() { $newValue = $this->memcached->increment($this->key); if (false !== $newValue) { return $this; } if (\Memcached::RES_NOTFOUND !== $this->memcached->getResultCode()) { throw new \Exception('Error collecting value'); } $this->memcached->set($this->key, 1, time() + $this->timeInterval); }
/** * Gets item from Memcached. * * @param string $key under which the item is stored * * @return mixed * @throws \iveeCrest\Exceptions\KeyNotFoundInCacheException if key is not found */ public function getItem($key) { $item = $this->memcached->get(md5($this->uniqId . '_' . $key)); if ($this->memcached->getResultCode() == \Memcached::RES_NOTFOUND) { $exceptionClass = Config::getIveeClassName('KeyNotFoundInCacheException'); throw new $exceptionClass("Key not found in memcached."); } //count memcached hit $this->hits++; return $item; }
/** * @param string $name * @param mixed $defaultValue * @return string */ public function read($name, $defaultValue = null) { if (!$this->memcached instanceof \Memcached) { $this->connect(); } $result = $this->memcached->get($name); if ($result === false && $this->memcached->getResultCode() === \Memcached::RES_NOTFOUND) { return $defaultValue; } else { return $result; } }
/** * @return bool */ public function check() { if (array_key_exists($this->key, static::$cache)) { return true; } $value = $this->memcached->get($this->key); if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) { static::$cache[$this->key] = $value; return true; } return false; }
public function limitRate($key) { $cas = null; do { $info = $this->client->get($key, null, $cas); if (!$info) { return false; } $info['calls']++; $this->client->cas($cas, $key, $info); } while ($this->client->getResultCode() != \Memcached::RES_SUCCESS); return $this->getRateInfo($key); }
/** * @param string $key the key to retrieve. * @param array $dependencies table dependencies. * @throws \Exception Throws \Exceptions if the cache node could not be connected or the key is not set. * @return mixed */ public static function get($key, array $dependencies = ['global']) { if (self::$current == null) { self::connect(_ini::get('memcached', 'instance'), _ini::get('memcached', 'server'), _ini::get('memcached', 'port')); } $key = self::get_key($key, $dependencies); if (!($res = self::$current->get($key))) { if (self::$current->getResultCode() != \Memcached::RES_SUCCESS) { throw new \Exception('Cache key not set, this is common to distinguish from null values', self::ERROR_RETRIEVE); } } return $res; }
/** * {@inheritDoc} */ protected function _execute(\Closure $action, $operation, $key_or_keys, $mutable = false, $value = null) { $result = parent::_execute($action, $operation, $key_or_keys, $mutable, $value); // Adapter connection itself will only report correctly when not currently buffering results if (!$this->isBuffering()) { $code = $this->_connection->getResultCode(); if ($code !== \Memcached::RES_SUCCESS) { $this->_handleFailure($this->_connection->getResultMessage(), null, null, $code); } } // if !isBuffering return $result; }
/** * @param string $key * @param bool $found * @return mixed The value retrieved. Null if not found. */ public function get($key, &$found = null) { if (!$this->memcached) { throw new CacheException('No memcached defined.'); } $var = $this->memcached->get($key); if ($this->memcached->getResultCode() === Memcached::RES_NOTFOUND) { $var = null; $found = false; } else { $found = true; } return $var; }
/** * Retrieve an item from the cache by key. * * @param $key * @param bool $strict * @param null $default * * @return mixed|null * * @throws CacheMissException * @throws \Exception */ public function get($key, $strict = true, $default = null) { $value = $this->memcached->get($key); $resultCode = $this->memcached->getResultCode(); if ($resultCode == \Memcached::RES_SUCCESS) { return $value; } if (\Memcached::RES_NOTFOUND == $resultCode) { if ($strict) { throw new CacheMissException($key); } return $default; } throw new \Exception('Memcached failed with result code ' . $resultCode); }
/** * Flood protection with Memcached * * @param string $misprintHash * @throws Exception if report is flood-positive * @return boolean */ protected function floodProtect($misprintHash) { if (!$this->memcached) { return false; } $ip = $this->getIP(); if ($ip !== false) { $mcIpHash = 'newrphus:byIP:' . md5($ip); $attemptsCount = $this->memcached->get($mcIpHash); if ($this->memcached->getResultCode() === 0) { if ($attemptsCount > $this->attemptsThreshold) { throw new Exception("Too many attempts", 429); } $this->memcached->increment($mcIpHash); } else { $this->memcached->set($mcIpHash, 1, 300); } } $mcTextHash = 'newrphus:byText:' . $misprintHash; $this->memcached->get($mcTextHash); if ($this->memcached->getResultCode() === 0) { throw new Exception("This misprint already was sent", 202); } $this->memcached->set($mcTextHash, true, 300); return true; }
public static function factory(Configuration $conf) { switch (strtolower($conf['session-server']['type'])) { case 'memcache': $memcache = new \Memcache(); if (!@$memcache->connect($conf['session-server']['host'], $conf['session-server']['port'])) { throw new RuntimeException(sprintf('Unable to connect to memcache server at %s:%s', $conf['session-server']['host'], $conf['session-server']['port'])); } return new MemcacheSessionHandler($memcache); break; case 'memcached': $memcached = new \Memcached(); if (!@$memcached->addServer($conf['session-server']['host'], $conf['session-server']['port'])) { throw new RuntimeException(sprintf('Unable to connect to memcached server at %s:%s', $conf['session-server']['host'], $conf['session-server']['port'])); } $memcached->getVersion(); if (\Memcached::RES_SUCCESS !== $memcached->getResultCode()) { throw new RuntimeException(sprintf('Unable to connect to memcached server at %s:%s', $conf['session-server']['host'], $conf['session-server']['port'])); } return new MemcachedSessionHandler($memcached); break; case 'mongo': case 'pdo': throw new RuntimeException(sprintf('Session handler %s is not yet supported', $conf['session-server']['type'])); break; default: throw new RuntimeException(sprintf('Session handler %s is not a valid type', $conf['session-server']['type'])); break; } }
public function __construct($host, $port, $id = 'story_pool') { parent::__construct($id); if (!parent::addServer($host, $port)) { throw new Exception("Could not connect memcached server {$host}:{$port}." . "\n" . 'Error code: ' . parent::getResultCode() . "\n" . ' Error message: ' . parent::getResultMessage()); } parent::setOption(Memcached::OPT_BINARY_PROTOCOL, true); }
/** * Read some data stored in session. * Expects a session id. * Must return a string (empty if no data could have been read). * @param string $sessionId * @return string */ public function read($sessionId = '') { $data = $this->_memcached->get($this->_key($sessionId)); if ($this->_memcached->getResultCode() === \Memcached::RES_SUCCESS) { return $data; } return ''; }
/** * Checks if a cache entry with the specified identifier exists. * * @param string $entryIdentifier An identifier specifying the cache entry * @return bool TRUE if such an entry exists, FALSE if not * @api */ public function has($entryIdentifier) { if ($this->usedPeclModule === 'memcache') { return $this->memcache->get($this->identifierPrefix . $entryIdentifier) !== false; } // pecl-memcached supports storing literal FALSE $this->memcache->get($this->identifierPrefix . $entryIdentifier); return $this->memcache->getResultCode() !== \Memcached::RES_NOTFOUND; }
/** * @throws \Exception */ private function verifyReturnCode() { $code = self::$cache->getResultCode(); if ($code === \Memcached::RES_SUCCESS) { return; } $message = self::$cache->getResultMessage(); throw new \Exception("Error {$code} interacting with memcached : {$message}"); }
/** * Internal method to get storage capacity. * * @param array $normalizedOptions * @return array|boolean Capacity as array or false on failure * @throws Exception\ExceptionInterface */ protected function internalGetCapacity(array &$normalizedOptions) { $stats = $this->memcached->getStats(); if ($stats === false) { throw $this->getExceptionByResultCode($this->memcached->getResultCode()); } $mem = array_pop($stats); return array('free' => $mem['limit_maxbytes'] - $mem['bytes'], 'total' => $mem['limit_maxbytes']); }
/** * @param string $key * @return bool */ protected function clearOneObjectFromCache($key) { $this->commit(); if ($this->cache->delete($key)) { return true; } // Return true if key not found return $this->cache->getResultCode() === \Memcached::RES_NOTFOUND; }
/** * Try to get an item, and if missed call the fallback method to produce the value and store it. * * @param string $key * @param callable $fallback * @param int|\DateInterval|\DateTime|callable $validity Number of seconds this is valid for (if int). If this is a callable, it will get the return value as its only argument, and will need to return int|\DateInterval|\DateTime. * @return mixed */ function getWithFallback($key, callable $fallback, $validity = null) { $value = $this->get($key); if ($value === false) { $resultCode = $this->memcache->getResultCode(); $value = $fallback(); if ($resultCode == \Memcached::RES_NOTFOUND) { if (is_callable($validity)) { $validity = $validity($value); if (!$validity instanceof \DateInterval && !$validity instanceof \DateTime && !is_int($validity)) { throw new \UnexpectedValueException('Validity callback should return instance of \\DateTime or \\DateInterval or int.'); } } $this->set($key, $value, $validity); } } return $value; }
/** * Load XML from cache * * @param int $userid * @param string $apikey * @param string $scope * @param string $name * @param array $args * @return string */ public function load($userid, $apikey, $scope, $name, $args) { $key = $this->getKey($userid, $apikey, $scope, $name, $args); $age_data = $this->memcached->get($key . '_age'); $age_result = $this->memcached->getResultCode(); if ($age_result != 0) { return false; } $age = time() - (int) $age_data['age']; if ($age > (int) $age_data['ttl']) { return false; } $read = (string) $this->memcached->get($key); $read_result = $this->memcached->getResultCode(); if ($read_result != 0) { return false; } return $read; }