本文整理汇总了PHP中Memcached::increment方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcached::increment方法的具体用法?PHP Memcached::increment怎么用?PHP Memcached::increment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Memcached
的用法示例。
在下文中一共展示了Memcached::increment方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: increment
/**
* increment
*
* @param mixed $identifier identifier to increment the value for
*
* @return boolean/int
*/
public function increment($identifier = null)
{
if (!$identifier) {
throw new \InvalidArgumentException('identifier is a required argument');
}
return $this->memcached->increment($this->fileName($identifier));
}
示例2: inc
function inc($key)
{
$key = str_replace('\\', '/', $key);
if (!$this->memcached->increment($key)) {
$message = sprintf('Memcache::increment() with key "%s" failed', $key);
\ManiaLib\Utils\Logger::error($message);
}
}
示例3: increment
public function increment($key, $value)
{
$this->ensureTriedToConnect();
try {
return $this->instance->increment($key, $value);
} catch (BaseException $e) {
return null;
}
}
示例4: collect
public function collect()
{
$newValue = $this->memcached->increment($this->key);
if (false !== $newValue) {
return $this;
}
if (\Memcached::RES_NOTFOUND !== $this->memcached->getResultCode()) {
throw new \Exception('Error collecting value');
}
$this->memcached->set($this->key, 1, time() + $this->timeInterval);
}
示例5: incrementItem
/**
* Increment 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 on failure
* @throws Exception
*
* @triggers incrementItem.pre(PreEvent)
* @triggers incrementItem.post(PostEvent)
* @triggers incrementItem.exception(ExceptionEvent)
*/
public function incrementItem($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, 'options' => &$options));
try {
$eventRs = $this->triggerPre(__FUNCTION__, $args);
if ($eventRs->stopped()) {
return $eventRs->last();
}
$internalKey = $options['namespace'] . $baseOptions->getNamespaceSeparator() . $key;
$value = (int) $value;
$newValue = $this->memcached->increment($internalKey, $value);
if ($newValue === false) {
if ($this->memcached->get($internalKey) !== false) {
throw new Exception\RuntimeException("Memcached::increment('{$internalKey}', {$value}) failed");
} elseif (!$options['ignore_missing_items']) {
throw new Exception\ItemNotFoundException("Key '{$internalKey}' not found");
}
$this->addItem($key, $value, $options);
$newValue = $value;
}
return $this->triggerPost(__FUNCTION__, $args, $newValue);
} catch (\Exception $e) {
return $this->triggerException(__FUNCTION__, $args, $e);
}
}
示例6: 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);
}
示例7: internalIncrementItem
/**
* Internal method to increment 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 internalIncrementItem(& $normalizedKey, & $value, array & $normalizedOptions)
{
$this->memcached->setOption(MemcachedResource::OPT_PREFIX_KEY, $normalizedOptions['namespace']);
$value = (int)$value;
$newValue = $this->memcached->increment($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: increment
/**
* Increment a numeric item's value.
*
* @link http://www.php.net/manual/en/memcached.increment.php
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to increment the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Returns item's new value on success or FALSE on failure.
*/
public function increment($key, $offset = 1, $group = 'default')
{
$derived_key = $this->buildKey($key, $group);
// Increment values in no_mc_groups
if (in_array($group, $this->no_mc_groups)) {
// Only increment if the key already exists and the number is currently 0 or greater (mimics memcached behavior)
if (isset($this->cache[$derived_key]) && $this->cache[$derived_key] >= 0) {
// If numeric, add; otherwise, consider it 0 and do nothing
if (is_numeric($this->cache[$derived_key])) {
$this->cache[$derived_key] += (int) $offset;
} else {
$this->cache[$derived_key] = 0;
}
// Returned value cannot be less than 0
if ($this->cache[$derived_key] < 0) {
$this->cache[$derived_key] = 0;
}
return $this->cache[$derived_key];
} else {
return false;
}
}
$result = $this->mc->increment($derived_key, $offset);
if (Memcached::RES_SUCCESS === $this->getResultCode()) {
$this->add_to_internal_cache($derived_key, $result);
}
return $result;
}
示例9: incrementItem
/**
* Increment 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 on failure
* @throws Exception
*
* @triggers incrementItem.pre(PreEvent)
* @triggers incrementItem.post(PostEvent)
* @triggers incrementItem.exception(ExceptionEvent)
*/
public function incrementItem($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->increment($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);
}
}
示例10: inc
/**
* Increase a stored number
*
* @param string $key
* @param int $step
* @return int | bool
*/
public function inc($key, $step = 1)
{
$this->add($key, 0);
$result = self::$cache->increment($this->getPrefix() . $key, $step);
$this->verifyReturnCode();
return $result;
}
示例11: floodProtect
/**
* Flood protection with Memcached
*
* @param string $misprintHash
* @throws Exception if report is flood-positive
* @return boolean
*/
protected function floodProtect($misprintHash)
{
if (!$this->memcached) {
return false;
}
$ip = $this->getIP();
if ($ip !== false) {
$mcIpHash = 'newrphus:byIP:' . md5($ip);
$attemptsCount = $this->memcached->get($mcIpHash);
if ($this->memcached->getResultCode() === 0) {
if ($attemptsCount > $this->attemptsThreshold) {
throw new Exception("Too many attempts", 429);
}
$this->memcached->increment($mcIpHash);
} else {
$this->memcached->set($mcIpHash, 1, 300);
}
}
$mcTextHash = 'newrphus:byText:' . $misprintHash;
$this->memcached->get($mcTextHash);
if ($this->memcached->getResultCode() === 0) {
throw new Exception("This misprint already was sent", 202);
}
$this->memcached->set($mcTextHash, true, 300);
return true;
}
示例12: inc
/**
* {@inheritdoc}
*/
public function inc($name, $delta = 1)
{
if (!$this->has($name)) {
$this->forever($name, $delta);
return $delta;
}
return $this->driver->increment($name, $delta);
}
示例13: inc
/**
* Increase a stored number
*
* @param string $key
* @param int $step
* @return int | bool
*/
public function inc($key, $step = 1)
{
$this->add($key, 0);
$result = self::$cache->increment($this->getPrefix() . $key, $step);
if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
return false;
}
return $result;
}
示例14: increment
/**
* @inheritdoc
*/
public function increment($key, $offset = 1, $expire = 0, $create = true)
{
$hash = $this->prepareKey($key);
if ($this->exists($key) === false) {
if ($create === false) {
return false;
}
$this->storage->add($hash, 0, $expire);
}
return $this->storage->increment($hash, $offset);
}
示例15: cleanP
/**
* (non-PHPdoc)
* @see \Cachearium\Backend\CacheRAM::cleanP()
*/
public function cleanP($base, $id)
{
// @codeCoverageIgnoreStart
if (!$this->enabled) {
throw new NotCachedException();
}
// @codeCoverageIgnoreEnd
$group = $this->getGroupString(new CacheKey($base, $id));
parent::cleanP($base, $id);
$this->memcached->increment($group);
return true;
}