Exemplo n.º 1
0
 /**
  * Increment cache item with given key
  *
  * @param string $key
  * @return integer|false
  */
 public function increment($key)
 {
     if (empty($key)) {
         throw new InvalidArgumentException("\$key is empty");
     }
     return $this->memcache->increment($key);
 }
Exemplo n.º 2
0
 public function increment($key, $value)
 {
     try {
         return $this->instance->increment($key, $value);
     } catch (BaseException $e) {
         return null;
     }
 }
 /**
  * Increment the value of the given key
  *
  * @param string $key
  * @return boolean
  */
 public function increment($key)
 {
     if (!$this->hasMemcache) {
         return false;
     }
     $this->memcache->increment($key);
     return true;
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function inc($name, $delta = 1)
 {
     if (!$this->has($name)) {
         $this->forever($name, $delta);
         return $delta;
     }
     return $this->driver->increment($name, $delta);
 }
Exemplo n.º 5
0
 /**
  * @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, MEMCACHE_COMPRESSED, $expire);
     }
     return $this->storage->increment($hash, $offset);
 }
Exemplo n.º 6
0
 /**
  * Get global handle for memcache access
  *
  * @return object Memcache
  */
 public function get_memcache()
 {
     if (!isset($this->memcache)) {
         // no memcache support in PHP
         if (!class_exists('Memcache')) {
             $this->memcache = false;
             return false;
         }
         $this->memcache = new Memcache();
         $this->mc_available = 0;
         // add all configured hosts to pool
         $pconnect = $this->config->get('memcache_pconnect', true);
         foreach ($this->config->get('memcache_hosts', array()) as $host) {
             if (substr($host, 0, 7) != 'unix://') {
                 list($host, $port) = explode(':', $host);
                 if (!$port) {
                     $port = 11211;
                 }
             } else {
                 $port = 0;
             }
             $this->mc_available += intval($this->memcache->addServer($host, $port, $pconnect, 1, 1, 15, false, array($this, 'memcache_failure')));
         }
         // test connection and failover (will result in $this->mc_available == 0 on complete failure)
         $this->memcache->increment('__CONNECTIONTEST__', 1);
         // NOP if key doesn't exist
         if (!$this->mc_available) {
             $this->memcache = false;
         }
     }
     return $this->memcache;
 }
 /**
  * Increments the value of an integer cached key
  *
  * @param string $key Identifier for the data
  * @param integer $offset How much to increment
  * @return New incremented value, false otherwise
  * @throws CacheException when you try to increment with compress = true
  */
 public function increment($key, $offset = 1)
 {
     if ($this->settings['compress']) {
         throw new CacheException(__d('cake_dev', 'Method increment() not implemented for compressed cache in %s', __CLASS__));
     }
     return $this->_Memcache->increment($key, $offset);
 }
Exemplo n.º 8
0
function cache($op, $key, $val = '', $expiry = 604800)
{
    static $memcache = false;
    if ($memcache === false) {
        $memcache = new Memcache();
        $memcache->connect('localhost', 11211) or die('Fatal error - could not connect to Memcache');
    }
    $retval = true;
    // Prefix the key to avoid collisions with other apps
    $key = 'twitapps_' . $key;
    switch ($op) {
        case 'set':
            $memcache->set($key, $val, false, $expiry) or die('Fatal error - could not store ' . htmlentities($key) . ' in Memcache');
            break;
        case 'get':
            $retval = $memcache->get($key);
            break;
        case 'inc':
            $retval = $memcache->increment($key);
            break;
        case 'add':
            $retval = $memcache->add($key, $val, false, $expiry);
            break;
    }
    return $retval;
}
 /**
  * Increments the value of an integer cached key
  *
  * @param string $key
  *        	Identifier for the data
  * @param integer $offset
  *        	How much to increment
  * @param integer $duration
  *        	How long to cache the data, in seconds
  * @return New incremented value, false otherwise
  * @access public
  */
 function increment($key, $offset = 1)
 {
     if ($this->settings['compress']) {
         trigger_error(sprintf(__('Method increment() not implemented for compressed cache in %s', true), get_class($this)), E_USER_ERROR);
     }
     return $this->__Memcache->increment($key, $offset);
 }
Exemplo n.º 10
0
 function inc($key)
 {
     $key = str_replace('\\', '/', $key);
     if (!$this->memcache->increment($key)) {
         $message = sprintf('Memcache::increment() with key "%s" failed', $key);
         \ManiaLib\Utils\Logger::error($message);
     }
 }
Exemplo n.º 11
0
 public function increment($key, $value = 1)
 {
     if (!$this->is_open) {
         return false;
     }
     $key = $this->format_key($key);
     return parent::increment($key, $value);
 }
 /**
  * Clears the cache by incrementing the namespace key
  *
  * @return void
  */
 public function clearCache($partition = '')
 {
     if (!$this->connected) {
         return false;
     }
     // Memcache has a built in increment method
     $this->memcache->increment($partition);
 }
Exemplo n.º 13
0
 public function increment($key, $value = 1)
 {
     if (!isset($this->memcache)) {
         $this->connect();
     }
     $key = $this->prefix . $key;
     return $this->memcache->increment($key, $value);
 }
Exemplo n.º 14
0
 protected function incrementCachedVal($cacheKey)
 {
     if (self::$memcache) {
         if ($this->cachePrefix) {
             $cacheKey = $this->cachePrefix . "-" . $cacheKey;
         }
         return self::$memcache->increment($cacheKey);
     }
     return False;
 }
Exemplo n.º 15
0
 /**
  * 递增
  * 与原始increment方法区别的是若memcache不存指定KEY时返回false,这个会自动递增
  *
  * @param string $key
  * @param int $offset
  * @param int $lifetime 当递减失则时当作set使用
  */
 public function increment($key, $offset = 1, $lifetime = 60)
 {
     if ($this->_memcache->increment($this->prefix . $key, $offset)) {
         return true;
     } elseif (null === $this->get($key) && $this->set($key, $offset, $lifetime)) {
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 16
0
 /**
  * Use key as a counter and add integet value to it
  */
 public function counter_add($key, $value)
 {
     if ($value == 0) {
         return true;
     }
     $storage_key = $this->get_item_key($key);
     $r = @$this->_memcache->increment($storage_key, $value);
     if (!$r) {
         // it doesnt initialize counter by itself
         $this->counter_set($key, 0);
     }
     return $r;
 }
Exemplo n.º 17
0
 /**
  * 递减
  * @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) {
             if ($this->handler->set($key, $value = -$step, 0)) {
                 return $value;
             }
         } else {
             //memcache会将数字存储为字符串
             if (!is_numeric($value)) {
                 return false;
             }
             $timeValue = self::getValue($this->handler->get(self::timeKey($key)));
             //未设置过期时间或未过期
             if ($timeValue === false || isset($timeValue['expire_time']) && $timeValue['expire_time'] > time()) {
                 //memcache 新的元素的值不会小于0
                 if ($value < 0 || $step > 0 && $value < $step) {
                     if ($this->handler->set($key, $value -= $step, 0)) {
                         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, 0)) {
                 return $value;
             }
         }
         return false;
     } catch (Exception $ex) {
         self::exception($ex);
         //连接状态置为false
         $this->isConnected = false;
     }
     return false;
 }
Exemplo n.º 18
0
 /**
  * Get global handle for memcache access
  *
  * @return object Memcache
  */
 public function get_memcache()
 {
     if (!isset($this->memcache)) {
         // no memcache support in PHP
         if (!class_exists('Memcache')) {
             $this->memcache = false;
             return false;
         }
         $this->memcache = new Memcache();
         $this->memcache_init();
         // test connection and failover (will result in $this->mc_available == 0 on complete failure)
         $this->memcache->increment('__CONNECTIONTEST__', 1);
         // NOP if key doesn't exist
         if (!$this->mc_available) {
             $this->memcache = false;
         }
     }
     return $this->memcache;
 }
Exemplo n.º 19
0
 /**
  * @inheritdoc
  */
 public function increment($name, $offset = 1)
 {
     return $this->driver->increment($name, $offset);
 }
Exemplo n.º 20
0
function memcache_increment($m, $key, $value = 1)
{
    return Memcache::increment($key, $value);
}
Exemplo n.º 21
0
 /**
  *
  * @param string $key
  * @param int $value
  * @return number
  * @todo 多备份实例上无原子操作
  */
 public function increase($key, $value = 1)
 {
     if ($this->backupInstance) {
         //集群操作
         $backupReturn = $this->backupInstance->increase($key, $value);
     }
     $key = $this->makeRealKey($key);
     $return = parent::increment($key, $value);
     if ($return) {
         $newValue = parent::get($key);
         if (false !== $newValue) {
             $backupValue['data'] = $newValue;
             parent::set($this->getBackupKey($key), $backupValue, MEMCACHE_COMPRESSED, 0);
         }
     }
     return $return;
 }
Exemplo n.º 22
0
 /**
  * Increments a given value by one
  *
  * @param String $key The key for the value
  * @return \r8\Cache\Memcache Returns a self reference
  */
 public function increment($key)
 {
     $this->connect();
     $this->link->increment($key);
     return $this;
 }
Exemplo n.º 23
0
 /**
  * Increments the value of an integer cached key
  *
  * @param string $key Identifier for the data
  * @param int $offset How much to increment
  * @return New incremented value, false otherwise
  * @throws CacheException when you try to increment with compress = true
  */
 public function increment($key, $offset = 1)
 {
     return $this->_Memcached->increment($key, $offset);
 }
Exemplo n.º 24
0
 public function increment($id)
 {
     return false !== @$this->memcache->increment($id);
 }
Exemplo n.º 25
0
 /**
  * Increments a given value by the step value supplied.
  * Useful for shared counters and other persistent integer based
  * tracking.
  *
  * @param   string    id of cache entry to increment
  * @param   int       step value to increment by
  * @return  integer
  * @return  boolean
  */
 public function increment($id, $step = 1)
 {
     return $this->_memcache->increment($id, $step);
 }
Exemplo n.º 26
0
Arquivo: Memcache.php Projeto: n8b/VMN
 /**
  * Increments the key and returns the new value.
  *
  * @param $key
  * @return int
  */
 public function inc($key)
 {
     $this->cas($key, 0);
     return $this->memcached->increment($key);
 }
Exemplo n.º 27
0
/**
 * Increments a cached item's value. The value must be a int, float or string
 * representing an integer e.g. 5, 5.0 or "5" or the call with fail.
 *
 * @param Memcache $memcache_obj The cache instance to increment the value in.
 *
 * @param string $key The key associated with the value to increment.
 *
 * @param int $value The amount to increment the value.
 *
 * @return mixed On success, the new value of the item is returned. On
 *               failure, false is returned.
 */
function memcache_increment($memcache_obj, $key, $value = 1)
{
    return $memcache_obj->increment($key, $value);
}
Exemplo n.º 28
0
<?php

$memcache = new Memcache();
$host = '127.0.0.1';
$port = '11211';
$memcache->connect($host, $port);
$memcache->set('class_name', '1', 0, 3600);
$memcache->increment('class_name', '2');
$result = $memcache->get('class_name');
$stats = $memcache->getStats();
var_dump($stats);
$memcache->flush();
$memcache->close();
Exemplo n.º 29
0
 /**
  * Increments the group value to simulate deletion of all keys under a group
  * old values will remain in storage until they expire.
  *
  * @param string $group The group to clear.
  * @return boolean success
  */
 public function clearGroup($group)
 {
     return (bool) $this->_Memcache->increment($this->settings['prefix'] . $group);
 }
Exemplo n.º 30
0
 /**
  * {@inheritDoc}
  *
  * @see http://www.php.net/manual/en/memcache.increment.php
  */
 protected function _increment($key, $value)
 {
     return $this->_connection->increment($key, $value);
 }