Redis's five data types (String, List, Hash, Set, Sorted Set)

1, String data type

  • Overview: String is the most basic type of redis, with a maximum storage capacity of 512MB
  • Data, String type is binary safe, that is, it can store any data, such as numbers, pictures, serialized objects, etc


#The append command of Redis is to append value to a key. If there is no key, create one and set value
exists blue
append blue hello
append blue benet
get blue


SET  	#Get data. The command format is get key
set blue 'hellobenet!'
get blue


strlen  	#Gets the character length of the specified Key
strlen blue


incr 	#The value of this Key is incremented by 1
decr   	#The value of this Key decreases by 1
incrby 	#Increment the specified integer
decrby	#Decrements the specified integer
set lll 10
incr lll
decr lll
incrby lll 20
decrby lll -10
decrby lll 10
incrby lll -20


#While obtaining the original value of the counter and setting it to the new value, the two operations are completed atomically at the same time
getset blue 20
getset blue wwwww
get blue


setex   #Set the expiration time of the specified Key to xx seconds
ttl     #View the remaining lifetime of the key
setex blue 33 'world'
ttl blue
get blue
 Wait 33 s
get blue


setnx  #Create the specified key. If the key exists, it will not be executed. If it does not exist, it will be executed
setnx world hello
setnx blue hello
setnx blue hello


mset		#Value of batch setting key
mget		#Batch get key values
msttnx		#Set key values in batch. If there are existing keys, they will not be executed
keys *
mset w1 10 w2 20 w3 30
mset w1 10 w2 20 w3 30
msetnx q1 1 q2 2 q3 3

2, List data type

Overview: the element type of the list is string, which is sorted according to the insertion order. Add elements at the head or tail of the list


lpush  	#This command will create the key and its associated List, and then insert the values in the parameter into the header from left to right
lpushx	#This command inserts the value value into the header only when the key exists
lrange	#Returns the elements within the specified interval in the list. 0 represents the first element and 1 represents the second element
lpush fire a b c d e
keys *
lrange fire 0 3
lrange fire 0 -1
lpushx fire a
lpushx wwww q


lpop		#Remove and return the first element, starting from scratch
llen		#View the number of elements in the list
lpop fire
llen fire
lrange fire 0 -1


lrem		#From the left to the right variable linked list, delete two elements with a value equal to a, and the return value is the actual deleted quantity
lset        #Set the element value with index value xxx to the new value xxx
lindex   	#Gets the element value with index value xxx.
ltrim     	#Only elements with index values xxx to xxx are retained
lpush sy a a a b c d e
lrange sy 0 -1
lrem sy 2 a 
lrange sy 0 -1
lindex sy 2
ltrim sy 0 3
lrange sy 0 -1


linsert		#Insert a new element before and after the xxx element of the key 
lpush fish a b c d e
linsert fish before b  22
linsert fish after b 33
lrange fish 0 -1


rpush		#Insert values from left to right at the end of the table
rpushx		#Execute when the specified key exists, otherwise do not execute
rpush tree a b c d e f 
lrange tree 0 -1
rpushx tree g
rpushx wwww g


rpop			#Removes and returns the first element of the key, starting at the end
rpoplpush	    #Pop up the element xxx at the end of key 1 and insert it into the head of key 2 at the same time (atomically complete these two steps)
rpop fish
LPUSH wo 1
rpoplpush fish wo
lrange fish
lrange fish 0 -1
lrange wo 0 -1

3, Hash data type (hash type)

Overview: hash is used to store objects. This naming method can be adopted: the object category and ID constitute the key name, the field is used to represent the attribute of the object, and the field value stores the attribute value. E.g. storage ID
Car object for 2.
If the Hash contains few fields, this type of data will also take up very little disk space. Each Hash can store 4294967295 key value pairs.


hset			#Set the field to xxx and the value to xxx for the xxx key
hget			#Get the xxx key, and the field is the value of xxx
hdel			#Delete the xxx field of the xxx key, and 1 is returned successfully
hexists		    #Judge whether the xxx field in the xxx key exists, and return 1 if it exists
hlen			#Gets the number of fields for the xxx key
hsetnx		    #To add a new field to the xxx key, whether to execute is based on whether this field exists or not. No matter whether the key exists or not, a return of 1 indicates that the execution is successful


