redis常用笔记(第一版)

1、SINTER

说明:多key之间取交集数据

key1 = {a,b,c,d} key2 = {c} key3 = {a,c,e} SINTER key1 key2 key3 = {c}

2、sadd

说明:添加队列数据

3、SUNION

说明:多key之间取并集数据

4、取出lRange多少,按区间查询,再移除取出的数据

$id = 1;

$key = 'phpTest:'.$id;

$result = $this->redis->lRange($key,0,1000);

$this->redis->lTrim($key,1000,-1);

echo '<pre>';

print_r($result);

$result1 = $this->redis->lRange($key,0,-1);

echo '<pre>';

print_r($result1);

5、redis加锁

protected function redisLock($flag, $expire = 1, $times = 1) {

    $lock = $this->redis->setnx($flag, time() + $expire);

    $now = time();

    //不成功

    while ($lock != 1) {

        $now++;

        $time = $now + $expire;

        //再重新创建

        $lock = $this->redis->setnx($flag, $time);

        if ($lock == 1 || ($now > $this->redis->get($flag) && $now > $this->redis->getset($flag, $time))) {//争锁

            break;

        } else {

            sleep(0.1);//0.5s

        }

    }

    return true;

}

/**

 * 释放锁

 * @param  String  $key 锁标识

 * @return Boolean

 */

protected function redisUnlock($key){

    return $this->redis->del($key);

}

6、哈希队列(不同List列队)

$redis->hSet('表名 = key','字段名','值');

$redis->hExists('表名','字段名');

$redis->hDel('表名','字段名');

$redis->hGetAll('表名'); // 这个数据不建议查超过上万的数据,会挂

$redis->hKeys('表名','字段名'); // 查询出字段名,这个很快

7、sAdd,sMembers集合,相对于set(字符串)而言,这个可以存放多个,相对于list(列表)而言,是去重的。

 后续还会更新,谢谢各位亲关注!

猜你喜欢

转载自www.cnblogs.com/FLy-1992/p/11464750.html
今日推荐