preface

Redis has a wealth of data structures, and knowing the common commands for these data structures and choosing the right data structure for the right scenario will make using Redis much more efficient

String

String is the most commonly used data structure in Redis. The value in key-value can be either a String or an integer. The following commands are commonly used

127.0.0.1:6379> set k1 1 OK Incr = 1 127.0.0.1:6379> incr k1 (INTEGER) 2 # decr = 1 127.0.0.1:6379> decr k1 (INTEGER) 1 # incrby = the specified number 127.0.0.1:6379> incrby k1 10 (INTEGER) 11 # decrby minus the specified number 127.0.0.1:6379> decrby k1 2 (INTEGER) 9 127.0.0.1:6379> set k2 Append = len+addlen<1MB; append = len+addlen<1MB; Append = len+addlen>1MB 127.0.0.1:6379> Append k2 def (INTEGER) 6 127.0.0.1:6379> get k2 "abcdef" # Add 127.0.0.1:6379> mset k3 v3 in batches K4 v4 k5 v5 OK # Return nil 127.0.0.1:6379> mget k3 k4 k6 1) "v3" 2) "v4" 3) (nil) # Add 127.0.0.1:6379> setnx k6 v6 (INTEGER) 1 if there is no key 127.0.0.1:6379> get k6 "v6" 127.0.0.1:6379> get k6 "v6" This command can also update the expiration time 127.0.0.1:6379> setex k7 20 v7 OK 127.0.0.1:6379> TTL k7 (INTEGER) 16 127.0.0.1:6379> set k7 abcdefg OK # Capture the string 127.0.0.1:6379> getrange k7 1 3 "BCD" # Rewrite the string 127.0.0.1:6379> setrange k7 4 ABC (INTEGER) 7 127.0.0.1:6379> get k7 "Abcdabc" 127.0.0.1:6379> setrange k7 4 abcd (INTEGER) 8 127.0.0.1:6379> get k7 "abcdabcd"Copy the code

Hash

The Hash in Redis is similar to the HashMap in Java. It is used to associate various data of a key, such as the name and age of a user. The following are the common commands

# Hash = 127.0.0.1:6379> hset user name zhangsan age 24 (INTEGER) 2 # Hash = 127.0.0.1:6379> hget user name "Zhangsan" 127.0.0.1:6379> hget user age "24" # delete field 127.0.0.1:6379> hdel user name (INTEGER) 1 127.0.0.1:6379> Value 127.0.0.1:6379> hlen user (INTEGER) 1 127.0.0.1:6379> hset user name Zhangsan (INTEGER) 1 127.0.0.1:6379> hlen user (integer) 2 # Checks whether the field contains 127.0.0.1:6379> hexists user age #. If yes, 1 is returned (INTEGER) 0 # View all keys 127.0.0.1:6379> hkeys user 1) "age" 2) "Name" (value 127.0.0.1:6379> hVALS user 1) "24" 2) "zhangsan"Copy the code

List

List in Redis is a two-way linked List, often used for asynchronous queues

127.0.0.1:6379> lpush list 2 1 3 (INTEGER) 3 127.0.0.1:6379> lpop list 1 1) "3 (INTEGER) 2 # Get element 127.0.0.1:6379> lindex list 0 "1" # Return all data 127.0.0.1:6379> lrange list 0-1 1) "1" 2) "2" # Return list 127.0.0.1:6379> lrange list 0 0 1) "1" 3 127.0.0.1:6379> lrange list 0-1 1) "3" 2) "3" 3) "3" 4) "6" 5) "5" 6) "4" 7) "3" 127.0.0.1:6379> lrem list 2 3 (INTEGER) 2 127.0.0.1:6379> lrange list 0-1 1) "3" 2) "6" 3) "5" 4) "4" 5) "3" 3 127.0.0.1:6379> lrange list 0-1 1) "3" 2) "6" 3) "5" 4) "4" 5) "3" 6) "3" 6) "3" 7) "3" 127.0.0.1:6379> lrem list-2 3 (INTEGER) 2 127.0.0.1:6379> lrange list 0-1 1) "3" 2) "6" 3) "5" 4) "4" 5) "3" 127.0.0.1:6379> lrange list 0-1 1) "3" 2) "6" 3) "5" 4) "4" 5) "3" 127.0.0.1:6379> lrem list 0 3 (INTEGER) 2 127.0.0.1:6379> lrange list 0-1 1) "6" 2) "5" 3) "4" 127.0.0.1:6379> ltrim list 0 0 OK 127.0.0.1:6379> lrange list 0-1 1) "6" # rpoplpush 127.0.0.1:6379> lrange list 0-1 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "6" 127.0.0.1:6379> lrange list 0-1 1) "6" 127.0.0.1:6379> lrange list 0-1 1) "1" 2) "2" 3) "3" 4) "4" 5)"Copy the code

Set

We can think of a Set as a Set in mathematics, where the elements of a Set are never repeated

# add element 127.0.0.1:6379> sadd set 12 3 4 5 6 (INTEGER) 6 127.0.0.1:6379> smembers set 1) "1" 2) "2" 3) "3" 4) "4" 5" 6) scard set (INTEGER) 6) scard set (integer) 127.0.0.1:6379> sismember set 1 (INTEGER) 1 127.0.0.1:6379> sismember set 10 (INTEGER) 0 # Remove elements from the set 127.0.0.1:6379> srem set 12 (INTEGER) 2 127.0.0.1:6379> smembers set 1) "3" 2) "4" 3) "5" 4) "6 127.0.0.1:6379> sadd set 12 3 4 5 6 (INTEGER) 6 127.0.0.1:6379> spop set 2 1) "5" 2) "6" 127.0.0.1:6379> smembers set 1) "1" 2) "2" 3) "3" 4) "4" 127.0.0.1:6379> srandmember set 1) "1" 127.0.0.1:6379> smembers set 1) "1" 2) "2" 3) "3"Copy the code

