/** * {@inheritdoc} */ public function exists() { if (false === $this->memcached->get($this->storeId)) { return MemcachedClient::RES_SUCCESS === $this->memcached->getResultCode(); } return true; }
public function get($id) { if (($value = $this->memcached->get($id)) === false) { return null; } return $value; }
/** * Чтение массива из кеша * @param string $cache_file кешируемый файл * @param int $mytime время жизни кеш файла, отличное от глобальных настроек * @return mixed массив, если файл кеширован и не требует обновления, * false - если необходимо обновить/создать */ public function read($cache_file, $mytime = false) { $cache_file = validpath($cache_file); if (!$cache_file || !$this->state) { return false; } $this->cache_files[] = $cache_file; $f = ROOT . 'include/cache/' . $cache_file . '.html'; $my_array = array(); if ($this->m) { $my_array = $this->m->get($cache_file); } elseif (file_exists($f)) { $contents = file_get_contents($f); $my_array = unserialize($contents); } if (!$my_array) { return false; } $array = (array) $my_array[0]; $time = $my_array['!_cache_time_']; $delay = $mytime ? $mytime : $this->time * 3600; if ($time < time() - $delay) { if ($this->m) { $this->m->delete($cache_file); } return false; } $this->pop_readed(); // Ибо уже кешировано return $array; }
/** * Read an object from memcached if it exists, * otherwise load the object from the database and then cache it * @param mixed $pkValue Primary key value * @param integer|boolean $cache Number of seconds to cache the object for * 0 = cache never expires, FALSE = do not cache * @return boolean */ public function read($pkValue = FALSE, $cache = FALSE) { // If we're not caching or there is no memcached object just call parent if ($cache === FALSE || !$this->memcached instanceof \Memcached) { return parent::read($pkValue); } // If there is a key value passed set it if ($pkValue !== FALSE) { $this->iset(static::$pk, $pkValue); } // Load memcached object attributres $obj = $this->memcached->get($this->getMemcachedID()); // If the object is not cached, load and cache it if ($obj === FALSE) { // Read object if (parent::read($pkValue) === FALSE) { return FALSE; } // Cache object attributes $this->memcached->set($this->getMemcachedID(), $this->getCacheAttributes(), $cache); } else { $this->setCacheAttributes($obj); } // Return return TRUE; }
/** * @return mixed */ public function get() { if (isset(static::$cache[$this->key])) { return static::$cache[$this->key]; } return $this->memcached->get($this->key); }
/** * Retrieve an item from the cache by key. * * @param string|array $key * @return mixed */ public function get($key) { $value = $this->memcached->get($this->prefix . $key); if ($this->memcached->getResultCode() == 0) { return $value; } }
/** * @see Cache::_exists() */ protected function _exists($key) { if (!$this->is_connected) { return false; } return $this->memcached->get($key) !== false; }
/** * 解锁 * @param unknown $key */ public function release($key) { $key = $this->prefix . $key; if ($this->memcached->get($key) === $this->Identifier) { $this->memcached->delete($key); } }
/** * @param $key * @return mixed */ public function get($key) { if (!is_null($this->memcache)) { return $this->memcache->get($key); } return false; }
/** * Gets an item from the cache * * @param string $key cache key * @return mixed cached object or false if not found */ public function Get($key) { if (empty($key)) { return false; } return $this->memcache->get($key); }
/** * Get a value from cache * * @param string $p_sKey The Key * @return mixed */ function get($p_sKey) { if ($this->_serverAdded === false) { $this->addServer('localhost'); } return $this->_handler->get($p_sKey); }
/** * {@inheritdoc} */ public function load() { $contents = $this->memcached->get($this->key); if ($contents) { $this->setFromStorage($contents); } }
/** * 通过加锁机制在cache失效时并发时只透传一个请求到后端子系统 * @param $method string 方法名 * @param $params array 参数列表 * @param $key string 缓存key */ public static function cacheStrategyOnce($method, $params, $key, $expire = 100) { // 做法相似,只是需要把概率判断的算法换成使用锁保证高并发请求时严格只有一个请求透传到后端系统 $csfKey = self::getCSTKey($key, 1); $lockKey = self::getCSTLockKey($key, 1); $memcached = new Memcached(); $memcached->addServer('127.0.0.1', 11211); $value = $memcached->get($csfKey); if (!empty($value) && !empty($value['data']) && !empty($value['doubleExpire'])) { $doubleExpire = $value['doubleExpire']; if (time() <= $doubleExpire - $expire) { // cache还在[start, start + expire]时间内仍有效,无需请求后端 return $value['data']; } $isLock = $memcached->get($lockKey); if (!empty($lockKey)) { // 有锁,表示当前已有请求到后端去读取最新数据并更新cache; return $value['data']; } } // cache已过期或者无锁,到后台实时请求并更新cache $ret = $memcached->set($lockKey, 1, 1); $data = self::$method($params); if (isset($data['resultCode']) && $data['resultCode'] == 0) { unset($data['resultCode']); $doubleExpire = time() + 2 * $expire; $value = array('data' => $data, 'doubleExpire' => $doubleExpire); $ret = $memcached->set($csfKey, $value, $doubleExpire); } $ret = $memcached->delete($lockKey, 1); return $data; }
/** * Loads item by cache key. * * @param string $key * @return mixed * * @throws Ejsmont\CircuitBreaker\Storage\StorageException if storage error occurs, handler can not be used */ protected function load($key) { try { return $this->memcached->get($key); } catch (\Exception $e) { throw new StorageException("Failed to load memcached key: {$key}", 1, $e); } }
/** * @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()); }
function __construct() { $memc = new Memcached(); $memc->addServer('localhost', '11211'); $memc->delete('callback_test'); var_dump($memc->get('callback_test', array($this, 'ReadThrough'))); var_dump($memc->get('callback_test')); }
/** * 是否有值 * * @return boolean */ function has_value() { $returnValue = $this->memcache_ins->get($this->key); if ($returnValue === FALSE) { return FALSE; } return TRUE; }
/** * Attempt to retrieve a value from the cache server, if not set it. * * @param string $key Key we can use to retrieve the data. * * @return bool|string False on failure or String, data belonging to the key. * @access public */ public function get($key) { if ($this->connected === true && $this->ping() === true) { $data = $this->server->get($key); return $this->isRedis ? $this->IgBinarySupport ? igbinary_unserialize($data) : unserialize($data) : $data; } return false; }
public function get($key, $group = null) { $r = self::$memcached->get($key); if ($r === false) { return null; } return $r; }
/** * Read session data * * @param string $id * @return string */ public function read($id) { $_SESSION = json_decode($this->memcached->get("sessions/{$id}"), true); if (isset($_SESSION) && !empty($_SESSION) && $_SESSION != null) { return session_encode(); } return ''; }
/** * * @param type $key * @return type */ public function get($key, $cache_cb = null, &$cas_token = null) { if ($this->_noCache) { return null; } $_ret = $this->_memcached->get($key); return $_ret; }
public function get() { if ($this->value !== null) { return $this->value; } $this->value = self::$memcached->get($this->key); return $this->value; }
/** * @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; }
public function cas($key, $value) { if (($return = @$this->memcached->get($key)) !== false) { return $return; } $this->memcached->set($key, $value); return $value; }
/** * {@inheritdoc} */ public function has($key) { $data = $this->server->get($this->getKey($key)); if (!$data) { return false; } return true; }
/** * {@inheritDoc} * * @return mixed */ public function get($key) { $data = $this->memcached->get($key); if ($data === false) { return null; } return $data; }
/** * Get the number of indexed terms * @return integer Number of indexed terms */ public function size() { $this->size = $this->index->get('index_size'); if ($this->size === false) { $this->size = 0; } return $this->size; }
public function get($key) { if ($this->isDisable) { $value = false; } else { $value = $this->memcached->get($key); } return $value; }
/** * Retrieve an item from the cache. * * @param string $name The name of the cache * @param boolean $hard_refresh True if we should do a hard refresh * @return mixed Cache data if successful, false if failure */ function fetch($name, $hard_refresh = false) { $data = $this->memcached->get($this->unique_id . "_" . $name); if ($data === false) { return false; } else { return $data; } }
/** * Retrieve an item from the cache driver. * * @param string $key * @return mixed */ protected function retrieve($key) { if ($this->sectionable($key)) { list($section, $key) = $this->parse($key); return $this->get_from_section($section, $key); } elseif (($cache = $this->memcache->get($this->key . $key)) !== false) { return $cache; } }