public function setUp() { parent::setUp(); $this->memcache = new \Memcached(); $this->memcache->addServer(self::TEST_MEMCACHE_SERVER, self::TEST_MEMCACHE_PORT); $this->memcache->flush(); if (!$this->memcache->set('test', 1, time() + 100)) { throw new \RuntimeException('Cannot save item to memcache. ' . $this->memcache->getResultMessage()); } }
/** * {@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; }
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); }
/** * Generate exception based of memcached result code * * @param int $code * @return Exception\RuntimeException * @throws Exception\InvalidArgumentException On success code */ protected function getExceptionByResultCode($code) { switch ($code) { case MemcachedResource::RES_SUCCESS: throw new Exception\InvalidArgumentException("The result code '{$code}' (SUCCESS) isn't an error"); default: return new Exception\RuntimeException($this->memcached->getResultMessage()); } }
/** * @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}"); }
/** * @param $key * @param $value * * @return bool * @throws Exception */ public function set($key, &$value) { $key = $this->cleanKey($key); if (!$this->_cache->set($key, $value, $this->_timeout)) { CRM_Core_Error::debug('Result Code: ', $this->_cache->getResultMessage()); CRM_Core_Error::fatal("memcached set failed, wondering why?, {$key}", $value); return FALSE; } return TRUE; }
/** * Add a Memcached server to stack * * @param string $server Server address (or IP) * @param string $port Server port * @param string $weight Server weight */ private function addServer($server, $port, $weight) { $status = $this->instance->addServer($server, $port, $weight); if ($status === false) { $this->raiseError("Error communicating with server", array("RESULTCODE" => $this->instance->getResultCode(), "RESULTMESSAGE" => $this->instance->getResultMessage())); } if (sizeof($this->instance->getServerList()) == 0) { $this->raiseError("No available server, disabling cache administratively"); $this->disable(); } else { $this->enable(); } }
function GetMemCachedObj() { static $MemCachedObj; if (!$MemCachedObj) { $MemCachedObj = new Memcached(); if (!$MemCachedObj->addServer(MEMCACHED_HOST, MEMCACHED_PORT)) { throw new Exception('No se pudo conectar al Memcached: ' . $MemCachedObj->getResultMessage() . '.'); } $MemCachedObj->setOption(Memcached::OPT_COMPRESSION, false); $MemCachedObj->setOption(Memcached::OPT_BINARY_PROTOCOL, false); $MemCachedObj->setOption(Memcached::OPT_CONNECT_TIMEOUT, 10000); } return $MemCachedObj; }
/** * @param array * @throws RunTimeException * @return array */ public function all(array $ids) { $this->keys = array(); $this->keys = preg_filter('/^/', $this->table . '_', $ids); $data = $this->Memcached->getMulti($this->keys); if ($data === false) { $msg = $this->Memcached->getResultMessage(); $code = $this->Memcached->getResultCode(); throw new \RunTimeException("Memcached Error (all): " . $msg . "(" . $code . ")"); } $result = array(); foreach ($data as $key => $val) { $result[str_replace($this->table . '_', '', $key)] = json_decode(gzuncompress($val), true); } return $result; }
/** * Retrieves data from the cache identified by the specified key * * @param string $key The key. * * @return mixed * * @throws \YapepBase\Exception\StorageException On error. */ public function get($key) { $debugger = Application::getInstance()->getDiContainer()->getDebugger(); $startTime = microtime(true); $result = $this->memcache->get($this->makeKey($key)); if (false === $result) { $code = $this->memcache->getResultCode(); if (\Memcached::RES_NOTFOUND !== $code && \Memcached::RES_SUCCESS !== $code) { throw new StorageException('Unable to get value in memcache. Error: ' . $this->memcache->getResultMessage(), $this->memcache->getResultCode()); } } // If we have a debugger, we have to log the request if (!$this->debuggerDisabled && $debugger !== false) { $debugger->addItem(new StorageItem('memcached', 'memcached.' . $this->currentConfigurationName, StorageItem::METHOD_GET . ' ' . $key, $result, microtime(true) - $startTime)); } return $result; }
protected function storeValuesImpl(array $values, $expirationTime) { $errorCacheEntryNames = NULL; $adjustedExpirationTime = $expirationTime; if (!isset($adjustedExpirationTime)) { $adjustedExpirationTime = 0; } $storableValues = $deletableCacheEntryNames = NULL; foreach ($values as $cacheEntryName => $value) { if (isset($value)) { $storableValues[$cacheEntryName] = $value; } else { $deletableCacheEntryNames[] = $cacheEntryName; } } if (isset($deletableCacheEntryNames)) { foreach ($deletableCacheEntryNames as $deletableCacheEntryName) { $result = $this->memcached->delete($deletableCacheEntryName); if (($result === FALSE) && ($this->memcached->getResultCode() != Memcached::RES_NOTFOUND)) { $errorCacheEntryNames[] = $deletableCacheEntryName; LogHelper::log_error(t( '[@cacheType] Internal error during value deletion: @message', array('@cacheType' => self::CACHE__TYPE, '@message' => $this->memcached->getResultMessage()))); } } } if (isset($storableValues)) { $result = $this->memcached->setMulti($storableValues, $adjustedExpirationTime); if ($result === FALSE) { LogHelper::log_error(t( '[@cacheType] Internal error during value storing: @message', array('@cacheType' => self::CACHE__TYPE, '@message' => $this->memcached->getResultMessage()))); ArrayHelper::appendValue($errorCacheEntryNames, array_keys($storableValues)); } } return $errorCacheEntryNames; }
public function testAddWithPrefixSuccess() { $memcached = new Memcached(); $memcached->setOption(Memcached::OPT_PREFIX_KEY, "widgets_"); $request = new MemcacheSetRequest(); $item = $request->addItem(); $item->setKey("widgets_float"); $item->setValue("2"); $item->setFlags(6); // float $item->setSetPolicy(SetPolicy::ADD); $item->setExpirationTime(30); $response = new MemcacheSetResponse(); $response->addSetStatus(SetStatusCode::STORED); $this->apiProxyMock->expectCall('memcache', 'Set', $request, $response); $this->assertTrue($memcached->add("float", 2.0, 30)); $this->assertEquals($memcached->getOption(Memcached::OPT_PREFIX_KEY), "widgets_"); $this->assertEquals($memcached->getResultCode(), Memcached::RES_SUCCESS); $this->assertEquals($memcached->getResultMessage(), "SUCCESS"); $this->apiProxyMock->verify(); }
/** * @return string */ public function getError() { return $this->memcached->getResultMessage(); }
/** * 错误处理 * @param $op * @param $key * @throws \Simple\Model\Exception\DbException */ private function error($op, $key) { throw new DbException($op . '操作' . $key . '出错,错误码为:' . $this->dbh->getResultCode() . ',具体信息为:' . $this->dbh->getResultMessage()); }
public function testGetMissing() { $request = new MemcacheGetRequest(); $request->addKey("key"); $response = new MemcacheGetResponse(); $this->apiProxyMock->expectCall('memcache', 'Get', $request, $response); $memcached = new Memcached(); $this->assertFalse($memcached->get("key")); $this->assertEquals($memcached->getResultCode(), Memcached::RES_NOTFOUND); $this->assertEquals($memcached->getResultMessage(), 'NOT FOUND'); $this->apiProxyMock->verify(); }
/** * {@inheritdoc} */ public function resultMessage() { return $this->memcache->getResultMessage(); }
<?php $m = new Memcached(); $m->setOption(Memcached::OPT_BINARY_PROTOCOL, true); $m->addServer('localhost', '11211'); $key = uniqid('touch_binary'); $m->set($key, 'test', time() + 86400); $m->get($key); echo "GET: " . $m->getResultMessage() . PHP_EOL; $m->touch($key, time() + 86400); echo "TOUCH: " . $m->getResultMessage() . PHP_EOL; $m->touch($key, time() + 86400); echo "TOUCH: " . $m->getResultMessage() . PHP_EOL; $m->get($key); echo "GET: " . $m->getResultMessage() . PHP_EOL; $m->get($key); echo "GET: " . $m->getResultMessage() . PHP_EOL; echo "DONE" . PHP_EOL;
protected function buildMemcached() { $memcached = new \Memcached(); $memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, TRUE); if (!$memcached->addServer($this->memcachedHost, $this->memcachedPort)) { $message = $memcached->getResultMessage() . "(" . $memcached->getResultCode() . ")"; throw new \RuntimeException("Can't connect to memcached: " . $host . ", message: {$port}"); } return $memcached; }
/** * Return the message describing the result of the last operation. * * @link http://www.php.net/manual/en/memcached.getresultmessage.php * * @return string Message describing the result of the last Memcached operation. */ public function getResultMessage() { return $this->m->getResultMessage(); }