当前位置: 首页>>代码示例>>PHP>>正文


PHP Memcached::decrement方法代码示例

本文整理汇总了PHP中Memcached::decrement方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcached::decrement方法的具体用法?PHP Memcached::decrement怎么用?PHP Memcached::decrement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Memcached的用法示例。


在下文中一共展示了Memcached::decrement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: 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);
     }
 }
开发者ID:ranxin1022,项目名称:zf2,代码行数:51,代码来源:Memcached.php

示例2: 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;
    }
开发者ID:necrogami,项目名称:zf2,代码行数:40,代码来源:Memcached.php

示例3: decrement

 /**
  * Decrement a numeric item's value.
  *
  * @link http://www.php.net/manual/en/memcached.decrement.php
  *
  * @param string    $key    The key under which to store the value.
  * @param int       $offset The amount by which to decrement 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 decrement($key, $offset = 1, $group = 'default')
 {
     $derived_key = $this->buildKey($key, $group);
     // Decrement values in no_mc_groups
     if (in_array($group, $this->no_mc_groups)) {
         // Only decrement if the key already exists and value is 0 or greater (mimics memcached behavior)
         if (isset($this->cache[$derived_key]) && $this->cache[$derived_key] >= 0) {
             // If numeric, subtract; 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->decrement($derived_key, $offset);
     if (Memcached::RES_SUCCESS === $this->getResultCode()) {
         $this->add_to_internal_cache($derived_key, $result);
     }
     return $result;
 }
开发者ID:Ramoonus,项目名称:wp-spider-cache,代码行数:38,代码来源:class-object-cache.php

示例4: 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, '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->decrement($internalKey, $value);
         if ($newValue === false) {
             if ($this->memcached->get($internalKey) !== false) {
                 throw new Exception\RuntimeException("Memcached::decrement('{$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);
     }
 }
开发者ID:nevvermind,项目名称:zf2,代码行数:50,代码来源:Memcached.php

示例5: dec

 /**
  * Decrease a stored number
  *
  * @param string $key
  * @param int $step
  * @return int | bool
  */
 public function dec($key, $step = 1)
 {
     $result = self::$cache->decrement($this->getPrefix() . $key, $step);
     if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
         return false;
     }
     return $result;
 }
开发者ID:stweil,项目名称:owncloud-core,代码行数:15,代码来源:Memcached.php

示例6: decrement

 public function decrement($key, $value)
 {
     $this->ensureTriedToConnect();
     try {
         return $this->instance->decrement($key, $value);
     } catch (BaseException $e) {
         return null;
     }
 }
开发者ID:justthefish,项目名称:hesper,代码行数:9,代码来源:PeclMemcached.php

示例7: decrement

 /**
  * @inheritdoc
  */
 public function decrement($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, 1, $expire);
     }
     return $this->storage->decrement($hash, $offset);
 }
开发者ID:romeoz,项目名称:rock-cache,代码行数:14,代码来源:Memcached.php

示例8: decr

 /**
  * 递减
  * @param string $key   键名
  * @param int $step     递减步长
  * @return int|false    递减后的值,失败返回false
  */
 public function decr($key, $step = 1)
 {
     if (!is_int($step)) {
         return false;
     }
     try {
         $value = $this->handler->get($key);
         if ($value === false && $this->handler->getResultCode() === \Memcached::RES_NOTFOUND) {
             if ($this->handler->set($key, $value = -$step)) {
                 return $value;
             }
         } else {
             //memcache会将数字存储为字符串
             if (!is_numeric($value)) {
                 return false;
             }
             $timeValue = self::getValue($this->handler->get(self::timeKey($key)));
             //未设置过期时间或未过期
             if ($timeValue === false && $this->handler->getResultCode() === \Memcached::RES_NOTFOUND || isset($timeValue['expire_time']) && $timeValue['expire_time'] > time()) {
                 //memcache 新的元素的值不会小于0
                 if ($value < 0 || $step > 0 && $value < $step) {
                     if ($this->handler->set($key, $value -= $step)) {
                         return $value;
                     }
                 } else {
                     if ($step > 0) {
                         $ret = $this->handler->decrement($key, $step);
                         return $ret;
                     } else {
                         return $this->handler->increment($key, -$step);
                     }
                 }
             }
             //已过期,重新设置
             if ($this->handler->set($key, $value = $step)) {
                 return $value;
             }
         }
         return false;
     } catch (Exception $ex) {
         self::exception($ex);
         //连接状态置为false
         $this->isConnected = false;
     }
     return false;
 }
开发者ID:iliubang,项目名称:LinkCache,代码行数:52,代码来源:Memcached.php

