table of Contents
-
Value type-list in Redis
The main knowledge points of List: stack (same direction command),
Queue (reverse command),
Array,
Blocking queue
Lpush is added to the list queue from the left, then the order of insertion is abc, .. g, the order of arrangement is g...cba
The last inserted at index 0
Lrange k1 0 -1 Display all elements in the queue
In the same way, rpush adds elements from the right side of the list
Lpop k1 pops an element from the left, then g,
Explain that lpush and lpop implement the data structure of the stack: last in, first out. Similarly, Rpush and Rpop also implement last in, first out
Rpop pops up from the right, pops up a, indicating the first-in first-out data structure implemented by lpush and rpop
Summary: Stack (same direction command) realizes last in first out, queue (reverse command) realizes first in first out
List has indexes: forward index and reverse index
Assuming list: abcdef
The index rules:
0 1 2 3 4 5 6 Forward index
a b c d e f g list
-3 -2 -1 reverse index
Get the value by index:
lset k1 0 w Replace the number at index 0 of k1 with w
lrem k2 2 a Remove two as from the left
Among them, if the number of num is positive, remove num from left to right, if it is negative, remove num from right to left, if it is 0, don’t remove
After the above removal is completed, if it is returned, how can I add it to the original position? The red one below has been removed and needs to be re-added to the original position
c a d a a b a a a
Values need to add a after c and a after d
linsert k2 after c a
After also has before, which is the opposite of usage
Llen k2 counts how many elements are in k2
Blocking, unicast queue blpop
Blpop k1 0 Take out an element in k1, if there is no element in k1, wait forever and block
If multiple clients go to k1 in redis to fetch data, if there is a value in k1, whoever is the first to block will get it first, which conforms to the first-in first-out atom
As follows, I pushed two numbers in k1 one after another, and checked that the elements in it were empty, because the element was just thrown in, and it was taken out by other clients by executing blpop.
Client 01
Client 02
Client 03
lrange k1 1 -2 Delete the data at both ends of the index
Value type-hash in Redis
Key:value(key:value)
Hset and hget set the hash value
Set multiple values and get multiple values at the same time hmset hmget
Get all the keys and get all the values
hkeys k1
whale k1
hgetall k1 Get all keys and values
Calculate the value of HINCRBYFLOAT k1 age 0.5
Value type-set in Redis
set collection, de-duplication, disorder, random events
Add data: sadd
Query all data: smembers
Remove element: srem
Intersection, union, difference
SINTERSTORE dest k2 k3 Take the intersection of k2 and k3 and store it in dest
Sunion k2 k3 obtains the union of k2 and k3
Take the difference set (k2 and k3 are not the same order, the result is different)
Random events
SRANDMEMBER k2 2 Get 2 random members from k2, the set does not change
SPOP k2 randomly pops an element from k2, k2 has changed
Random event application:
1. Lottery, there are 3 first prizes, 2 2nd prizes, 1 1st prize, there are 12 users, and prizes are drawn randomly.
12 users are put into the set collection
Three randomly selected: No. 8. No. 14 and No. 5 won the prize! The winners can continue to draw
What if the person who has been drawn can no longer participate in the draw?
Those who have drawn will be removed from the lottery pool and will no longer participate in the subsequent lottery