redis installation and five core data structures

Keywords: Database Redis data structure

 

catalogue

1. redis installation

2.Redis's single thread and high performance

3. Five core data structures are most commonly used in redis Internet

3.1 common string operations

3.2 string common scenarios

3.2.1 single value cache

3.2.2 object cache

3.2.3 distributed lock

3.2.4 counter

3.2.5 counter

3.2.6 Web Cluster session sharing

3.2.7 global serial number of distributed system

3.3 common hash operations

3.4 common hash scenarios

3.4.1   Object cache

3.4.2   E-commerce shopping cart

3.5 common operations

3.6 common application scenarios

3.6.1   Common data structures

3.6.2   Microblog messages and wechat public account messages

3.7 common operations

3.8 common scenarios:

3.8.1   Wechat lottery applet

3.8.2   Wechat microblog likes, collections, labels

3.8.3   Collection operation to realize microblog and wechat attention model

3.8.4   E-commerce commodity screening or condition screening

3.9 zset ordered set structure

3.10 zset common scenarios

3.10.1   Zset set operation implementation Leaderboard

1. redis installation

Download address: http://redis.io/download 
Installation steps: 
# Install gcc 
yum install gcc 

# Put the downloaded redis ‐ 5.0.3.tar.gz in the / usr/local folder and unzip it 
wget http://download.redis.io/releases/redis‐5.0.3.tar.gz 
tar xzf redis‐5.0.3.tar.gz 
cd redis‐5.0.3 

# Enter the decompressed redis ‐ 5.0.3 directory for compilation and installation 
make 

#  Modify redis.conf configuration
vi redis.conf perhaps vim redis.conf
daemonize yes #Background start 
protected‐mode no #If the protection mode is turned off and turned on, only the local machine can access redis 
# bind needs to be commented out 
#bind 127.0.0.1 (bind is bound to the ip of the network card of the machine. If multiple network cards can be configured with multiple ip, it represents which network card ip of the machine the client is allowed to access. Bind can not be configured in the intranet. Just comment it out.) 

# Start service 
 src/redis‐server redis.conf 

# Verify that the startup was successful 
ps ‐ef | grep redis 

# Enter redis client 
 src/redis‐cli 

# Exit client 
 quit 

# Exit redis service: 
 (1)pkill redis‐server 
 (2)kill Process number 
 (3)src/redis‐cli shutdown

2.Redis's single thread and high performance

Is Redis single threaded?
The single thread of Redis mainly refers to that the reading and writing of Redis network IO and key value pairs are completed by one thread, which is also Redis external
The main process of providing key value storage service. However, other Redis functions, such as persistence, asynchronous deletion and cluster data synchronization, are actually executed by additional threads.
Why can Redis single thread be so fast?
Because all its data is Memory In, all operations are memory level operations, and single thread avoids the switching of multiple threads
Energy loss problem. Because Redis is a single thread, you should use Redis instructions carefully for those time-consuming instructions (such as
keys) must be used with caution. If you are not careful, Redis may get stuck.
How does Redis single thread handle so many concurrent client connections?
Redis IO multiplexing : redis uses epoll to realize IO multiplexing, and puts the connection information and events in the queue, and then in turn
File event dispatcher, which distributes events to event handlers.
3.  Other advanced commands
keys: full traversal key , It is used to list all key s that meet specific regular string rules. When the amount of redis data is large,
Poor performance, avoid using
scan: progressive traversal key
SCAN cursor [MATCH pattern] [COUNT count]
The scan parameter provides three parameters, the first is the cursor integer value (the index value of the hash bucket), and the second is the regular pattern of the key,
The third is the number of key s traversed at one time (reference value, the number of bottom-level traversals is not necessarily), which is not the number of qualified results. The first
During one traversal, the cursor value is 0, and then the first integer value in the returned result is used as the cursor for the next traversal. Always traverse
Ends when the returned cursor value is 0.
be careful: However, scan is not perfect. If there are key changes (addition, deletion and modification) in the process of scan, then
The traversal effect may encounter the following problems: the new keys may not be traversed, and repeated keys may be traversed, that is to say
scan does not guarantee the complete traversal of all keys, which we need to consider when developing.
Info: View the redis service operation information. It is divided into nine blocks. Each block has many parameters. These nine blocks are:
Environment parameters for Server operation
Clients client related information
Memory server running memory statistics
Persistence persistent information
Stats general statistics
Replication master-slave replication related information
CPU usage
Cluster cluster information
KeySpace key value pair statistics quantity information

