[Redis] Redis basics of redis learning

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

protected-mode no
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
### 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> set name Rose  //didn't exist
OK> get name 
"Rose"> set name Jack //Originally exists, is to modify
OK> get name
  • MSET and MGET> MSET k1 v1 k2 v2 k3 v3
OK> 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> get age 
"10"> incr age //increase by 1
(integer) 11> get age //get age
"11"> incrby age 2 //increase by 2 at a time
(integer) 13 //Returns the current value of age> incrby age 2
(integer) 15> incrby age -1 //Negative numbers can also be added, which is equivalent to subtracting
(integer) 14> incrby age -2 //Reduce 2 at a time
(integer) 12> DECR age //Equivalent to negative incr, reducing normal usage
(integer) 11> get age 

  • SETNX> 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> set name Jack  //set name
OK> setnx name lisi //If the key does not exist, the addition is successful
(integer) 0> get name //The operation of lisi failed because name already exists
"Jack"> setnx name2 lisi //name2 does not exist, so the operation succeeds
(integer) 1> get name2 
  • SETEX> setex name 10 jack
OK> ttl name
(integer) 8> ttl name
(integer) 7> 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> 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> HSET heima:user:3 age 21// If the operation does not exist data, it is new
(integer) 1> HSET heima:user:3 age 17 //If the operation exists on the data, it is a modification
(integer) 0> HGET heima:user:3 name 
"Lucy"> HGET heima:user:3 age
  • HMSET and HMGET> HMSET heima:user:4 name HanMeiMei
OK> HMSET heima:user:4 name LiLei age 20 sex man
OK> HMGET heima:user:4 name age sex
1) "LiLei"
2) "20"
3) "man"
  • HGETALL> HGETALL heima:user:4
1) "name"
2) "LiLei"
3) "age"
4) "20"
5) "sex"
6) "man"
  • HKEYS and HVALS> HKEYS heima:user:4
1) "name"
2) "age"
3) "sex"> HVALS heima:user:4
1) "LiLei"
2) "20"
3) "man"
  • HINCRBY> HINCRBY  heima:user:4 age 2
(integer) 22> HVALS heima:user:4
1) "LiLei"
2) "22"
3) "man"> HINCRBY  heima:user:4 age -2
(integer) 20
  • HSETNX> HSETNX heima:user4 sex woman
(integer) 1> HGETALL heima:user:3
1) "name"
2) "Lucy"
3) "age"
4) "17"> HSETNX heima:user:3 sex woman
(integer) 1> 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> LPUSH users 1 2 3
(integer) 3> RPUSH users 4 5 6
(integer) 6
  • LPOP and RPOP> LPOP users
"3"> RPOP users
  • LRANGE> 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> sadd s1 a b c
(integer) 3> smembers s1
1) "c"
2) "b"
3) "a"> srem s1 a
(integer) 1> SISMEMBER s1 a
(integer) 0> SISMEMBER s1 b
(integer) 1> SCARD s1
(integer) 2


  • 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> SADD zs lisi wangwu zhaoliu
(integer) 3> SADD ls wangwu mazi ergou
(integer) 3> SCARD zs
(integer) 3> SINTER zs ls
1) "wangwu"> SDIFF zs ls
1) "zhaoliu"
2) "lisi"> SUNION zs ls
1) "wangwu"
2) "zhaoliu"
3) "lisi"
4) "mazi"
5) "ergou"> SISMEMBER zs lisi
(integer) 1> SISMEMBER ls zhangsan
(integer) 0> SREM zs lisi
(integer) 1> 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

Tags: Database Redis

Posted by Bulbe on Wed, 09 Nov 2022 20:22:25 +0300