示例9: decrement

 /**
  * {@inheritdoc}
  */
 public function decrement($key, $amount = 1, $options = array())
 {
     if (!$this->online()) {
         return Gdn_Cache::CACHEOP_FAILURE;
     }
     $finalOptions = array_merge($this->StoreDefaults, $options);
     $initial = val(Gdn_Cache::FEATURE_INITIAL, $finalOptions, null);
     $expiry = val(Gdn_Cache::FEATURE_EXPIRY, $finalOptions, null);
     $requireBinary = $initial || $expiry;
     $initial = !is_null($initial) ? $initial : 0;
     $expiry = !is_null($expiry) ? $expiry : 0;
     $tryBinary = $this->option(Memcached::OPT_BINARY_PROTOCOL, false) & $requireBinary;
     $realKey = $this->makeKey($key, $finalOptions);
     switch ($tryBinary) {
         case false:
             $decremented = $this->memcache->decrement($realKey, $amount);
             if (is_null($decremented) && $initial) {
                 $decremented = $this->memcache->set($realKey, $initial);
                 if ($decremented) {
                     $decremented = $initial;
                 }
             }
             break;
         case true:
             $decremented = $this->memcache->decrement($realKey, $amount, $initial, $expiry);
             break;
     }
     // Check if things went ok
     $ok = $this->lastAction($realKey);
     if (!$ok) {
         return Gdn_Cache::CACHEOP_FAILURE;
     }
     if ($decremented !== false) {
         Gdn_Cache::localSet($realKey, $decremented);
         return $decremented;
     }
     return Gdn_Cache::CACHEOP_FAILURE;
 }
开发者ID:mcnasby,项目名称:datto-vanilla,代码行数:41,代码来源:class.memcached.php

示例10: decrement

 /**
  * 减少整数数据的值
  *
  * @param string $key
  * @param int $offset
  * @return bool
  */
 public function decrement($key, $offset = 1)
 {
     return $this->memcached ? $this->memcached->decrement($key, $offset) : false;
 }
开发者ID:liyu9092,项目名称:zphp,代码行数:11,代码来源:Memcached.php

示例11: _counter

 /**
  * Получение текущего evid или инкремент текущего evid и получения нового.
  * Здесь необходимо подключаться к memcached напрямую без memBuff и всех его "обвесов", чтобы
  * делать инкремент штатными средствами. Если делать это с помощью set/get может возникнуть
  * ситуация когда два процесса получат одинковый evid, сделают инкремент и оба перезапишут
  * одинаковое значение, из-за этого все сломается :).
  * 
  * evid это счетчик событий. Когда создается новое событие, счетчик инкрементируется.
  * Каждое подключение знает какое последние событие оно выполнило и по evid ориентируется есть ли
  * новые.
  * 
  * @param bool $inc если true -> инкрементирует evid и возвращает новое значение, иначе текущее
  *
  * @return int новое или текущее значение evid
  */
 private function _counter($name, $inc = 0)
 {
     if (empty($this->_memcache) || $inc) {
         $this->_memcache = new Memcached();
         $svk = $GLOBALS[memBuff::SERVERS_VARKEY];
         if (empty($GLOBALS[$svk])) {
             $svk = 'memcachedServers';
         }
         if (!($servers = $GLOBALS[$svk])) {
             self::error(1, true);
         }
         foreach ($servers as $s) {
             $bIsConnected = $this->_memcache->addServer($s, 11211);
         }
         if (!$bIsConnected) {
             self::error(1, true);
         }
     }
     $key = self::MEMBUFF_COUNTERS_KEY . $this->_uid . ':' . $name . (defined('SERVER') ? SERVER : '');
     if ($inc) {
         $v = $inc > 0 ? $this->_memcache->increment($key, $inc) : $this->_memcache->decrement($key, $inc * -1);
     } else {
         $v = $this->_memcache->get($key);
     }
     if ($v === false) {
         $start = $name == 'connects' ? 1 : 0;
         $this->_memcache->set($key, $start, $name == 'evid' ? 0 : self::CONNECT_TTL * 1.3);
     }
     return (int) $v;
 }
开发者ID:kapai69,项目名称:fl-ru-damp,代码行数:45,代码来源:QChat.php

示例12: dec

 /**
  * Decrease a stored number
  *
  * @param string $key
  * @param int $step
  * @return int | bool
  */
 public function dec($key, $step = 1)
 {
     return self::$cache->decrement($this->getPrefix() . $key, $step);
 }
开发者ID:brunomilet,项目名称:owncloud-core,代码行数:11,代码来源:memcached.php

示例13: decrement

 /**
  * {@inheritdoc}
  */
 public function decrement($name, $delta = 1)
 {
     return $this->service->decrement($name, $delta);
 }
开发者ID:jwdeitch,项目名称:components,代码行数:7,代码来源:MemcachedDriver.php

示例14: decrement

 public function decrement($key, $by_value)
 {
     return $this->memcached->decrement($key, $by_value);
 }
开发者ID:jamm,项目名称:memory,代码行数:4,代码来源:MemcachedDecorator.php

示例15: testPassingNullKey

 public function testPassingNullKey()
 {
     $memcached = new Memcached();
     $this->assertFalse($memcached->add(null, 1));
     $this->assertFalse($memcached->replace(null, 1));
     $this->assertFalse($memcached->set(null, 1));
     $this->assertFalse($memcached->increment(null));
     $this->assertFalse($memcached->decrement(null));
 }
开发者ID:spudder-com,项目名称:spudder-django,代码行数:9,代码来源:MemcachedTest.php


注:本文中的Memcached::decrement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。