3. Five core data structures are most commonly used in redis Internet

Common data structures: string,hash,list,set,sorted set

Official website command portal!!!

3.1 common string operations

SET  key  value 			//Save string key value pair
MSET  key  value [key value ...] 	//Batch store string key value pairs
SETNX  key  value 		//Store a nonexistent string key value pair
GET  key 			//Gets a string key value
MGET  key  [key ...]	 	//Batch get string key values
DEL  key  [key ...] 		//Delete a key
EXPIRE  key  seconds 		//Set the expiration time of a key (seconds)


INCR  key 			//Add 1 to the numeric value stored in the key
DECR  key 			//Subtract 1 from the numeric value stored in the key
INCRBY  key  increment 		//Add increment to the value stored by key
DECRBY  key  decrement 	//Subtract increment from the value stored in the key

3.2 string common scenarios

3.2.1 single value cache

3.2.2 object cache

3.2.3 distributed lock

3.2.4 counter

3.2.5 counter

3.2.6 Web Cluster session sharing

3.2.7 global serial number of distributed system

Single value cache
SET  key  value 	
GET  key 

Object cache
1) SET  user:1  value(json Format data)
2) MSET  user:1:name  zhuge   user:1:balance  1888
    MGET  user:1:name   user:1:balance 

Distributed lock
SETNX  product:10001  true 		//A return of 1 indicates that the lock was obtained successfully
SETNX  product:10001  true 		//A return of 0 indicates that the lock acquisition failed
. . . Perform business operations
DEL  product:10001			//Release the lock after executing the service

SET product:10001 true  ex  10  nx	//Prevent accidental program termination from causing deadlock

Counter
INCR article:readcount:{article id}  	
GET article:readcount:{article id} 


Web colony session share
spring session + redis realization session share


Global serial number of distributed system	
INCRBY  orderId  1000		//redis batch generates serial numbers to improve performance

3.3 common hash operations

Hash Common operation
HSET  key  field  value 			//Store the key value of a hash table key
HSETNX  key  field  value 		//Store the key value of a non-existent hash table key
HMSET  key  field  value [field value ...] 	//Store multiple key value pairs in a hash table key
HGET  key  field 				//Get the field key value corresponding to the hash table key
HMGET  key  field  [field ...] 		//Batch obtain multiple field key values in hash table key
HDEL  key  field  [field ...] 		//Delete the field key value in the hash table key
HLEN  key				//Returns the number of field s in the hash table key
HGETALL  key				//Returns all key values in the hash table key

HINCRBY  key  field  increment 		//Add increment to the value of the field key in the hash table key

3.4 common hash scenarios

3.4.1   Object cache

3.4.2   E-commerce shopping cart

Object cache
HMSET  user  {userId}:name  zhuge  {userId}:balance  1888
HMSET  user  1:name  zhuge  1:balance  1888
HMGET  user  1:name  1:balance 

E-commerce shopping cart
1)To user id by key
2)commodity id by field
3)The quantity of goods is value


Shopping cart operation
1)Add itemhset cart:1001 10088 1
2)Increase quantityhincrby cart:1001 10088 1
3)Total number of goodshlen cart:1001
4)Delete itemhdel cart:1001 10088
5)Get all items in shopping carthgetall cart:1001

3.5 common operations

LPUSH  key  value [value ...] 		//Insert one or more values value into the header (leftmost) of the key list
RPUSH  key  value [value ...]	 	//Insert one or more values value at the end of the key list (rightmost)
LPOP  key			//Removes and returns the header element of the key list
RPOP  key			//Removes and returns the last element of the key list
LRANGE  key  start  stop		//Returns the elements within the specified interval in the list key. The interval is specified by offset start and stop

BLPOP  key  [key ...]  timeout	//Pop up an element from the header of the key list. If there is no element in the list, block and wait 					 Timeout seconds. If timeout=0, it will block and wait all the time
BRPOP  key  [key ...]  timeout 	//Pop up an element from the end of the key list. If there is no element in the list, block and wait 					 Timeout seconds. If timeout=0, it will block and wait all the time

3.6 common application scenarios

3.6.1   Common data structures

3.6.2   Microblog messages and wechat public account messages

Common data structures
Stack(Stack) = LPUSH + LPOP
Queue(Queue)= LPUSH + RPOP
Blocking MQ(Blocking queue)= LPUSH + BRPOP