hset lfp q1 qqq w1 www e1 eee
hget lfp w1
hdel lfp q1
HEXISTS lfp q1
hlen lfp
hsetnx lfp r1 rrr


hincrby		#Add x to the xxx field value of the xxx key
 Figures plus impairment


hmset		#Create fields and assign values for xxx key in batch
hmget		#Gets or specifies multiple field values
hgetall		#Return all fields of xxx key and their values, which are listed pair by pair
hkeys		#Only get all field names in xxx key
hvals		#Get only the values of all fields in the xxx key


4, Set data type (unordered set)

Overview: unordered collection. The element type is String. The element is unique. Duplicate members are not allowed. Union, intersection and difference operations can be performed among multiple set types. Application scope:
1. Redis' Set data type can be used to track some unique data, such as the unique IP address information of a blog. For this scenario, we only need to store the visitor's IP in Redis every time we visit the blog, and the Set data type will automatically ensure the uniqueness of the IP address.
2. make full use of the convenient and efficient features of Set type server aggregation operation, which can be used to maintain the association relationship between data objects. For example, all customer IDs for purchasing an electronic device are stored in a specified Set, while the customer ID for purchasing another electronic product is stored in another Set. If we want to obtain which customers have purchased these two goods at the same time, the intersection command of Set can give full play to its convenience and efficiency.


sadd				#If one or more member elements are added to the collection, the member elements that already exist in the collection will be ignored. If the set key does not exist, create a set containing only the added elements as members
smembers			#adopt smembers Command to view the insertion results. The order of output is independent of the insertion order scard			#Gets the number of members in the collection
scard			    #Gets the number of members in the collection
sismember	     	#Judge whether the xxx member in the key exists. Return 0 to indicate that it does not exist and 1 to indicate that it exists


spop				#Randomly remove and return a member of the key
srem				#Remove xxx, xxx and xxx members from the key and return the number of removed members
srandmember		    #This command returns a member randomly
smove 			    #Move xxx member of key 1 to key 2, return 1 for success and 0 for failure

5, Sorted Set data type (zset, ordered set)

1. Ordered collection. The element type is String. The element is unique and cannot be repeated.
2. Each element will be associated with a score of double type (representing weight), which can be sorted by the size of weight, and the score of elements can be the same.

Application scope:
1) It can be used for the leaderboard of a large online game. Whenever the player's score changes, you can execute the ZADD command to update the player's score, and then obtain the user information of the top 10 through the ZRANGE command. Of course, we can also use the ZRANK command to obtain the ranking information of players through username. The last user of ZRANK will use a combination of ZRANK and other users to quickly obtain the information of points.
2) The sorted set type can also be used to build index data.


zadd			#Add one or more member elements and their fractional values to the ordered set
zcard		    #Gets the number of members in the key
zcount	    	#Number of members whose score satisfies the expression x < = score < = x
zrem		 	#Delete members xxx and xxx, and return the actual number of deleted members
zincrby	     	#If the member xxx does not exist, the zincrby command will add the member and assume its initial score is 0
zscore		    #Get the score of member xxx
zrank	    	#Get the location index value of member xxx


zrangebyscore		#Gets the member whose score satisfies the expression x < = score < = X
zremrangebyrank		#Delete members whose location index satisfies the expression x < = rank < = X.
zremrrangebyscore	#Delete members whose scores meet the expression x < = score < = x, and return the actual deleted quantity.


zrevrange			#Get and return the members in this interval from high to low by location index
zrevrangebyscore		#Get the members whose scores satisfy the expression x > = score > = x and output them in the order from high to low.
zrevrank	 			#Get member index


Commands starting with H are related to fields
Commands with M are mostly batch
Commands beginning with S and Z are mostly related to members
Most of the items that start with L are related to the element index

Tags: MySQL Database Redis

Posted by Logical1 on Fri, 15 Apr 2022 02:05:27 +0930