public function decr($key, $offset = 1, $group = 'default') { if (empty($group)) { $group = 'default'; } if ($this->_connected && !in_array($group, $this->_no_redis_groups)) { try { $this->_redis->decrBy($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; }
/** * Decreases the value * * @param string $key * @param int $value * @return void */ public function decrement($key, $value = 1) { if ($value == 1) { $this->_redis->decr($this->_prefix . $key); } else { $this->_redis->decrBy($this->_prefix . $key, $value); } }
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)); }
/** * 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 testDecr() { $this->redis->set('key', 5); $this->redis->decr('key'); $this->assertEquals(4, (int) $this->redis->get('key')); $this->redis->decr('key'); $this->assertEquals(3, (int) $this->redis->get('key')); $this->redis->decr('key', 2); $this->assertEquals(1, (int) $this->redis->get('key')); $this->redis->decr('key', 2); $this->assertEquals(-1, (int) $this->redis->get('key')); $this->redis->decrBy('key', 2); $this->assertEquals(-3, (int) $this->redis->get('key')); $this->redis->decrBy('key', 1); $this->assertEquals(-4, (int) $this->redis->get('key')); $this->redis->decr('key', -10); $this->assertEquals(6, (int) $this->redis->get('key')); }
/** * Decrements the value of an integer cached key * * @param string $key Identifier for the data * @param int $offset How much to subtract * @return New decremented value, false otherwise * @throws CacheException when you try to decrement with compress = true */ public function decrement($key, $offset = 1) { return (int) $this->_Redis->decrBy($key, $offset); }
/** * 递减 * * 与原始decrement方法区别的是若不存指定KEY时返回false,这个会自动递减 * * @param string $key * @param int $offset */ public function decrement($key, $offset = 1, $lifetime = 60) { return $this->_redis->decrBy($key, $offset); }
/** * {@inheritDoc} * * @link https://github.com/phpredis/phpredis#decr-decrby */ protected function _decrement($key, $value) { return $this->_connection->decrBy($key, $value); }
/** * 给缓存值减去一个数 * * @param string $key 缓存键 * @param mix $value 减少的值 */ public function decrease($key, $value = 1) { return $this->connect->decrBy($key, $value); }
$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); // switch to DB 0 $redis->set('x', '42'); // write 42 to x $redis->move('x', 1); // move to DB 1 $redis->select(1);
/** * @inheritdoc */ public function decrement($name, $offset = 1) { return $this->driver->decrBy($name, $offset); }
/** * 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; }