Microblog messages and wechat public account messages
LPUSH  msg:test1  10018
LPUSH  msg:test1 10086
3)View the latest microblog messages
LRANGE  msg:test1  0  4

3.7 common operations

The key value of sdiff operation can only be the big one in the front and the small one in the back;

Set Common operation
SADD  key  member  [member ...]			//Store elements into the set key, and ignore them if they exist,
							if key New if none exists
SREM  key  member  [member ...]			//Delete element from set key
SMEMBERS  key					//Get all elements in the set key
SCARD  key					//Gets the number of elements of the set key
SISMEMBER  key  member			//Judge whether the member element exists in the set key
SRANDMEMBER  key  [count]			//Select count elements from the set key, and the elements will not be deleted from the key
SPOP  key  [count]				//Select count elements from the set key, and delete the elements from the key

Set Arithmetic operation
SINTER  key  [key ...] 				//Intersection operation
SINTERSTORE  destination  key  [key ..]		//Store the intersection results in the new set destination
SUNION  key  [key ..] 				//Union operation
SUNIONSTORE  destination  key  [key ...]		//Store the union result in the new set destination
SDIFF  key  [key ...] 				//Difference set operation
SDIFFSTORE  destination  key  [key ...]		//Save the difference set result into the new set destination

3.8 common scenarios:

3.8.1   Wechat lottery applet

3.8.2   Wechat microblog likes, collections, labels

3.8.3   Collection operation to realize microblog and wechat attention model

3.8.4   E-commerce commodity screening or condition screening

Wechat lottery applet
1)Click to participate in the lottery to join the collection
SADD key {userlD}
2)View all users participating in the raffle
SMEMBERS key	  
3)extract count Winners
SRANDMEMBER key [count] / SPOP key [count]


Wechat microblog likes, collections, labels
1) give the thumbs-up
SADD  like:{news ID}  {user ID}
2) Cancel like
SREM like:{news ID}  {user ID}
3) Check whether the user likes it
SISMEMBER  like:{news ID}  {user ID}
4) Get the list of users who like
SMEMBERS like:{news ID}
5) Number of users getting likes 
SCARD like:{news ID}

Collection operation to realize microblog and wechat attention model
1)Common concern
sinter test test1
2) Do the people I follow follow follow others:
sismember test test2
3) People I may know:
sdiff test2 test1

E-commerce commodity screening:
SADD  brand:huawei  P40
SADD  brand:xiaomi  mi-10
SADD  brand:iPhone iphone12
SADD os:android  P40  mi-10
SADD cpu:brand:intel  P40  mi-10
SADD ram:8G  P40  mi-10  iphone12

SINTER  os:android  cpu:brand:intel  ram:8G


3.9 zset ordered set structure

The score after the zet command is a number. Generally, the member behind the zet command can't only be a number. It is officially used "";

 

ZSet Common operation
ZADD key score member [[score member]...]	//Add elements with scores to the ordered set key
ZREM key member [member ...]		//Delete element from ordered set key
ZSCORE key member 			//Returns the score of the element member in the ordered set key
ZINCRBY key increment member		//Add increment to the score of the element member in the ordered set key 
ZCARD key				//Returns the number of elements in the ordered set key
ZRANGE key start stop [WITHSCORES]	//Obtain the elements of the ordered set key from the start subscript to the stop subscript in positive order
ZREVRANGE key start stop [WITHSCORES]	//Get the elements of the ordered set key from the start subscript to the stop subscript in reverse order

Zset Collection operation
ZUNIONSTORE destkey numkeys key [key ...] 	//Union calculation
ZINTERSTORE destkey numkeys key [key ...]	//Intersection calculation

3.10 zset common scenarios

3.10.1   Zset set operation implementation Leaderboard

Zset Collection operation implementation Leaderboard
1)Click news
ZINCRBY  hotNews:20190819  1  Guard Hong Kong
2)Top ten on display day
ZREVRANGE  hotNews:20190819  0  9  WITHSCORES 
3)Seven day search list calculation
ZUNIONSTORE  hotNews:20190813-20190819  7 
hotNews:20190813  hotNews:20190814... hotNews:20190819
4)Show the top ten in seven days
ZREVRANGE hotNews:20190813-20190819  0  9  WITHSCORES

Posted by Sikk Industries on Thu, 28 Oct 2021 06:50:19 -0700