Set also provides commands for intersection, union, and difference sets

127.0.0.1:6379> smembers set 1) "1" 2) "2" 3) "3" 4) "4" 5) "59" 127.0.0.1:6379> sinter set set2 1) "2" 2) "3" 127.0.0.1:6379> sdiff set set2 1) "1" 2) "4" 3) "59"Copy the code
Application scenarios

A Set is a collection of elements that do not repeat themselves. This property allows us to use a Set to record unique data

For example, daily user check-in records of an application and daily user login records of an application

Redis sets also provide commands for intersection, union, and difference, which we can use to calculate many metrics

For example, if we want to obtain the next-day retention of users on a certain day, we only need to record the new users on a certain day in one set and the active users on the next day in another set. The intersection of the two sets is the next-day retention

For example, if we want to obtain the users who logged in for 7 consecutive days, we only need to record the users who logged in every day to a set. The intersection of sets for 7 consecutive days is the user who logged in for 7 consecutive days

For example, if we want to get the mutual friends of two users, we also want to find the intersection of the friends set of two users

Sorted Set

Ordered set is the interview of the high frequency test point, often used to do leaderboards, such as rank ranking, score ranking

Zadd sset 10 user1 (INTEGER) 1 127.0.0.1:6379> zadd sset 20 user2 (INTEGER) 1 127.0.0.1:6379> zadd sset Zadd sset 30 user3 (INTEGER) 1 127.0.0.1:6379> zrange sset 0-1 1) "user1" 2) "user2" 3) "user3 (integer) 1 127.0.0.1:6379> zrange sset 0-1 1) "user1" 2) "user3" # Check the score 127.0.0.1:6379> zscore sset user3" 30" # Zrevrange sset 0 1 withscores 1) "user3" 2) "30" 3) "user2" 4) "20" Zrevrank sset user3 (INTEGER) 0 127.0.0.1:6379> zRevRank sset user2 (INTEGER) 1 # Increases or decreases the player's score by 127.0.0.1:6379> zincrby sset 6 127.0.0.1:6379> zrevrange sset 0 2 withscores 1) "user3" 2) "30" 3) "user2" 4) "26" 5) "user1" 6) "10"Copy the code
Application scenarios

Sorted sets are often used for lists, such as scores and likes

Zadd key Score member, only need to write score, like number, member write user name, you can complete a leaderboard

And then there’s a little bit of an operation, if we wanted to rank by points, if we had equal points, by likes, what would you do?

Score can be used as a floating point number. As long as the integer bit of score is set to the number of points and the decimal place is set to the number of likes, the requirement can be realized naturally

HyperLogLog

# Add count 127.0.0.1:6379> pfadd user a (INTEGER) 1 127.0.0.1:6379> pfadd user b (INTEGER) 1 127.0.0.1:6379> pfadd user c (integer) 1 # Get count 127.0.0.1:6379> pfCount user (integer) 3 127.0.0.1:6379> pfadd user d (integer) 1 127.0.0.1:6379> pfcount user (integer) 4Copy the code
Application scenarios

Cardinality statistics are usually used to count the number of non-repeating elements in a set

For example, there is such a requirement, the product manager wants to know the DAILY visit to the website UV, and do not need to know the details of the user, how will you achieve?

Looked at the above data structure, you may have thought of a simple solution, using a separate set of storage each visited the user’s id, but if page views is very big, the size of the set is very breathtaking, just need to know the final result and the demand, don’t need to know detail, record all the detail that would be a little waste

At this time, we can use Redis’ HyperLogLog data structure to solve this problem. HyperLogLog provides an imprecise deweighting scheme, which is not accurate but not very inaccurate, with a standard error of 0.81%

GeoHash

Address coordinates are often implemented using a GeoHash

Juejin (INTEGER) 1 127.0.0.1:6379> GeoAdd Company 116.514203 39.905409 iReader (INTEGER) 1 127.0.0.1:6379> GeoAdd Company 116.489033 40.007669 Meituan (INTEGER) 1 # can be added in batches 127.0.0.1:6379> Xiaomi 116.562108 39.787602 JD 116.334255 40.027400 xiaomi (INTEGER) 2 # 127.0.0.1:6379> Geodist Company Juejin ireader km "10.5501" # Georadiusbymember company ireader 20 km count 3 asc 1) "ireader" 2) "juejin" 3) "meituan" # 127.0.0.1:6379> Georadius company 116.489033 40.007669 20 km withdist count 3 asc 1) 1) "meituan" 2) "0.0001" 2) 1) "ireader" 3) 1) "ireader" 3) "ireader"Copy the code

Generic class

127.0.0.1:6379> setex k200 2000 v200 OK # Object type 127.0.0.1:6379> type k200 String # Get expiration time 127.0.0.1:6379> TTL k200 (INTEGER) 1993 # Update expiration time 127.0.0.1:6379> expire k200 200 (integer) 1 127.0.0.1:6379> TTL k200 (integer) 197Copy the code