Chapter 4: set of redis array structure and some general commands

Store set, set does not allow duplicate elements;

Set the get element:

127.0.0.1:6379> sadd set1 a b c
(integer) 3
127.0.0.1:6379> SMEMBERS set1
1) "a"
2) "c"
3) "b"

Insert the same element and keep only one:

127.0.0.1:6379> sadd set1 a e f
(integer) 2
127.0.0.1:6379> smembers set1
1) "a"
2) "e"
3) "c"
4) "b"
5) "f"

Remove multiple elements:

127.0.0.1:6379> SMEMBERS set1
1) "c"
2) "b"
3) "e"
4) "f"
127.0.0.1:6379> serm set1 c f
(error) ERR unknown command 'serm'
127.0.0.1:6379> srem set1 c f
(integer) 2
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
simember key Determines whether an element exists, returns 1 for existence, and returns 0 for non-existence
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
127.0.0.1:6379> SMEMBERS key e
(error) ERR wrong number of arguments for 'smembers' command
127.0.0.1:6379> SISMEMBER key e
(integer) 0
127.0.0.1:6379> SISMEMBER set1 and
(integer) 1
127.0.0.1:6379> SISMEMBER set1 r
(integer) 0

sdiff uses the previous set as a reference to calculate the difference set:

127.0.0.1:6379> sadd set2 b c d e
(integer) 4
127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
4) "b"
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
127.0.0.1:6379> SDIFF set2 set3
1) "d"
2) "e"
3) "b"
4) "c"
127.0.0.1:6379> SDIFF set3 set2
(empty list or set)
127.0.0.1:6379> SDIFF set2 set1
1) "d"
2) "c"

sinter computes the intersection sunion computes the union:

127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
4) "b"
127.0.0.1:6379> SINTER set1 set2
1) "e"
2) "b"
127.0.0.1:6379> SUNION set1 set2
1) "d"
2) "e"
3) "c"
4) "b"

scard calculates the total number of elements:

127.0.0.1:6379> SCARD set1
(integer) 2
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"

srandmember randomly picks one or more numbers:

127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
4) "b"
127.0.0.1:6379> SRANDMEMBER set2
"e"
127.0.0.1:6379> SRANDMEMBER set2 2
1) "d"
2) "c"

sdiffstore stores the difference result in a new set

127.0.0.1:6379> SMEMBERS set2
1) "d"
2) "e"
3) "c"
4) "b"
127.0.0.1:6379> SMEMBERS set1
1) "e"
2) "b"
127.0.0.1:6379> SDIFFSTORE r set1 set2
(integer) 0
127.0.0.1:6379> SDIFFSTORE r set2 set1
(integer) 2
127.0.0.1:6379> SMEMBERS r
1) "d"
2) "c"

sunionstore stores the intersection result in a new collection:

127.0.0.1:6379> SUNIONSTORE r2 set1 set2
(integer) 4
127.0.0.1:6379> SMEMBERS r2
1) "d"
2) "e"
3) "c"
4) "b"


Let's talk about the data structure of sorted-set ordered sorting, that is, the elements of the set are arranged in order:

zadd adds elements, here we take grades and names as examples, here are arranged from small to high grades:

127.0.0.1:6379> zadd sort2 2 xiaoming 3 xiaohua 1 xiaohong
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoming"
3) "xiaohua"

zadd adds elements, updates if there are, and adds if not, that is, there are no duplicate values:

127.0.0.1:6379> zadd sort2 2 xiaoming 3 xiaohua 1 xiaohong
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoming"
3) "xiaohua"
127.0.0.1:6379> ZADD sort2 6 xiaoming
(integer) 0
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohua"
3) "xiaoming"
127.0.0.1:6379> zadd sort2 4 xiaoliang
(integer) 1
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohua"
3) "xiaoliang"
4) "xiaoming"

zrem removes elements:

127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohua"
3) "xiaoliang"
4) "xiaoming"
127.0.0.1:6379> zrem sort2 xiaohua
(integer) 1
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoliang"
3) "xiaoming"

zcard to view the number of elements:

127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoliang"
3) "xiaoming"
127.0.0.1:6379> ZCARD sort2
(integer) 3

Display both scores and names:

127.0.0.1:6379> ZRANGE sort2 0 -1 withscores
1) "xiaohong"
2) "1"
3) "xiaoliang"
4) "4"
5) "xiaoming"
6) "6"

zrevrange in descending order:

