public function incrBy($chiave, $valore = 1, $scadenza = Cache::SCADENZA_DEFAULT) { $x = parent::incrBy($chiave, $valore); if ($scadenza) { parent::expire($chiave, $scadenza); } return $x; }
public static function increment($host) { $count = self::$redis->incrBy(self::$prefix . $host, 1); if (self::$redis->get(self::$prefix . $host) == 1) { self::$redis->setTimeout(self::$prefix . $host, self::$ttl); } return $count; }
public function incr($key, $offset = 1, $group = 'default') { if (empty($group)) { $group = 'default'; } if ($this->_connected && !in_array($group, $this->_no_redis_groups)) { try { $this->_redis->incrBy($this->_get_redis_key($key, $group), $offset); } catch (Exception $e) { $this->_connected = false; return false; } if (!$this->_exists($key, $group)) { $this->get($key, $group); } else { $this->_cache[$this->_get_prefix($group)][$group][$key] += $offset; } } else { if (!$this->_exists($key, $group)) { $this->_cache[$this->_get_prefix($group)][$group][$key] = 0; } $this->_cache[$this->_get_prefix($group)][$group][$key] += $offset; } return true; }
/** * Increases the value * * @param string $key * @param int $value * @return void */ public function increment($key, $value = 1) { if ($value == 1) { $this->_redis->incr($this->_prefix . $key); } else { $this->_redis->incrBy($this->_prefix . $key, $value); } }
public function incrBy($key='', $count='') { try { return $this->_redis->incrBy($key, $count); } catch (Exception $e) { $this->_error = $e->getMessage(); return false; } }
public function testDifferentTypeHash() { $key = '{hash}hash'; $dkey = '{hash}hash'; $this->redis->del($key); $this->assertEquals(1, $this->redis->hSet($key, 'key', 'value')); // string I/F $this->assertEquals(FALSE, $this->redis->get($key)); $this->assertEquals(FALSE, $this->redis->getset($key, 'value2')); $this->assertEquals(FALSE, $this->redis->append($key, 'append')); $this->assertEquals(FALSE, $this->redis->getRange($key, 0, 8)); $this->assertEquals(array(FALSE), $this->redis->mget(array($key))); $this->assertEquals(FALSE, $this->redis->incr($key)); $this->assertEquals(FALSE, $this->redis->incrBy($key, 1)); $this->assertEquals(FALSE, $this->redis->decr($key)); $this->assertEquals(FALSE, $this->redis->decrBy($key, 1)); // lists I/F $this->assertEquals(FALSE, $this->redis->rPush($key, 'lvalue')); $this->assertEquals(FALSE, $this->redis->lPush($key, 'lvalue')); $this->assertEquals(FALSE, $this->redis->lLen($key)); $this->assertEquals(FALSE, $this->redis->lPop($key)); $this->assertEquals(FALSE, $this->redis->lrange($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->lTrim($key, 0, 1)); $this->assertEquals(FALSE, $this->redis->lGet($key, 0)); $this->assertEquals(FALSE, $this->redis->lSet($key, 0, "newValue")); $this->assertEquals(FALSE, $this->redis->lrem($key, 'lvalue', 1)); $this->assertEquals(FALSE, $this->redis->lPop($key)); $this->assertEquals(FALSE, $this->redis->rPop($key)); $this->assertEquals(FALSE, $this->redis->rPoplPush($key, $dkey . 'lkey1')); // sets I/F $this->assertEquals(FALSE, $this->redis->sAdd($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->srem($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->sPop($key)); $this->assertEquals(FALSE, $this->redis->sMove($key, $dkey . 'skey1', 'sValue1')); $this->assertEquals(FALSE, $this->redis->scard($key)); $this->assertEquals(FALSE, $this->redis->sismember($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->sInter($key, $dkey . 'skey2')); $this->assertEquals(FALSE, $this->redis->sUnion($key, $dkey . 'skey4')); $this->assertEquals(FALSE, $this->redis->sDiff($key, $dkey . 'skey7')); $this->assertEquals(FALSE, $this->redis->sMembers($key)); $this->assertEquals(FALSE, $this->redis->sRandMember($key)); // sorted sets I/F $this->assertEquals(FALSE, $this->redis->zAdd($key, 1, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRem($key, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zIncrBy($key, 1, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRank($key, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRevRank($key, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRange($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->zRevRange($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->zRangeByScore($key, 1, 2)); $this->assertEquals(FALSE, $this->redis->zCount($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->zCard($key)); $this->assertEquals(FALSE, $this->redis->zScore($key, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRemRangeByRank($key, 1, 2)); $this->assertEquals(FALSE, $this->redis->zRemRangeByScore($key, 1, 2)); }
/** * {@inheritdoc} */ public function increment($key, $value = 1) { if ($value < 1) { throw new \InvalidArgumentException('Value of incrementation must be greater that nil.'); } if ($value === 1) { return $this->redis->incr($key); } return $this->redis->incrBy($key, $value); }
/** * @param string $key * @param string $value = 1 * @return int */ public function incrBy($key, $value = 1) { try { $this->_useCnt++; return parent::incrBy($key, $value); } catch (Exception $e) { Wk::logger()->err($e->getMessage()); } return 0; }
/** * Perform a calculation. * * @param string $key * @param int|float $byHowMuch * @param string $type * @return $this * @throws \Exception */ private function calculation($key, $byHowMuch, $type) { $byHowMuch = $byHowMuch === false ? 1 : $byHowMuch; if ($this->exists($key)) { $result = $type == "add" ? $this->redis->incrBy($key, $byHowMuch) : $this->redis->decrBy($key, $byHowMuch); if ($result === false) { throw new \Exception("This value can not be used in calculations"); } } else { throw new \Exception("You can not perform calculations from a value which is not set."); } return $this; }
public function testIncr() { $this->redis->set('key', 0); $this->redis->incr('key'); $this->assertEquals(1, (int) $this->redis->get('key')); $this->redis->incr('key'); $this->assertEquals(2, (int) $this->redis->get('key')); $this->redis->incr('key', 3); $this->assertEquals(5, (int) $this->redis->get('key')); $this->redis->incrBy('key', 3); $this->assertEquals(8, (int) $this->redis->get('key')); $this->redis->incrBy('key', 1); $this->assertEquals(9, (int) $this->redis->get('key')); $this->redis->incrBy('key', -1); $this->assertEquals(8, (int) $this->redis->get('key')); $this->redis->delete('key'); $this->redis->set('key', 'abc'); $this->redis->incr('key'); $this->assertTrue("abc" === $this->redis->get('key')); $this->redis->incr('key'); $this->assertTrue("abc" === $this->redis->get('key')); }
public function testIncr() { $this->redis->set('key', 0); $this->redis->incr('key'); $this->assertEquals(1, (int) $this->redis->get('key')); $this->redis->incr('key'); $this->assertEquals(2, (int) $this->redis->get('key')); $this->redis->incr('key', 3); $this->assertEquals(5, (int) $this->redis->get('key')); $this->redis->incrBy('key', 3); $this->assertEquals(8, (int) $this->redis->get('key')); $this->redis->incrBy('key', 1); $this->assertEquals(9, (int) $this->redis->get('key')); $this->redis->incrBy('key', -1); $this->assertEquals(8, (int) $this->redis->get('key')); $this->redis->delete('key'); $this->redis->set('key', 'abc'); $this->redis->incr('key'); $this->assertTrue("abc" === $this->redis->get('key')); $this->redis->incr('key'); $this->assertTrue("abc" === $this->redis->get('key')); // incrbyfloat $this->redis->delete('key'); $this->redis->set('key', 0); $this->redis->incrbyfloat('key', 1.5); $this->assertEquals('1.5', $this->redis->get('key')); $this->redis->incrbyfloat('key', 2.25); $this->assertEquals('3.75', $this->redis->get('key')); $this->redis->incrbyfloat('key', -2.25); $this->assertEquals('1.5', $this->redis->get('key')); $this->redis->set('key', 'abc'); $this->redis->incrbyfloat('key', 1.5); $this->assertTrue("abc" === $this->redis->get('key')); $this->redis->incrbyfloat('key', -1.5); $this->assertTrue("abc" === $this->redis->get('key')); }
/** * 给缓存值加上一个数 * * @param string $key 缓存键 * @param mix $value 增加的值 */ public function increase($key, $value = 1) { return $this->connect->incrBy($key, $value); }
/** * key值自增或者自减 * @param $key string key名 * @param $type int 0:自减 1:自增 默认为1 * @param $n int 自增步长 默认为1 */ public static function deinc($key, $type = 1, $n = 1) { $redis = new \Redis(); $redis->connect(self::_HOST, self::_PORT); $return = null; $n = (int) $n; switch ($type) { case 0: if ($n == 1) { $return = $redis->decr($key); } else { if ($n > 1) { $return = $redis->decrBy($key, $n); } } break; case 1: if ($n == 1) { $return = $redis->incr($key); } else { if ($n > 1) { $return = $redis->incrBy($key, $n); } } break; default: $return = false; break; } $redis->close(); $redis = null; return $return; }
<?php $redis = new Redis(); $redis->connect('127.0.0.1'); $redis->set('counter', 0); $redis->incrBy('counter', 7); $counter = $redis->get('counter'); print $counter;
/** * 递增 * 与原始increment方法区别的是若不存指定KEY时返回false,这个会自动递增 * * @param string $key * @param int $offset */ public function increment($key, $offset = 1, $lifetime = 60) { return $this->_redis->incrBy($key, $offset); }
$redis->setex('key', 3600, 'value'); $redis->psetex('key', 100, 'value'); $redis->setnx('key', 'value'); $redis->set('key1', 'val1'); $redis->set('key2', 'val2'); $redis->set('key3', 'val3'); $redis->set('key4', 'val4'); $redis->delete('key1', 'key2'); /* return 2 */ $redis->delete(array('key3', 'key4')); /* return 2 */ $redis->set('key', 'value'); $redis->exists('NonExistingKey'); $redis->incr('key1'); /* 4 */ $redis->incrBy('key1', 10); /* 14 */ $redis->decr('key1'); /* -2 */ $redis->decr('key1'); /* -3 */ $redis->decrBy('key1', 10); /* -13 */ $redis->mGet(array('key1', 'key2', 'key3')); $redis->mGet(array('key0', 'key1', 'key5')); $redis->set('x', '42'); $exValue = $redis->getSet('x', 'lol'); // return '42', replaces x by 'lol' $newValue = $redis->get('x'); // return 'lol' $redis->select(0);
/** * @inheritdoc */ public function increment($name, $offset = 1) { return $this->driver->incrBy($name, $offset); }
/** * 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 (int) $this->_Redis->incrBy($key, $offset); }
/** * {@inheritDoc} * * @link https://github.com/phpredis/phpredis#incr-incrby */ protected function _increment($key, $value) { return $this->_connection->incrBy($key, $value); }