本文整理汇总了PHP中Memcached::replace方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcached::replace方法的具体用法?PHP Memcached::replace怎么用?PHP Memcached::replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Memcached
的用法示例。
在下文中一共展示了Memcached::replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: update
/**
* update
*
* @param mixed $identifier identifier to save
* @param string $amount the current amount
* @param string $timespan the timespan in seconds
*
* @return boolean
*/
public function update($identifier = null, $amount = null, $timespan = null)
{
if (!$identifier && !$amount && !$timespan) {
throw new \InvalidArgumentException('identifier, amount and timespan are required');
}
return $this->memcached->replace($this->fileName($identifier), $amount, $timespan);
}
示例2: update
/**
* 替换已经存在的key
* @param $key
* @param $val
* @param $expireTime
* @return bool
*/
public function update($key, $val, $expireTime)
{
$ret = $this->dbh->replace($key, $val, $expireTime);
if ($ret === false) {
$this->error('update', $key);
}
return true;
}
示例3: replace
function replace($key, $value, $ttl = 0)
{
$key = str_replace('\\', '/', $key);
if (!$this->memcached->replace($key, $value, $ttl)) {
$message = sprintf('Memcache::replace() with key "%s" failed', $key);
\ManiaLib\Utils\Logger::error($message);
}
}
示例4: put
/**
* {@inheritdoc}
*/
public function put($key, $data, $ttl = 0)
{
if ($ttl !== 0) {
$ttl += time();
}
if ($this->memcached->replace($key, $data, $ttl) === false) {
return $this->memcached->set($key, $data, $ttl);
}
return true;
}
示例5: unlock
/**
* 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 12.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);
$this->unlockindex();
return self::$_db->delete($cache_id);
}
示例6: replace
/**
* Replaces a value in cache.
*
* This method is similar to "add"; however, is does not successfully set a value if
* the object's key is not already set in cache.
*
* @link http://www.php.net/manual/en/memcached.replace.php
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $byKey True to store in internal cache by key; false to not store by key
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function replace($key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false)
{
$derived_key = $this->buildKey($key, $group);
$expiration = $this->sanitize_expiration($expiration);
// If group is a non-Memcached group, save to runtime cache, not Memcached
if (in_array($group, $this->no_mc_groups)) {
// Replace won't save unless the key already exists; mimic this behavior here
if (!isset($this->cache[$derived_key])) {
return false;
}
$this->cache[$derived_key] = $value;
return true;
}
// Save to Memcached
if (false !== $byKey) {
$result = $this->mc->replaceByKey($server_key, $derived_key, $value, $expiration);
} else {
$result = $this->mc->replace($derived_key, $value, $expiration);
}
// Store in runtime cache if add was successful
if (Memcached::RES_SUCCESS === $this->getResultCode()) {
$this->add_to_internal_cache($derived_key, $value);
}
return $result;
}
示例7: _get_lock
/**
* Get lock
*
* Acquires an (emulated) lock.
*
* @param string $session_id Session ID
* @return bool
*/
protected function _get_lock($session_id)
{
if (isset($this->_lock_key)) {
return $this->_memcached->replace($this->_lock_key, time(), 300);
}
// 30 attempts to obtain a lock, in case another request already has it
$lock_key = $this->_key_prefix . $session_id . ':lock';
$attempt = 0;
do {
if ($this->_memcached->get($lock_key)) {
sleep(1);
continue;
}
if (!$this->_memcached->set($lock_key, time(), 300)) {
return FALSE;
}
$this->_lock_key = $lock_key;
break;
} while (++$attempt < 30);
if ($attempt === 30) {
return FALSE;
}
$this->_lock = TRUE;
return TRUE;
}
示例8: lockSession
/**
* Get lock
*
* Acquires an (emulated) lock.
*
* @param string $session_id Session ID
*
* @return bool
*/
protected function lockSession(string $session_id) : bool
{
if (isset($this->lockKey)) {
return $this->memcached->replace($this->lockKey, time(), 300);
}
// 30 attempts to obtain a lock, in case another request already has it
$lock_key = $this->keyPrefix . $session_id . ':lock';
$attempt = 0;
do {
if ($this->memcached->get($lock_key)) {
sleep(1);
continue;
}
if (!$this->memcached->set($lock_key, time(), 300)) {
$this->logger->error('Session: Error while trying to obtain lock for ' . $this->keyPrefix . $session_id);
return false;
}
$this->lockKey = $lock_key;
break;
} while (++$attempt < 30);
if ($attempt === 30) {
$this->logger->error('Session: Unable to obtain lock for ' . $this->keyPrefix . $session_id . ' after 30 attempts, aborting.');
return false;
}
$this->lock = true;
return true;
}
示例9: replaceItem
/**
* Replace an item.
*
* Options:
* - ttl <float> optional
* - The time-to-live (Default: ttl of object)
* - namespace <string> optional
* - The namespace to use (Default: namespace of object)
*
* @param string $key
* @param mixed $value
* @param array $options
* @return boolean
* @throws Exception
*
* @triggers replaceItem.pre(PreEvent)
* @triggers replaceItem.post(PostEvent)
* @triggers replaceItem.exception(ExceptionEvent)
*/
public function replaceItem($key, $value, array $options = array())
{
$baseOptions = $this->getOptions();
if (!$baseOptions->getWritable()) {
return false;
}
$this->normalizeOptions($options);
$this->normalizeKey($key);
$args = new ArrayObject(array('key' => &$key, 'value' => &$value, 'options' => &$options));
try {
$eventRs = $this->triggerPre(__FUNCTION__, $args);
if ($eventRs->stopped()) {
return $eventRs->last();
}
$this->memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $options['namespace']);
$expiration = $this->expirationTime($options['ttl']);
if (!$this->memcached->replace($key, $value, $expiration)) {
throw $this->getExceptionByResultCode($this->memcached->getResultCode());
}
$result = true;
return $this->triggerPost(__FUNCTION__, $args, $result);
} catch (\Exception $e) {
return $this->triggerException(__FUNCTION__, $args, $e);
}
}
示例10: _get_lock
/**
* Get lock
*
* Acquires an (emulated) lock.
*
* @param string $session_id Session ID
* @return bool
*/
protected function _get_lock($session_id)
{
// PHP 7 reuses the SessionHandler object on regeneration,
// so we need to check here if the lock key is for the
// correct session ID.
if ($this->_lock_key === $this->_key_prefix . $session_id . ':lock') {
return $this->_memcached->replace($this->_lock_key, time(), 300) ? $this->_success : $this->_failure;
}
// 30 attempts to obtain a lock, in case another request already has it
$lock_key = $this->_key_prefix . $session_id . ':lock';
$attempt = 0;
do {
if ($this->_memcached->get($lock_key)) {
sleep(1);
continue;
}
if (!$this->_memcached->set($lock_key, time(), 300)) {
log_message('error', 'Session: Error while trying to obtain lock for ' . $this->_key_prefix . $session_id);
return $this->_failure;
}
$this->_lock_key = $lock_key;
break;
} while (++$attempt < 30);
if ($attempt === 30) {
log_message('error', 'Session: Unable to obtain lock for ' . $this->_key_prefix . $session_id . ' after 30 attempts, aborting.');
return $this->_failure;
}
$this->_lock = TRUE;
return $this->_success;
}
示例11: replace
/**
* @Name replace
*
* @param string|array $key 要替换的key
* @param mixed $value 要替换的value
* @param int|null $expiration 到期时间
* @return bool add by cheng.yafei
*
*/
public function replace($key = NULL, $value = NULL, $expiration = NULL)
{
if (is_null($expiration)) {
$expiration = C(Config::MEMCACHE_EXPIRATION);
}
if (is_array($key)) {
foreach ($key as $multi) {
if (!isset($multi['expiration']) || $multi['expiration'] == '') {
$multi['expiration'] = $expiration;
}
$this->replace($multi['key'], $multi['value'], $multi['expiration']);
}
$replace_status = true;
} else {
$this->local_cache[$this->key_name($key)] = $value;
$replace_status = false;
switch ($this->client_type) {
case 'Memcache':
$replace_status = $this->m->replace($this->key_name($key), $value, C(Config::MEMCACHE_COMPRESSION), $expiration);
break;
case 'Memcached':
$replace_status = $this->m->replace($this->key_name($key), $value, $expiration);
break;
}
}
return $replace_status;
}
示例12: _get_lock
/**
* Get lock
*
* Acquires an (emulated) lock.
*
* @param string $session_id Session ID
* @return bool
*/
protected function _get_lock($session_id)
{
if (isset($this->_lock_key)) {
return $this->_memcached->replace($this->_lock_key, time(), 300);
}
// 30 attempts to obtain a lock, in case another request already has it
$lock_key = $this->_key_prefix . $session_id . ':lock';
$attempt = 0;
do {
if ($this->_memcached->get($lock_key)) {
sleep(1);
continue;
}
if (!$this->_memcached->set($lock_key, time(), 300)) {
log_message('error', 'Session: Error while trying to obtain lock for ' . $this->_key_prefix . $session_id);
return FALSE;
}
$this->_lock_key = $lock_key;
break;
} while (++$attempt < 30);
if ($attempt === 30) {
log_message('error', 'Session: Unable to obtain lock for ' . $this->_key_prefix . $session_id . ' after 30 attempts, aborting.');
return FALSE;
}
$this->_lock = TRUE;
return TRUE;
}
示例13: write
/**
* Write
*
* Writes (create / update) session data
*
* @param string $session_id Session ID
* @param string $session_data Serialized session data
* @return bool
*/
public function write($session_id, $session_data)
{
if (!isset($this->_memcached)) {
return $this->_fail();
} elseif ($session_id !== $this->_session_id) {
if (!$this->_release_lock() or !$this->_get_lock($session_id)) {
return $this->_fail();
}
$this->_fingerprint = md5('');
$this->_session_id = $session_id;
}
if (isset($this->_lock_key)) {
$key = $this->_key_prefix . $session_id;
$this->_memcached->replace($this->_lock_key, time(), 300);
if ($this->_fingerprint !== ($fingerprint = md5($session_data))) {
if ($this->_memcached->replace($key, $session_data, $this->_config['expiration']) or $this->_memcached->getResultCode() === Memcached::RES_NOTFOUND && $this->_memcached->set($key, $session_data, $this->_config['expiration'])) {
$this->_fingerprint = $fingerprint;
return $this->_success;
}
return $this->_fail();
}
if ($this->_memcached->touch($key, $this->_config['expiration']) or $this->_memcached->getResultCode() === Memcached::RES_NOTFOUND && $this->_memcached->set($key, $session_data, $this->_config['expiration'])) {
return $this->_success;
}
}
return $this->_fail();
}
示例14: replaceItem
/**
* Replace an item.
*
* Options:
* - namespace <string> optional
* - The namespace to use (Default: namespace of object)
* - tags <array> optional
* - An array of tags
*
* @param string $key
* @param mixed $value
* @param array $options
* @return boolean
* @throws Exception
*
* @triggers replaceItem.pre(PreEvent)
* @triggers replaceItem.post(PostEvent)
* @triggers replaceItem.exception(ExceptionEvent)
*/
public function replaceItem($key, $value, array $options = array())
{
$baseOptions = $this->getOptions();
if (!$baseOptions->getWritable()) {
return false;
}
$this->normalizeOptions($options);
$this->normalizeKey($key);
$args = new ArrayObject(array('key' => &$key, 'value' => &$value, 'options' => &$options));
try {
$eventRs = $this->triggerPre(__FUNCTION__, $args);
if ($eventRs->stopped()) {
return $eventRs->last();
}
$internalKey = $options['namespace'] . $baseOptions->getNamespaceSeparator() . $key;
if (!$this->memcached->get($internalKey)) {
throw new Exception\ItemNotFoundException("Key '{$internalKey}' doesn't exist");
}
$result = $this->memcached->replace($internalKey, $value, $options['ttl']);
if ($result === false) {
$type = is_object($value) ? get_class($value) : gettype($value);
throw new Exception\RuntimeException("Memcached::replace('{$internalKey}', <{$type}>, {$options['ttl']}) failed");
}
return $this->triggerPost(__FUNCTION__, $args, $result);
} catch (\Exception $e) {
return $this->triggerException(__FUNCTION__, $args, $e);
}
}
示例15: internalReplaceItem
/**
* Internal method to replace an existing item.
*
* Options:
* - ttl <float>
* - The time-to-life
* - namespace <string>
* - The namespace to use
*
* @param string $normalizedKey
* @param mixed $value
* @param array $normalizedOptions
* @return boolean
* @throws Exception\ExceptionInterface
*/
protected function internalReplaceItem(&$normalizedKey, &$value, array &$normalizedOptions)
{
$this->memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $normalizedOptions['namespace']);
$expiration = $this->expirationTime($normalizedOptions['ttl']);
if (!$this->memcached->replace($normalizedKey, $value, $expiration)) {
throw $this->getExceptionByResultCode($this->memcached->getResultCode());
}
return true;
}