Redis data type

Posted by SpecialFX on Thu, 20 Jan 2022 00:37:38 +0100

1, String data type

String is the most basic type of redis. It can store up to 512MB of data. String is binary safe, that is, it can store any data, such as numbers, pictures, serialized objects, etc

1,append

append   #The append command of Redis is to append value to a key. If there is no key, create one and set value
 Example:
redis-cli
exists bzx
append bzx   666
append bzx   555
get blue

2,set

SET  	#The command format is get key
 Example:
set  dnf  'haojiubujian'
get  dnf 

3,strlen

strlen  	#Gets the character length of the specified Key
 Example:
strlen bzx

4,incr,decr,incrby,decrby

incr 	#The value of the Key is incremented by 1
decr   	#The value of the Key is decremented by 1
incrby 	#Increments the specified integer
decrby	#Decrements the specified integer

5,getset

getset  #While getting the original value of the counter and setting it to the new value, the two operations are completed atomically at the same time
 Example:
incr gg
getset gg 0
get gg

6,setex

setex   #Set the expiration time of the specified Key to xx seconds
ttl key #View the remaining lifetime of the key

set gg 'haha'
ttl gg


7,setnx

setnx  #Create the specified key. If the key exists, it will not be executed. If it does not exist, it will be executed
 Example:
del gg
setnx gg haha
setnx gg sha
get gg

8,mset,mget,msetnx

mset		#Value of batch setting key
mget		#Batch get key values
msetnx		#Set key values in batch. If there are existing keys, they will not be executed

2, List data type

The element type of the list is string, sorted according to the insertion order, and elements are added at the head or tail of the list

1,lpush,lpushx,lrange

lpush  	#The 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 tt a b c d
lrange tt 0 -1
lpushx tt e
keys c*
lpushx tt e
lrange tt 0 -1

2,lpop,lle

lpop		#Remove and return the first element, starting from scratch
llen		#View the number of elements in the list

3,lrem,lset,lindex,ltrim

lrem		#Delete two elements with a value equal to a from the left variable list to the right variable list, 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

4,linsert

linsert		#Insert a new element before and after the xxx element of the key 
Example:
del tt
lpush  tt a b c d e 
lrange tt 0 -1
linsert tt before a 0
linsert tt after e 1

5,rpush,rpushx

rpush		#Insert values from left to right at the end of the table rpushx		#Execute when the specified key exists, otherwise do not execute

6,rpop,rpoplpush

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 (atomically complete these two steps)


3, Hash data type (hash type)

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. For example, store the car object with ID 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

1,hset,hget,hdel,hexists,hlen,hsetnx

hset			#Set the field to xxx and the value to xxx for the xxx key
hget			#obtain xxx Key, field is xxx Value of 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 or not is based on whether this field exists. No matter whether the key exists or not, a return of 1 indicates that the execution is successful



hset hash field1 a field2 b field3 c
hget hash field1
hdel hash field2

hexists hash field2
hlen hash

hsetnx hash1 field3 d
keys has*
hsetnx hash field3 d

2,hincrby

hincrby		#Add x to the xxx field value of the xxx key
 Example:
hincrby hash3 field1 5
hincrby hash3 field1 -10

3,hmset,hmget,hgetall,hkeys,hvals

hmset		#Create fields and assign values for xxx keys in batches
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's Set data type can be used to track some unique data, such as the unique IP address information of accessing 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. Making full use of the convenient and efficient characteristics of Set type server aggregation operation, it 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.

1,sadd,smembers,scard,sismember

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 collection key does not exist, create a collection containing only the added elements as members
smembers			#View the insertion results through the smembers command. The order of output is independent of the insertion order
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 0

2,spop,srem,srandmember,smove

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 			    #Moving the xxx member of key 1 to key 2 returns 1 for success and 0 for failure

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

summary:

  • Ordered collection. The element type is String. The element is unique and cannot be repeated.
  • Each element is associated with a score of double type (representing weight), which can be sorted by the size of weight, and the scores 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 score TOP10 through the ZRANGE command. Of course, we can also use the ZRANK command to obtain the ranking information of players through username. Finally, we will combine the ZRANGE and ZRANK commands to quickly obtain the information of other users with similar points to a player.
  2. The sorted set type can also be used to build index data.

1,zadd,zcard,zcount,zrem,zincrby,zscore,zrank

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		#Gets the location index value of member xxx


2,zrangebyscore,zremrangebyrank,zremrrangebyscore

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

3,zrevrange,zrevrangebyscore,zrevrank

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 bottom.
zrevrank	 			#Get member index

Topics: Redis