Redis basics 7 - data types and practice types

Keywords: Redis search engine nosql

Business scenario 1

Automatic dialogue of semantic recognition language in the field of artificial intelligence. The trial behavior of trial users is limited to 10 calls per minute


1. Design a counter to record the call times, which is used to control the service execution times. Take the user id as the key and the usage times as the value

2. Obtain the number of times before calling to judge whether it exceeds the limit

If the number of times is not exceeded, the count of each call is + 1

Business call is white, technology - 1

3. Set the life cycle of the counter as the specified cycle, such as 1 second / minute, and automatically clear the usage times in the cycle

Method 1> get 415
(nil)> setex 415 60 1
OK> get 415
"1"> incr 415
(integer) 2> get 415
"2"> incr 415
(integer) 3> incrby 415 7
(integer) 10

Solution improvement

Cancel the determination of the maximum value, and use the incr operation to throw an exception when it exceeds the maximum value instead of determining whether it is greater than the maximum value each time

Judge whether it is nul. If yes, set it to Max times

                        If not, count + 1

                        Business call failed, count - 1> get 415
(nil)> setex 415 60 9223372036854775797
OK> get 415
"9223372036854775797"> incr 415
(integer) 9223372036854775798> incr 415
(integer) 9223372036854775799> incr 415
(integer) 9223372036854775800> incr 415
(integer) 9223372036854775801> incr 415
(integer) 9223372036854775802> incr 415
(integer) 9223372036854775803> incr 415
(integer) 9223372036854775804> incr 415
(integer) 9223372036854775805> incr 415
(integer) 9223372036854775806> incr 415
(integer) 9223372036854775807> incr 415
(error) ERR increment or decrement would overflow> incr 415       

Business scenario 2

Display sequence of wechat messages. In the process of using wechat, when wechat accepts messages, it will set the most recently accepted messages to the top by default. When multiple friends and concerned subscription numbers send messages at the same time, the sorting will continue to alternate. You can also set important sessions to top. Once the user is offline and opens wechat again, what order should the messages be displayed?

set top users are 400 and 500

  The user 300 sends a message to determine whether to set the top. If not, add the list

400 sends a message, and in the set top, add the list top

200 sends a message, repeats the process, enters right, and the list usually has 300 and 200

300 sends a message, the list is normally 300 left, and the list is usually 200 300


1. The data dependent on the list has the characteristics of order, manages the message, and uses the list structure as a stack

2. Create an independent list for top and ordinary sessions and manage them separately

3. When a user message is received in a list, add the id of the message sender from one side of the list to the list (set here as the left side)

4. There will be problems when multiple messages with the same id are repeatedly pushed into the stack. Before pushing into the stack, whether there is a message corresponding to the current id or not, delete the corresponding id first

5. When pushing a message, push the top session list first. When pushing the ordinary session list, the pushed list clears all data

6. The number of messages, that is, the number of wechat user conversations, is recorded separately with the idea of counter and updated synchronously with the list operation> lrem 100 1 300
(integer) 0> lpush 100 300
(integer) 1> lrem 100 1 400
(integer) 0> lpush 100 400
(integer) 2> lrem 100 1 200
(integer) 0> lpush 100 200
(integer) 3> lrem 100 1 300
(integer) 1> lpush 100 300
(integer) 3> lrange 100 0 -1
1) "300"
2) "200"
3) "400"


redis is applied to data operations based on chronological order without paying attention to specific time

Posted by mightymouse on Tue, 30 Nov 2021 17:52:40 -0800