127.0.0.1:6379> ZREVRANGE sort2 0 -1 withscores
1) "xiaoming"
2) "6"
3) "xiaoliang"
4) "4"
5) "xiaohong"
6) "1"

Add two elements, delete elements according to the ranking, delete the first three here:

127.0.0.1:6379> ZADD sort2 2 xiaohuang
(integer) 1
127.0.0.1:6379> ZADD sort2 3 xiaozi
(integer) 1
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohuang"
3) "xiaozi"
4) "xiaoliang"
5) "xiaoming"
127.0.0.1:6379> ZREMRANGEBYRANK sort2 0 2
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaoliang"
2) "xiaoming"

zremrangebyscore removes elements based on a specific score range:

127.0.0.1:6379> ZRANGE sort2 0 -1 withscores
 1) "xiaohong"
 2) "1"
 3) "xiaohuang"
 4) "2"
 5) "xiaozi"
 6) "3"
 7) "xiaoliang"
 8) "4"
 9) "xiaoming"
10) "6"
127.0.0.1:6379> ZREMRANGEBYscore sort2 2 5
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaoming"

Add more elements, and then look up according to the score range:

127.0.0.1:6379> zadd sort2 2 xiaohuang 3 xiaozi 4 xiaoliang
(integer) 3
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohong"
2) "xiaohuang"
3) "xiaozi"
4) "xiaoliang"
5) "xiaoming"
127.0.0.1:6379> ZRANGEBYSCORE sort2 2 5
1) "xiaohuang"
2) "xiaozi"
3) "xiaoliang"

limit is similar to paging lookup:

127.0.0.1:6379>  ZRANGE sort2 0 -1 withscores
 1) "xiaohong"
 2) "1"
 3) "xiaohuang"
 4) "2"
 5) "xiaozi"
 6) "3"
 7) "xiaoliang"
 8) "4"
 9) "xiaoming"
10) "6"
127.0.0.1:6379> ZRANGEBYSCORE sort2 0 9 limit 0 2
1) "xiaohong"
2) "xiaohuang"
127.0.0.1:6379> ZRANGEBYSCORE sort2 0 9 withscores limit 0 2
1) "xiaohong"
2) "1"
3) "xiaohuang"
4) "2"

Zincrby bonus points:

127.0.0.1:6379> ZINCRBY sort2 10 xiaohong
"11"
127.0.0.1:6379> ZRANGE sort2 0 -1
1) "xiaohuang"
2) "xiaozi"
3) "xiaoliang"
4) "xiaoming"
5) "xiaohong"
127.0.0.1:6379> ZRANGE sort2 0 -1 withscores
 1) "xiaohuang"
 2) "2"
 3) "xiaozi"
 4) "3"
 5) "xiaoliang"
 6) "4"
 7) "xiaoming"
 8) "6"
 9) "xiaohong"
10) "11"

zcount counts the number within a certain scoring range:

127.0.0.1:6379> ZRANGE sort2 0 -1 withscores
 1) "xiaohuang"
 2) "2"
 3) "xiaozi"
 4) "3"
 5) "xiaoliang"
 6) "4"
 7) "xiaoming"
 8) "6"
 9) "xiaohong"
10) "11"
127.0.0.1:6379> ZCOUNT sort2 0 20
(integer) 5


Some common operations of redis:

Keyword lookup key:

127.0.0.1:6379> keys s*
1) "sort2"
2) "set2"
3) "set1"
4) "sort"
5) "sort1"

Find keys that start with s and are followed by only one character:

127.0.0.1:6379> set s1 aa
OK
127.0.0.1:6379> key s*
(error) ERR unknown command 'key'
127.0.0.1:6379> keys s?
1) "s1"

del removes an element:

127.0.0.1:6379> of luck s1
(integer) 2
127.0.0.1:6379> keys s*
1) "sort2"
2) "set2"
3) "set1"
4) "sort1"

rename rename:

127.0.0.1:6379> set s2 ss
OK
127.0.0.1:6379> rename s2 s3
OK
127.0.0.1:6379> get s3
"ss"

expire sets the effective time in seconds:

127.0.0.1:6379> EXPIRE s3 12
(integer) 1
127.0.0.1:6379> s3 ttl
(error) ERR unknown command 's3'
127.0.0.1:6379> ttl s3
(integer) -2
127.0.0.1:6379> get s3
(nil)

type to view the data type:

127.0.0.1:6379> type sort2
zset
127.0.0.1:6379> type name
string
127.0.0.1:6379> type l1
list



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940002&siteId=291194637