Redis常用命令(Set、Hash、Zset)

1、Set(单值多value)

  1、 sadd、smembers key 、

127.0.0.1:6379> SADD set01 1 1 2 2 3 3
(integer) 3
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"

  2、sismember value  (存在为1,不存在为0)

127.0.0.1:6379> SISMEMBER set01 1
(integer) 1
127.0.0.1:6379> SISMEMBER set01 x
(integer) 0

  3、scard  key (统计集合里面个数)

127.0.0.1:6379> scard set01 
(integer) 3

  4、srem key  value(删除里面有的元素)

127.0.0.1:6379> srem set01 4
(integer) 0
127.0.0.1:6379> srem set01 3
(integer) 1
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "2"

  5、srandmember key  随机数个数(在set中寻找随机数个数)

127.0.0.1:6379> srandmember set 3
1) "5"
2) "6"
3) "7"
127.0.0.1:6379> srandmember set 3
1) "3"
2) "4"
3) "7"

  6、spop key (随机出栈)

127.0.0.1:6379> sadd set01 1 2 3 4 5 6
(integer) 6
127.0.0.1:6379> spop set01
"3"
127.0.0.1:6379> spop set01
"5"
127.0.0.1:6379> spop set01
"6"
127.0.0.1:6379> SMEMBERS set01
1) "1"
2) "2"
3) "4"

  7、smove  key1   key2   key1中有的值 (将key1中的值移到key2)

127.0.0.1:6379> smove set01 set02 8
(integer) 1
127.0.0.1:6379> SMEMBERS set02
1) "8"

  8、数学集合 sdiff(差集)、sinter(交集)、sunion(并集)

127.0.0.1:6379> sadd set01 1 2 3 4 5
(integer) 5
127.0.0.1:6379> sadd set02 1 2 3 a b
(integer) 5
127.0.0.1:6379> sdiff set01 set02
1) "4"
2) "5"
127.0.0.1:6379> sinter set01 set02
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> sunion set01 set02
1) "5"
2) "1"
3) "3"
4) "2"
5) "a"
6) "b"
7) "4"

2、Hash(KV模式不变,但V也是一个键值对)

  1、hset 、 hget 、hmset 、hmget 、hgetall 、hdel

127.0.0.1:6379> hset user id 11
(integer) 1
127.0.0.1:6379> HGET user id
"11"
127.0.0.1:6379> hmset user name "zhangsan1" age 18 
OK
127.0.0.1:6379> hmget user  id name age
1) "11"
2) "zhangsan1"
3) "18"


127.0.0.1:6379> hgetall user
1) "id"
2) "11"
3) "name"
4) "zhangsan1"
5) "age"
6) "18"

127.0.0.1:6379> hdel user name
(integer) 1

  2、hexists  key   value(key)  

127.0.0.1:6379> HEXISTS user id
(integer) 1
127.0.0.1:6379> HEXISTS user n
(integer) 0
127.0.0.1:6379> HEXISTS user name
(integer) 0
127.0.0.1:6379> HEXISTS user age
(integer) 1

  3、hkeys 、 hvalue

127.0.0.1:6379> hkeys user
1) "id"
2) "age"
127.0.0.1:6379> HVALS user
1) "11"
2) "18"

  4、hincrby 、hincrbyfloat

127.0.0.1:6379> HINCRBY user age 78
(integer) 97
127.0.0.1:6379> HINCRBYFLOAT user age 12.3
"109.3"

  5、hsetnx (有的就加入不了)

127.0.0.1:6379> hsetnx user id 45
(integer) 0
127.0.0.1:6379> hsetnx user age1 45
(integer) 1

3、Zset

  1、zset  key  、zrange  key   0 -1  、zset  key   0 -1  withscores

127.0.0.1:6379> zadd zset01 60 v1 70 v2 80 v3 90 v4 100 v5 
(integer) 5
127.0.0.1:6379> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
127.0.0.1:6379> zrange zset01 0 -1 withscore
(error) ERR syntax error
127.0.0.1:6379> zrange zset01 0 -1 withscores
 1) "v1"
 2) "60"
 3) "v2"
 4) "70"
 5) "v3"
 6) "80"
 7) "v4"
 8) "90"
 9) "v5"
10) "100"

                      ------  withscores

 2、zrangebyscore key  起始分数  终止分数      ------- ( 不包括

                      ------  limit  作用返回限制 

  (1)查找分数在   起始分数  ------ 》 终止分数   的   value(值)

  (2)查找分数在    起始分数  ------ 》 终止分数  (不包括   分数  (分数     的value(值)

  (3)查找分数在     起始分数  ------ 》 终止分数   limit   起始下标    截取几位  的value(值)

127.0.0.1:6379> ZRANGEBYSCORE zset01 60 90
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> ZRANGEBYSCORE zset01 (60 (90
1) "v2"
2) "v3"
127.0.0.1:6379> ZRANGEBYSCORE zset01 60 90 limit 2 2
1) "v3"
2) "v4"

  3、zrem key

127.0.0.1:6379> zrem zset01 v5
(integer) 1
127.0.0.1:6379> ZRANGE zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> ZRANGE zset01 0 -1 withscores
1) "v1"
2) "60"
3) "v2"
4) "70"
5) "v3"
6) "80"
7) "v4"
8) "90"

  4、zcard key (求个数)

127.0.0.1:6379> zcard zset01
(integer) 4

  5、zcount  key  起始分数   终止分数(统计个数)

127.0.0.1:6379> zcount zset01 61 80
(integer) 2

  6、zrank key  key(value) :求下标   zscore  key   value  :求key(value)

127.0.0.1:6379> zrank zset01 v4
(integer) 3
127.0.0.1:6379> zscore zset01 v4

  7、zrevrank  key   key(value) :逆序获取下标

127.0.0.1:6379> ZREVRANK  zset01 v4
(integer) 0

  8、zrevange  key  key(value):逆序打印

127.0.0.1:6379> zrevrange zset01 0 -1
1) "v4"
2) "v3"
3) "v2"
127.0.0.1:6379> zrange zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"

 

posted @ 2019-07-29 16:37  编程小白1024  阅读(4962)  评论(0编辑  收藏  举报