本文整理汇总了PHP中Memcached::add方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcached::add方法的具体用法?PHP Memcached::add怎么用?PHP Memcached::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Memcached
的用法示例。
在下文中一共展示了Memcached::add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: add
/**
* 新增key
* @param $key
* @param $val
* @param $expireTime
* @return bool
*/
public function add($key, $val, $expireTime)
{
$ret = $this->dbh->add($key, $val, $expireTime);
if ($ret === false) {
$this->error('add', $key);
}
return true;
}
示例2: add
function add($key, $value, $ttl = 0)
{
$key = str_replace('\\', '/', $key);
if (!$this->memcached->add($key, $value, $ttl)) {
$message = sprintf('Memcache::set() with key "%s" failed', $key);
\ManiaLib\Utils\Logger::error($message);
}
}
示例3: add
public function add($id, $value, $ttl = null)
{
$ttl = $ttl ?: 0;
if ($ttl > 0) {
$ttl = time() + $ttl;
}
return $this->memcached->add($id, $value, $ttl);
}
示例4: exists
public function exists($id)
{
$success = $this->memcached->add($id, 0);
if (!$success) {
return true;
}
$this->memcached->delete($id);
return false;
}
示例5: lock
/**
* Lock the mutex
*
* @param string $mutexKey Identifier of the mutex.
* @param int $expiry How long in seconds to keep the mutex locked just in
* case the script dies. 0 = never expires.
*
* @throws LockFailedException
*/
public function lock($mutexKey, $expiry)
{
// Try and set the value. If it fails check to see if
// it already contains our ID
if (!$this->_memcache->add($mutexKey, $this->_getLockId(), $expiry)) {
if (!$this->isLocked($mutexKey)) {
throw new LockFailedException();
}
}
}
示例6: acquire
/**
* 上锁
*
* @param string $key
* @return boolean
*/
public function acquire($key)
{
$key = $this->prefix . $key;
$time = time();
while (time() - $time < $this->max_timeout) {
if ($this->memcached->add($key, $this->Identifier, $this->timeout)) {
return true;
}
usleep($this->retry_wait_usec);
}
return false;
}
示例7: internalDecrementItem
/**
* Internal method to decrement an item.
*
* Options:
* - ttl <float>
* - The time-to-life
* - namespace <string>
* - The namespace to use
*
* @param string $normalizedKey
* @param int $value
* @param array $normalizedOptions
* @return int|boolean The new value on success, false on failure
* @throws Exception\ExceptionInterface
*/
protected function internalDecrementItem(& $normalizedKey, & $value, array & $normalizedOptions)
{
$this->memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $normalizedOptions['namespace']);
$value = (int)$value;
$newValue = $this->memcached->decrement($normalizedKey, $value);
if ($newValue === false) {
$rsCode = $this->memcached->getResultCode();
// initial value
if ($rsCode == MemcachedResource::RES_NOTFOUND) {
$newValue = -$value;
$expiration = $this->expirationTime($normalizedOptions['ttl']);
$this->memcached->add($normalizedKey, $newValue, $expiration);
$rsCode = $this->memcached->getResultCode();
}
if ($rsCode) {
throw $this->getExceptionByResultCode($rsCode);
}
}
return $newValue;
}
示例8: doIncrement
/**
* {@inheritdoc}
*/
public function doIncrement($key, $increment = 1, $initialValue = 0, $expiration = null, array $options = array())
{
$key = $this->getKey($key, $options);
/* Only the binary protocol supports initial value, in which case the implementation is simpler. */
if ($this->isBinaryProtocolActive()) {
$result = $this->client->increment($key, $increment, $initialValue, $expiration);
return new CacheResponse($result, $this->isSuccess(), $this->isSuccess() || $this->isNotFound());
}
/* If the binary protocol is disable we must implement the initial value logic. */
$result = $this->client->increment($key, $increment);
/* In case or success or any error aside "not found" there's nothing more to do. */
if ($this->isSuccess() || !$this->isNotFound()) {
return new CacheResponse($result, true, true);
}
/**
* Try to add the key; notice that "add" is used instead of "set", to ensure we do not override
* the value in case another process already set it.
*/
$result = $this->client->add($key, $increment + $initialValue, $expiration);
/* Created the key successfully. */
if ($this->isSuccess()) {
return new CacheResponse($increment + $initialValue, true, true);
}
/* The key was not stored because is already existed, try to increment a last time. */
if ($this->isNotStored()) {
$result = $this->client->increment($key, $increment);
return new CacheResponse($result, $this->isSuccess(), $this->isSuccess() || $this->isNotFound());
}
return new CacheResponse($result, false, false);
}
示例9: mSetNX
/**
* 批量设置键值(当键名不存在时);<br>
* 只有当键值全部设置成功时,才返回true,否则返回false并尝试回滚
* @param array $sets 键值数组
* @return boolean 是否成功
*/
public function mSetNX($sets)
{
try {
$keys = [];
$status = true;
foreach ($sets as $key => $value) {
$status = $this->handler->add($key, self::setValue($value));
if ($status) {
$keys[] = $key;
} else {
break;
}
}
//如果失败,尝试回滚,但不保证成功
if (!$status) {
foreach ($keys as $key) {
$this->handler->delete($key);
}
}
return $status;
} catch (Exception $ex) {
self::exception($ex);
//连接状态置为false
$this->isConnected = false;
}
return false;
}
示例10: lockindex
/**
* Lock cache index
*
* @return boolean True on success, false otherwise.
*
* @since 12.1
*/
protected function lockindex()
{
$looptime = 300;
$data_lock = self::$_db->add($this->_hash . '-index_lock', 1, 30);
if ($data_lock === false)
{
$lock_counter = 0;
// Loop until you find that the lock has been released. that implies that data get from other thread has finished
while ($data_lock === false)
{
if ($lock_counter > $looptime)
{
return false;
break;
}
usleep(100);
$data_lock = self::$_db->add($this->_hash . '-index_lock', 1, 30);
$lock_counter++;
}
}
return true;
}
示例11: addItem
/**
* Add 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 addItem.pre(PreEvent)
* @triggers addItem.post(PostEvent)
* @triggers addItem.exception(ExceptionEvent)
*/
public function addItem($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->add($internalKey, $value, $options['ttl'])) {
if ($this->memcached->get($internalKey) !== false) {
throw new Exception\RuntimeException("Key '{$internalKey}' already exists");
}
$type = is_object($value) ? get_class($value) : gettype($value);
throw new Exception\RuntimeException("Memcached::add('{$internalKey}', <{$type}>, {$options['ttl']}) failed");
}
$result = true;
return $this->triggerPost(__FUNCTION__, $args, $result);
} catch (\Exception $e) {
return $this->triggerException(__FUNCTION__, $args, $e);
}
}
示例12: decrementItem
/**
* Decrement an item.
*
* Options:
* - namespace <string> optional
* - The namespace to use (Default: namespace of object)
* - ignore_missing_items <boolean> optional
* - Throw exception on missing item or return false
*
* @param string $key
* @param int $value
* @param array $options
* @return int|boolean The new value or false or failure
* @throws Exception
*
* @triggers decrementItem.pre(PreEvent)
* @triggers decrementItem.post(PostEvent)
* @triggers decrementItem.exception(ExceptionEvent)
*/
public function decrementItem($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']);
$value = (int) $value;
$newValue = $this->memcached->decrement($key, $value);
if ($newValue === false) {
if (($rsCode = $this->memcached->getResultCode()) != 0 && ($rsCode != MemcachedResource::RES_NOTFOUND || !$options['ignore_missing_items'])) {
throw $this->getExceptionByResultCode($rsCode);
}
$expiration = $this->expirationTime($options['ttl']);
if (!$this->memcached->add($key, -$value, $expiration)) {
throw $this->getExceptionByResultCode($this->memcached->getResultCode());
}
$newValue = -$value;
}
return $this->triggerPost(__FUNCTION__, $args, $newValue);
} catch (\Exception $e) {
return $this->triggerException(__FUNCTION__, $args, $e);
}
}
示例13: add
/**
* 添加数据
* @param string|array $key
* @param mixed $value
* @param int $expiration
* @return array|bool
*/
public function add($key = NULL, $value = NULL, $expiration = 0)
{
if (is_null($expiration)) {
$expiration = C(Config::MEMCACHE_EXPIRATION);
}
$expiration = intval($expiration);
if (is_array($key)) {
foreach ($key as $multi) {
if (!isset($multi['expiration']) || $multi['expiration'] == '') {
$multi['expiration'] = $expiration;
}
$this->add($this->key_name($multi['key']), $multi['value'], $multi['expiration']);
}
$add_status = true;
} else {
$keyName = $this->key_name($key);
$this->local_cache[$keyName] = $value;
switch ($this->client_type) {
case 'Memcache':
$add_status = $this->m->add($keyName, $value, C(Config::MEMCACHE_COMPRESSION), $expiration);
break;
default:
case 'Memcached':
$add_status = $this->m->add($keyName, $value, $expiration);
break;
}
}
return $add_status;
}
示例14: add
/**
* Adds a value to cache.
*
* If the specified key already exists, the value is not stored and the function
* returns false.
*
* @link http://www.php.net/manual/en/memcached.add.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 add($key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false)
{
/**
* Ensuring that wp_suspend_cache_addition is defined before calling, because sometimes an advanced-cache.php
* file will load object-cache.php before wp-includes/functions.php is loaded. In those cases, if wp_cache_add
* is called in advanced-cache.php before any more of WordPress is loaded, we get a fatal error because
* wp_suspend_cache_addition will not be defined until wp-includes/functions.php is loaded.
*/
if (function_exists('wp_suspend_cache_addition') && wp_suspend_cache_addition()) {
return 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)) {
// Add does not set the value if the key exists; mimic that here
if (isset($this->cache[$derived_key])) {
return false;
}
$this->add_to_internal_cache($derived_key, $value);
return true;
}
// Save to Memcached
if (false !== $byKey) {
$result = $this->mc->addByKey($server_key, $derived_key, $value, $expiration);
} else {
$result = $this->mc->add($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;
}
示例15: add
/**
* Set a value in the cache if it's not already stored
*
* @param string $key
* @param mixed $value
* @param int $ttl Time To Live in seconds. Defaults to 60*60*24
* @return bool
* @throws \Exception
*/
public function add($key, $value, $ttl = 0)
{
$result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
$this->verifyReturnCode();
}
return $result;
}