/** * Save getting data to memory (RAM) */ public function save() { $this->storage_data['last_update'] = $this->last_update; if ($this->memcache->replace('storage_data', $this->storage_data) === false) { $this->memcache->add('storage_data', $this->storage_data); } }
function replace($key, $value, $ttl = 0) { $key = str_replace('\\', '/', $key); if (!$this->memcache->replace($key, $value, false, $ttl)) { $message = sprintf('Memcache::replace() with key "%s" failed', $key); \ManiaLib\Utils\Logger::error($message); } }
/** * 添加一个缓存 * * 修改为: 强制设置,强制过期 * * @param string $key 缓存键 * @param mix $value 缓存值 * @param string $flag 是否压缩 MEMCACHE_COMPRESSED(2)使用zlib压缩 * @param int $expire 过期时间,0永不过期,最大30天(2592000) 或unix时间戳 */ public function add($key, $value, $flag = FALSE, $expire = 0) { if (!$this->connect->add($this->prefix . $key, $value, $flag, $expire)) { //只能新添加一个值,重复刷新不会覆盖 return $this->connect->replace($this->prefix . $key, $value, $flag, $expire); //必须是已存在的,不存在的会导致设置失败 } return true; }
/** * {@inheritdoc} */ public function put($key, $data, $ttl = 0) { if ($ttl !== 0) { $ttl += time(); } if ($this->memcache->replace($key, $data, $this->compressionLevel, $ttl) === false) { return $this->memcache->set($key, $data, $this->compressionLevel, $ttl); } return true; }
/** * @see \Kalibri\Cache\BaseInterface::set(); */ public function set($key, $value, $expire = 0) { if (\Kalibri::config()->get('debug.log.is-enabled', false)) { \Kalibri::logger()->add(\Kalibri\Logger\Base::L_DEBUG, "SET: (ttl={$expire}) {$key}=" . var_export($value, true), $this); } if (!$this->_memcache->replace($key, $value, MEMCACHE_COMPRESSED, $expire)) { $this->_memcache->set($key, $value, MEMCACHE_COMPRESSED, $expire); } $this->_local[$key] = $value; return $this; }
/** * {@inheritdoc} */ public function write($sessionId, $data) { if (!$this->memcache->replace($this->prefix . $sessionId, $data, 0, $this->memcacheOptions['expiretime'])) { return $this->memcache->set($this->prefix . $sessionId, $data, 0, $this->memcacheOptions['expiretime']); } return true; }
/** * Adds entry into memcache/apc DB. * * @param string $key Cache key name * @param mxied $data Serialized cache data * @param bollean $index Enables immediate index update * * @param boolean True on success, False on failure */ private function add_record($key, $data, $index = false) { if ($this->type == 'memcache') { $result = $this->db->replace($key, $data, MEMCACHE_COMPRESSED, $this->ttl); if (!$result) { $result = $this->db->set($key, $data, MEMCACHE_COMPRESSED, $this->ttl); } } else { if ($this->type == 'apc') { if (apc_exists($key)) { apc_delete($key); } $result = apc_store($key, $data, $this->ttl); } } // Update index if ($index && $result) { $this->load_index(); if (array_search($key, $this->index) === false) { $this->index[] = $key; $data = serialize($this->index); $this->add_record($this->ikey(), $data); } } return $result; }
/** * Unlock cached item - override parent for cacheid compatibility with lock * * @param string $id The cache data id * @param string $group The cache data group * * @return boolean True on success, false otherwise. * * @since 11.1 */ public function unlock($id, $group = null) { $cache_id = $this->_getCacheId($id, $group) . '_lock'; if (!$this->lockindex()) { return false; } $index = self::$_db->get($this->_hash . '-index'); if ($index === false) { $index = array(); } foreach ($index as $key => $value) { if ($value->name == $cache_id) { unset($index[$key]); } break; } self::$_db->replace($this->_hash . '-index', $index, 0, 0); $this->unlockindex(); return self::$_db->delete($cache_id); }
/** * 替换k存储的值,当k不存在时返回FALSE * @param mixed $v 将要替换存储的值。字符串和整型被以原文存储,其他类型序列化后存储 * @param string $compress 为TRUE时使用 MEMCACHE_COMPRESSED 标记对数据进行压缩(使用zlib)。 * @param number $expire 写入缓存的数据的失效时间。如果此值设置为0表明此数据永不过期。可以设置一个UNIX时间戳或以秒为单位的整数(从当前算起的时间差)来说明此数据的过期时间,但是在后一种设置方式中,不能超过 2592000秒(30天)。 * @return bool 成功时返回TRUE,当k不存在时返回FALSE */ public function replace($v, $expire = 0, $compress = FALSE) { if ($expire <= 0) { return FALSE; } return $this->mc->replace($this->k, $v, $compress, $expire); }
/** * Set/add something to the cache * * @param string $key Key for the value * @param mixed $value Value * @return boolean */ public function set($key, $value) { if ($this->cache->add($key, $value, $this->compression, $this->expire)) { return true; } return $this->cache->replace($key, $value, $this->compression, $this->expire); }
/** * Read from cache. * @param string key * @return mixed|NULL */ public function read($key) { $key = $this->prefix . $key; $meta = $this->memcache->get($key); if (!$meta) { return NULL; } // meta structure: // array( // data => stored data // delta => relative (sliding) expiration // callbacks => array of callbacks (function, args) // ) // verify dependencies if (!empty($meta[self::META_CALLBACKS]) && !NCache::checkCallbacks($meta[self::META_CALLBACKS])) { $this->memcache->delete($key, 0); return NULL; } if (!empty($meta[self::META_DELTA])) { $this->memcache->replace($key, $meta, 0, $meta[self::META_DELTA] + time()); } return $meta[self::META_DATA]; }
public function replace($key, $data, $expire = 0) { if (!isset($this->memcache)) { $this->connect(); } $key = $this->prefix . $key; return $this->memcache->replace($key, $data, $expire); }
public function saveConfiguration($namespace, $context, $language, $environment, $name, Configuration $config) { $name = $this->remapConfigurationName($name); // saving the configuration always includes saving in both the // persistent file and the memcached store! $key = $this->getStoreIdentifier($namespace, $context, $language, $environment, $name); $this->memcachedService->replace($key, $config, 0, $this->expireTime); ConfigurationManager::saveConfiguration($namespace, $context, $language, $environment, $name, $config); }
public function afterSave(Atomik_Model_Builder $builder, Atomik_Model $model) { $key = $builder->name . ':' . $model->getPrimaryKey(); $data = $model->toArray(); if ($this->_memcache->replace($key, $data) === false) { $this->_memcache->set($key, $data); } }
/** * 更新过期时间 * * @return void */ public function touch() { if (null !== $this->_sessionId && $this->_expire) { // we try replace() first becase set() seems to be slower if (!($result = $this->_memcache->replace($this->_sessionId, $this->_session, 0, $this->_expire))) { $result = $this->_memcache->set($this->_sessionId, $this->_session, 0, $this->_expire); } } }
/** * Store some data in session. * Expects a session id and the data to write. * @param string $sessionId * @param mixed $data * @return boolean */ public function write($sessionId = '', $data = '') { if ($sessionId !== '') { if ($this->_memcache->replace($this->_key($sessionId), $data, null, $this->_lifetime) === false) { // add the item if we couldn't have replaced it (i.e. if it doesn't already exists) $this->_memcache->add($this->_key($sessionId), $data, null, $this->_lifetime); } } return true; }
public function replace($key, $var, $flag = null, $expire = null) { if ($this->stopwatch) { $e = $this->getStopwatchEvent('replace'); } $result = parent::replace($key, $var, $flag, $expire); if ($this->stopwatch) { $e->stop(); } return $result; }
/** * set expire the memcache object * @param string $key */ private static function _setExpire($key) { $lifeTime = ini_get("session.gc_maxlifetime"); $expire = self::$_db->get($key . "_expire"); if ($expire + $lifeTime < time()) { self::$_db->delete($key); self::$_db->delete($key . "_expire"); } else { self::$_db->replace($key . "_expire", time()); } }
/** * Replace the value of a key. * * @see Memcache::replace() * * @param string $key The key. * @param string $var The data to store. * @param integer $timeout Expiration time in seconds. * * @return boolean True on success, false if key doesn't exist. */ public function replace($key, $var, $expire = 0) { $var = @serialize($var); $len = strlen($var); if ($len > self::MAX_SIZE) { if (!empty($this->_params['large_items']) && $this->_memcache->get($this->_key($key))) { return $this->_set($key, $var, $expire, $len); } return false; } return $this->_memcache instanceof Memcached ? $this->_memcache->replace($key, $var, $expire) : $this->_memcache->replace($this->_key($key), $var, $this->_getFlags(1), $expire); }
/** * Purges the entire cache folder - no way to limit it to specific routes for the default cache system * * @param array $tags * @return bool */ public function purge(array $tags) { $micro = substr(hash('md5', microtime()), 0, 6); foreach ($tags as $tag) { $tagKey = $this->getMemcacheKey() . 'tags.' . $tag; $tagValue = $this->memcache->get($tagKey); if (empty($tagValue)) { $this->memcache->set($tagKey, $micro); } else { $this->memcache->replace($tagKey, $micro); } } return true; }
/** * Adds entry into memcache/apc DB. * * @param string $key Cache key name * @param mixed $data Serialized cache data * * @param boolean True on success, False on failure */ private function add_record($key, $data) { if ($this->type == 'memcache') { $result = $this->db->replace($key, $data, MEMCACHE_COMPRESSED, $this->ttl); if (!$result) { $result = $this->db->set($key, $data, MEMCACHE_COMPRESSED, $this->ttl); } } else { if ($this->type == 'apc') { if (apc_exists($key)) { apc_delete($key); } $result = apc_store($key, $data, $this->ttl); } } return $result; }
/** * Alter the TTL for a given key. Essentially, renewing it in * the cache. * * @param string $key * @param integer $ttl * @return boolean */ public function renew($key, $ttl = self::DEFAULT_TTL) { if (!$this->hasConnection()) { // @codeCoverageIgnoreStart return false; // @codeCoverageIgnoreEnd } $value = $this->get($key); if ($value) { try { return $this->memcache->replace($this->getKeyString($key), $value, $this->getFlagFromValue($value), $ttl); // @codeCoverageIgnoreStart } catch (\Exception $e) { } } // @codeCoverageIgnoreEnd return false; }
/** * Erase cached entry by its key * * @param string $key * @return boolean TRUE if deletion was successful; otherwise FALSE * @link http://www.php.net/manual/en/memcache.get.php */ public function delete($key) { if (!$this->get_internal_cache()->isCached($key)) { return TRUE; } $this->get_internal_cache()->delete($key); $this->make_dirty(); if ($this->_save_on_destruction) { return true; } $this->connect(); $res = $this->mem_cache_instace->replace($this->genKey(), $this->get_internal_cache()); if ($this->mem_cache_instace->get($this->genKey()) != $this->get_internal_cache()) { $this->disconnect(); return FALSE; } $this->disconnect(); return $res; }
public function allow($key, $quota = 60, $period = 60.0) { $allow = true; try { $memcache = new Memcache(); } catch (Exception $e) { throw new Exception('Failed to create memcache object.'); } if (!$memcache->connect('127.0.0.1')) { throw new Exception('Failed to connect to memcache server.'); } if (!($cached = $memcache->get($key))) { $quota = max(1, intval($quota)); $period = max(1.0E-6, floatval($period)); $this->quota = $quota; $this->ttl = $period / $quota; $this->time = microtime(true) - $period - $this->ttl; $this->stock = $quota - 1; if (!$memcache->add($key, $this)) { throw new Exception('Failed to add memcache key.'); } } else { $this->quota = $cached->quota; $this->ttl = $cached->ttl; $this->time = microtime(true); $this->stock = min($cached->stock + intval(($this->time - $cached->time) / $this->ttl), $this->quota); if ($this->stock > 0) { $this->stock--; } else { $allow = false; } if (!$memcache->replace($key, $this)) { throw new Exception('Failed to replace memcache key.'); } } if (!$memcache->close()) { throw new Exception('Failed to close memcache connection.'); } unset($memcache); return $allow; }
/** * Renew a cached item by key * @param string $key * @param integer $ttl (optional) default 86400 * @return boolean */ public function renew($key, $ttl = self::DEFAULT_TTL) { if (!$this->hasConnection()) { // @codeCoverageIgnoreStart return false; // @codeCoverageIgnoreEnd } $value = $this->get($key); if ($value) { $flag = null; if (!is_bool($value) && !is_int($value) && !is_float($value)) { $flag = MEMCACHE_COMPRESSED; } try { return $this->memcache->replace($key, $value, $flag, $ttl); // @codeCoverageIgnoreStart } catch (\Exception $e) { } } // @codeCoverageIgnoreEnd return false; }
/** * Read from cache. * @param string key * @return mixed|NULL */ public function read($key) { $key = $this->prefix . $key; $meta = $this->memcache->get($key); if (!$meta) { return NULL; } // meta structure: // array( // data => stored data // delta => relative (sliding) expiration // df => array of dependent files (file => timestamp) // consts => array of constants (const => [value]) // ) // verify dependencies if (!empty($meta[self::META_CONSTS])) { foreach ($meta[self::META_CONSTS] as $const => $value) { if (!defined($const) || constant($const) !== $value) { $this->memcache->delete($key); return NULL; } } } if (!empty($meta[self::META_FILES])) { //clearstatcache(); foreach ($meta[self::META_FILES] as $depFile => $time) { if (@filemtime($depFile) != $time) { $this->memcache->delete($key); return NULL; } } } if (!empty($meta[self::META_DELTA])) { $this->memcache->replace($key, $meta, 0, $meta[self::META_DELTA] + time()); } return $meta[self::META_DATA]; }
/** * Updates data in cache. * * @param string $key The dataset Id * @param string $value The data to write to cache * @param string $namespace The dataset namespace * @param int $lifetime Date/Time on which the cache-entry expires * @param string $userdata The custom userdata to add * * @return bool TRUE on success, otherwise FALSE * @author Benjamin Carl <*****@*****.**> * @access public * @throws Doozr_Cache_Service_Exception */ public function update($key, $value, $namespace, $lifetime = null, $userdata = null) { // Get internal used key $key = $this->calculateUuid($key . $namespace); // Build dataset from input $dataset = array($this->getExpiresAbsolute($lifetime), $userdata, $this->encode($value), $namespace); if (true !== ($result = $this->connection->replace($key, $dataset, $this->getCompress() === true ? MEMCACHE_COMPRESSED : 0, $this->getExpiresAbsolute($lifetime)))) { throw new Doozr_Cache_Service_Exception('Error while updating dataset!'); } if ($result === true) { // On create we need to purge the old entry from runtime cache ... $this->addToRuntimeCache($key, $dataset, $namespace); } return true; }
function memcache_replace($m, $key, $var, $flag = null, $expire) { return Memcache::replace($key, $var, $flag, $expire); }
/** * Replaces data * * @param string $key * @param mixed $var * @param integer $expire * @return boolean */ function replace($key, &$var, $expire = 0) { return @$this->_memcache->replace($key, $var, false, $expire); }
/** * {@inheritDoc} * * @see http://www.php.net/manual/en/memcache.replace.php */ protected function _replace($key, $value, $ttl) { return $this->_connection->replace($key, $value, null, $ttl); }