This article is mainly to learn Dark horse programmer Redis entry to actual combat tutorial, in-depth analysis of the underlying principle of redis + redis distributed lock + enterprise solution + dark horse comment on the actual project income from
1 Introduction to Redis
Redis is a key-value NoSql database. The key-value type means that the data stored in Redis is stored in the form of key.value pairs, and the value can be in various forms, which can be strings, numbers, or even json etc.
redis has the following characteristics:
- key-value type, value supports a variety of different data structures with rich functions
- Single thread, each command is atomic
- Low latency, fast (memory based. IO multiplexing. Good coding).
- Support data persistence
- Support master-slave cluster and sharded cluster
- Multilingual client support
2 redis installation
This article relies on docker to install the redis single-instance image, and takes version 6.2.7 as an example
2.1 Searching for mirrors
docker search redis
2.2 redis related links
Official website: https://redis.io/
Mirror site: https://hub.docker.com/
download link: http://download.redis.io/releases
2.3 Pull the image
docker pull redis:6.2.7
2.4 Install the image
## Create a local directory, where a directory with multiple ports is created for future cluster use mkdir -p /mydata/redis/conf mkdir -p /mydata/redis/data ## configuration directory mkdir -p /mydata/redis/conf/6370 mkdir -p /mydata/redis/conf/6371 mkdir -p /mydata/redis/conf/6372 ## data directory mkdir -p /mydata/redis/data/6370 mkdir -p /mydata/redis/data/6371 mkdir -p /mydata/redis/data/6372
2.5 Upload configuration file
from http://download.redis.io/releases Download the specified version of redis and upload redis.conf and sentinel.conf to the /mydata/redis/conf directory
Modify redis.conf
bind 0.0.0.0 protected-mode no #password requirepass 123456
Copy to specified directory
cp /mydata/redis/conf/redis.conf /mydata/redis/conf/6370/redis.conf cp /mydata/redis/conf/redis.conf /mydata/redis/conf/6371/redis.conf cp /mydata/redis/conf/redis.conf /mydata/redis/conf/6372/redis.conf
2.7 docker start redis
docker run -p 6370:6379 --name redis-6370 --restart=always -v /mydata/redis/conf/redis-6370.conf:/etc/redis.conf -v /mydata/redis/conf/sentinel-26370.conf:/etc/sentinel.conf -v /mydata/redis/data/6370:/data -d docker.io/redis:6.2.7 redis-server /etc/redis.conf --appendonly yes
Check the installation
docker ps
2.9 Access redis
## into the container docker exec -it redis-6370 /bin/bash ## enter redis redis-cli ### Authentication password auth 123456 ### query key keys *
2.10 stop redis
docker stop redis-6370
3 Download the redis graphical client
address: https://github.com/lework/RedisDesktopManager-Windows/releases
4 Redis commands
4.1 Introduction to Redis data structure
Redis is a key-value database. The key is generally of type String, but there are various types of value.
In order to facilitate our learning, Redis also groups commands that operate on different data types. On the official website ( https://redis.io/commands ) to see different commands
You can also view the commands through the help command
help keys
4.2 Hierarchical structure of redis
- Redis does not have a concept similar to Table in MySQL. How can we distinguish different types of key s?
We can distinguish by adding a prefix to the key, but this prefix is not random, there are certain specifications:
The key of Redis allows multiple words to form a hierarchical structure. Multiple words are separated by: in the following format:
If Value is a Java object, such as a User object, you can serialize the object to a JSON string and store it:
4.3 Redis general commands
General instructions are some data types that can be used. The common ones are:
- KEYS: View all key s that match the template
- DEL: delete a specified key
- EXISTS: determine whether the key exists 1: exists, 0: does not exist
- EXPIRE: Set a validity period for a key, the key will be automatically deleted when the validity period expires
- TTL: View the remaining validity period of a KEY
4.4 Redis command - String command
String type, also known as string type, is the simplest storage type in Redis.
Its value is a string, but according to the format of the string, it can be divided into three categories:
- string: normal string
- int: Integer type, can do self-increment. Self-decrement operation
- float: floating point type, which can be incremented and decremented
Common commands for String:
- SET: Add or modify an existing key-value pair of type String
- GET: Get the value of String type according to the key
- MSET: Add multiple key-value pairs of String type in batches
- MGET: Get multiple String value s based on multiple key s
- INCR: increment an integer key by 1
- INCRBY: increment an integer key and specify the step size, for example: incrby num 2 increment the num value by 2
- INCRBYFLOAT: Increment a floating-point number and specify the step size
- SETNX: Add a key-value pair of type String, provided that the key does not exist, otherwise it will not be executed
- SETEX: Add a key-value pair of type String and specify the validity period
Intimate tip: the above commands are common commands except INCRBYFLOAT
- SET and GET: if the key does not exist, it is added, if it exists, it is modified
127.0.0.1:6379> set name Rose //didn't exist OK 127.0.0.1:6379> get name "Rose" 127.0.0.1:6379> set name Jack //Originally exists, is to modify OK 127.0.0.1:6379> get name "Jack"
- MSET and MGET
127.0.0.1:6379> MSET k1 v1 k2 v2 k3 v3 OK 127.0.0.1:6379> MGET name age k1 k2 k3 1) "Jack" //pre-existing name 2) "10" //pre-existing age 3) "v1" 4) "v2" 5) "v3"
- INCR and INCRBY and DECY
127.0.0.1:6379> get age "10" 127.0.0.1:6379> incr age //increase by 1 (integer) 11 127.0.0.1:6379> get age //get age "11" 127.0.0.1:6379> incrby age 2 //increase by 2 at a time (integer) 13 //Returns the current value of age 127.0.0.1:6379> incrby age 2 (integer) 15 127.0.0.1:6379> incrby age -1 //Negative numbers can also be added, which is equivalent to subtracting (integer) 14 127.0.0.1:6379> incrby age -2 //Reduce 2 at a time (integer) 12 127.0.0.1:6379> DECR age //Equivalent to negative incr, reducing normal usage (integer) 11 127.0.0.1:6379> get age "11"
- SETNX
127.0.0.1:6379> help setnx SETNX key value summary: Set the value of a key, only if the key does not exist since: 1.0.0 group: string 127.0.0.1:6379> set name Jack //set name OK 127.0.0.1:6379> setnx name lisi //If the key does not exist, the addition is successful (integer) 0 127.0.0.1:6379> get name //The operation of lisi failed because name already exists "Jack" 127.0.0.1:6379> setnx name2 lisi //name2 does not exist, so the operation succeeds (integer) 1 127.0.0.1:6379> get name2 "lisi"
- SETEX
127.0.0.1:6379> setex name 10 jack OK 127.0.0.1:6379> ttl name (integer) 8 127.0.0.1:6379> ttl name (integer) 7 127.0.0.1:6379> ttl name (integer) 5
4.5 Redis command - Hash command
Hash type, also called hash, its value is an unordered dictionary, similar to the HashMap structure in Java.
The String structure is to serialize the object into a JSON string and store it, which is very inconvenient when you need to modify a field of the object:
The Hash structure can store each field in the object independently, and can do CRUD for a single field:
Common Commands of Hash Type
-
HSET key field value: add or modify the value of the field of the hash type key
-
HGET key field: Get the value of a hash type key field
-
HMSET: Batch add the values of field s of multiple hash type key s
-
HMGET: Get the values of field s of multiple hash type key s in batches
-
HGETALL: Get all field s and value s in a hash type key
-
HKEYS: Get all field s in a hash type key
-
HINCRBY: Let the field value of a hash type key auto-increment and specify the step size
-
HSETNX: Add the field value of a hash type key, provided that the field does not exist, otherwise it will not be executed
- HSET and HGET
127.0.0.1:6379> HSET heima:user:3 name Lucy//The big key is heima:user:3, the small key is name, and the small value is Lucy (integer) 1 127.0.0.1:6379> HSET heima:user:3 age 21// If the operation does not exist data, it is new (integer) 1 127.0.0.1:6379> HSET heima:user:3 age 17 //If the operation exists on the data, it is a modification (integer) 0 127.0.0.1:6379> HGET heima:user:3 name "Lucy" 127.0.0.1:6379> HGET heima:user:3 age "17"
- HMSET and HMGET
127.0.0.1:6379> HMSET heima:user:4 name HanMeiMei OK 127.0.0.1:6379> HMSET heima:user:4 name LiLei age 20 sex man OK 127.0.0.1:6379> HMGET heima:user:4 name age sex 1) "LiLei" 2) "20" 3) "man"
- HGETALL
127.0.0.1:6379> HGETALL heima:user:4 1) "name" 2) "LiLei" 3) "age" 4) "20" 5) "sex" 6) "man"
- HKEYS and HVALS
127.0.0.1:6379> HKEYS heima:user:4 1) "name" 2) "age" 3) "sex" 127.0.0.1:6379> HVALS heima:user:4 1) "LiLei" 2) "20" 3) "man"
- HINCRBY
127.0.0.1:6379> HINCRBY heima:user:4 age 2 (integer) 22 127.0.0.1:6379> HVALS heima:user:4 1) "LiLei" 2) "22" 3) "man" 127.0.0.1:6379> HINCRBY heima:user:4 age -2 (integer) 20
- HSETNX
127.0.0.1:6379> HSETNX heima:user4 sex woman (integer) 1 127.0.0.1:6379> HGETALL heima:user:3 1) "name" 2) "Lucy" 3) "age" 4) "17" 127.0.0.1:6379> HSETNX heima:user:3 sex woman (integer) 1 127.0.0.1:6379> HGETALL heima:user:3 1) "name" 2) "Lucy" 3) "age" 4) "17" 5) "sex" 6) "woman"
4.6 Redis command-List command
The List type in Redis is similar to the LinkedList in Java, which can be regarded as a doubly linked list structure. Both forward search and reverse search can be supported. It is often used to store an ordered data, such as a list of likes in a circle of friends, a list of comments, etc.
Features are also similar to LinkedList:
- orderly
- Elements can be repeated
- Insert and delete fast
- Average query speed
Common commands for List:
- LPUSH key element ... : Insert one or more elements to the left of the list
- LPOP key: remove and return the first element on the left of the list, or nil if not
- RPUSH key element ... : Insert one or more elements to the right of the list
- RPOP key: remove and return the first element on the right side of the list
- LRANGE key star end: Returns all elements within an index range
- BLPOP and BRPOP: Similar to LPOP and RPOP, but wait for a specified time when there are no elements, instead of returning null directly
- LPUSH and RPUSH
127.0.0.1:6379> LPUSH users 1 2 3 (integer) 3 127.0.0.1:6379> RPUSH users 4 5 6 (integer) 6
- LPOP and RPOP
127.0.0.1:6379> LPOP users "3" 127.0.0.1:6379> RPOP users "6"
- LRANGE
127.0.0.1:6379> LRANGE users 1 2 1) "1" 2) "4"
4.7 Redis command - Set command
The Set structure of Redis is similar to HashSet in Java, which can be regarded as a HashMap whose value is null.
Because it is also a hash table, it has similar characteristics to HashSet:
- disorder
- Elements cannot be repeated
- Find fast
- Support intersection, union, difference and other functions
Common commands of type Set
- SADD key member ... : add one or more elements to the set
- SREM key member ... : remove the specified element in the set
- SCARD key: Returns the number of elements in the set
- SISMEMBER key member: Determine whether an element exists in the set
- SMEMBERS: get all elements in set
- SINTER key1 key2 ... : Find the intersection of key1 and key2
- SDIFF key1 key2 ... : Find the difference between key1 and key2
- SUNION key1 key2 ..: find the union of key1 and key2
For example: two sets: s1 and s2:
Find the intersection: SINTER s1 s2
specific command
127.0.0.1:6379> sadd s1 a b c (integer) 3 127.0.0.1:6379> smembers s1 1) "c" 2) "b" 3) "a" 127.0.0.1:6379> srem s1 a (integer) 1 127.0.0.1:6379> SISMEMBER s1 a (integer) 0 127.0.0.1:6379> SISMEMBER s1 b (integer) 1 127.0.0.1:6379> SCARD s1 (integer) 2
case
- Store the following data with Redis's Set collection:
- Zhang San's friends are: Li Si. Wang Wu. Zhao Liu
- Li Si's friends are: Wang Wu. Mazi. Ergou
- Use the Set command to achieve the following functions:
- Calculate how many friends Zhang San has
- Calculate what common friends Zhang San and Li Si have
- Find out who is Zhang San's friend but not Li Si's friend
- Find out who are the friends of Zhang San and Li Si in total
- Determine whether Li Si is a friend of Zhang San
- Determine whether Zhang San is Li Si's friend
- Remove Li Si from Zhang San's friend list
127.0.0.1:6379> SADD zs lisi wangwu zhaoliu (integer) 3 127.0.0.1:6379> SADD ls wangwu mazi ergou (integer) 3 127.0.0.1:6379> SCARD zs (integer) 3 127.0.0.1:6379> SINTER zs ls 1) "wangwu" 127.0.0.1:6379> SDIFF zs ls 1) "zhaoliu" 2) "lisi" 127.0.0.1:6379> SUNION zs ls 1) "wangwu" 2) "zhaoliu" 3) "lisi" 4) "mazi" 5) "ergou" 127.0.0.1:6379> SISMEMBER zs lisi (integer) 1 127.0.0.1:6379> SISMEMBER ls zhangsan (integer) 0 127.0.0.1:6379> SREM zs lisi (integer) 1 127.0.0.1:6379> SMEMBERS zs 1) "zhaoliu" 2) "wangwu"
4.8 Redis Command - SortedSet Type
Redis's SortedSet is a sortable set collection, which is somewhat similar to TreeSet in Java, but the underlying data structure is very different. Each element in the SortedSet has a score attribute, which can sort the elements based on the score attribute. The underlying implementation is a skip list (SkipList) plus a hash table. Because of the sortable nature of SortedSet, it is often used to implement functions such as leaderboards.
SortedSet has the following properties:
- Sortable
- Elements are not repeated
- Fast query speed
Common commands for SortedSet are:
- ZADD key score member: add one or more elements to the sorted set, and update its score value if it already exists
- ZREM key member: delete a specified element in the sorted set
- ZSCORE key member : Get the score value of the specified element in the sorted set
- ZRANK key member: Get the rank of the specified element in the sorted set
- ZCARD key: Get the number of elements in the sorted set
- ZCOUNT key min max: count the number of all elements whose score value is within a given range
- ZINCRBY key increment member: Let the specified element in the sorted set increment automatically, and the step size is the specified increment value
- ZRANGE key min max: After sorting by score, get the elements within the specified ranking range
- ZRANGEBYSCORE key min max: After sorting by score, get the elements within the specified score range
- ZDIFF.ZINTER.ZUNION: Find the difference set. Intersection. Union
Note: All rankings are in ascending order by default. If you want to descend in descending order, you can add REV after the command Z, for example:
- Get the rank of the specified element in the sorted set in ascending order: ZRANK key member
- Get the rank of the specified element in the sorted set in descending order: ZREVRANK key memeber