public function testDecr() { $this->redis->set('key', 5); $this->redis->decr('key'); $this->assertEquals(4, $this->redis->get('key')); $this->redis->decr('key'); $this->assertEquals(3, $this->redis->get('key')); $this->redis->decr('key', 2); $this->assertEquals(1, $this->redis->get('key')); }
/** * 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)); }
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')); }
/** * Decrement a raw value * * @param string $id Cache ID * @param int $offset Step/value to reduce by * @return mixed New value on success or FALSE on failure */ public function decrement($id, $offset = 1) { return $this->_redis->decr($id, $offset); }
// 还存在商品,将用户放入一个数组中 // 判断用户是否已经抢过了 $exist = 0; $userArr = getUserList(); foreach ($userArr as $key => $user) { if ($userArr[$key]['ip'] == $ip) { $exist = 1; break; } } if ($exist) { $arr['msg'] = $ip . '您已经抢到了商品,不能重复抢哦!'; } else { $userInfo = array('ip' => $ip, 'addtime' => microtime()); $redis->rPush('userlist', json_encode($userInfo)); $redis->decr('goods_count'); $arr['succ'] = 'T'; $arr['msg'] = $userInfo['ip'] . '恭喜您,抢到了商品!'; } } else { // 不存在商品了,直接返回数据 $arr['msg'] = '商品已经抢光了,谢谢您的参与,下次再来吧'; } backjson($arr); } elseif ($act == 'user') { //获取中奖用户 $msg = ""; $userArr = getUserList(); if (!empty($userArr)) { foreach ($userArr as $user) { $msg .= "<tr>\r\n <td>" . $user['ip'] . "</td>\r\n <td>" . $user['addtime'] . "</td>\r\n </tr>";
/** * Decrement a raw value * * @param string $id Cache ID * @param int $offset Step/value to reduce by * @return mixed New value on success or FALSE on failure */ public function decrement($id, $offset = 1) { return $this->_redis->exists($id) ? $this->_redis->decr($id, $offset) : FALSE; }
/** * @param string $key * @return int */ public function decr($key) { try { $this->_useCnt++; return parent::decr($key); } catch (Exception $e) { Wk::logger()->err($e->getMessage()); } return 0; }
$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); // switch to DB 0 $redis->set('x', '42');
/** * Enter description here... * * @param string $p_sKey The Key * @param numeric $p_iDec The decremental value * @return numeric */ function decrement($p_sKey, $p_iDec = 1) { return $this->_handler->decr($p_sKey, $p_iDec); }
/** * 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; }
描述:数字递减存储键值。 参数:key value:将被添加到键的值 返回值:INT the new value incr 描述:数字递减存储键值。 参数:key value:将被添加到键的值 返回值:INT the new value */ $redis->set('test', "100"); var_dump($redis->incr("test")) . '<br>'; //结果:int(101) var_dump($redis->incr("test")) . '<br>'; //结果:int(102) $redis->set('test1', "10"); var_dump($redis->decr("test1")) . '<br>'; //结果:int(9) var_dump($redis->decr("test1")) . '<br>'; //结果:int(8) /** getMultiple 描述:取得所有指定键的值。如果一个或多个键不存在,该数组中该键的值为假 参数:其中包含键值的列表数组 返回值:返回包含所有键的值的数组 */ $arr = array('136502993', 'zhuwawa'); $int = 100; $string = 'my love....'; $redis->set('demo2', $arr); $redis->set('demo3', $int);