Redis data type and basic operation

catalogue

string

basic operation

Add and modify data

get data

Delete data

Add and delete the value of key

Set the life time of the key

hash

basic operation

get data

Delete data

Check for data

list 

basic operation

Add and modify data

Get data

set

basic operation

Add data

get data

Delete data

sorted_set

basic operation

Add data

Get data

Delete data

Sort data

string

Stored data: single data

Data format: one storage space holds one data

basic operation

Add and modify data

set key value    #Add single data
mset key1 value1 key2 value2 key3 value3...    #Add multiple data through key
append key value    #If there is data, append. If there is no data, create a new operation. After appending, return the length of the string

get data

Get value through key, return value if there is data, and nil if there is no data

get key    #Get value through key, return value if there is data, and nil if there is no data
mget key1 key2 key3    #Get multiple data
strlen key    #Get the length of string through key

Delete data

If (integer)1 is returned after deletion, the deletion is successful; If (integer)0 is returned, the deletion fails

del key

Add and delete the value of key

incr key  #Add 1

incrby key increment  #Increase the specified value of the data
#Examples
incrby id 10 #Add 10

incrbyfloat key increment  #Append decimals to data
#Examples
incrbyfloat id 0.1  #Add 0.1

decr key  #Minus 1

Set the life time of the key

When the set time is up, go to get and a nil will be returned

setex key seconds value  #Seconds write time is in seconds
psetex key milliseconds value  #Millicons write time in milliseconds

hash

A key corresponds to many data, including field and value. A field corresponds to a value, that is, a key corresponds to the data of multiple key value pairs

The main purpose is to store data

basic operation

Add and modify data

hset key field value  #Compared with string, you need to write more field s and insert a single data
hmset key field value field1 value1 field2 value2  #Insert multiple data

get data

hget key field  #Get single data through key and field
hmget key field1 field2 field3..  #Get multiple data through key
hgetall key field  #Get all data through key and field

hkeys key    #Query all key s
hvals key    #Query all values

Delete data

hdel key field field1 field2...    #Return (integer)1 means success and 0 means failure

Check for data

hlen key    #Get length
hexists key field    #Check whether it is empty. If 0 is returned, it means there is no data in 1

list 

Multiple data can be saved. The bottom layer adopts the structure of two-way linked list to distinguish the order of data entry, which can reflect the order of entry

basic operation

Add and modify data

lpush key value value1 value2...    #Add from the left
rpush key value value1 value2...    #Add from the right

Get data

#Get data through the index range. If you add data output through lpush, it is in reverse order. If you add data output through rpush, it is in order
lrange key start stop    
#Example 1: lrange student 0 10 means to obtain data from index 0 to index 10 
#Example 2: lrange student 0 -1, where - 1 represents the last one, you can view all

#Get data by index
lindex key start stop 
#Example: lindex student 10 obtains the data of index 10

#View length
len key

#Get and remove the data, take out the data, print it, and remove it from the list
lpop key  #Get and remove from left
rpop key  #Get and remove from the right

#Obtain and remove the data within the specified time
blpop key key1 timeout
brpop key key1 timeout
#Example: blpop student 15 adds data within 15 seconds, and the operation instruction acquires and removes the data. After 15 seconds, the data disappears

set

Compared with hash, if the key is cancelled, a field corresponds to a value, which provides a higher internal storage mechanism, makes it easier to query efficiently, and the value cannot be repeated

basic operation

Add data

sadd key member member1...    #Return 1 for success and 0 for failure

get data

smembers key  #Get all data through key
scard key  #Total amount of data obtained
sismember key member  #Judge whether there is a specified member, 1 means include, 0 means not include

Delete data

srem key member member1...    #Delete data through key

sorted_set

The storage structure based on set provides a sort method, which is more conducive to the display of data

basic operation

Add data

zadd key score member score1  member1...
#Example: zadd student 8 kobe

Get data

#Total data obtained
zcard key

#Get the total amount of data by conditions
zcount key min max

#The score is output in ascending order. If whitscore is added, member and score will be displayed. If it is not added, only member will be displayed
zrange key start stop whitscores
#Example: zrange student 0 -1, where 0 means to start from 0 and - 1 means to print all from the last one


#The score is output in descending order. If whitscore is added, member and score will be displayed. If it is not added, only member will be displayed
zrevrange key start stop whitscores
#Example: zrevrange student 0 -1, where 0 means to start from 0 and - 1 means to print all from the last

#Data acquisition according to the required conditions (ascending order)
zrangebyscore key min max withscores 
#Example: zrangebyscore student 40 60 whitscores indicates students who have obtained 40-60

#Obtain the data according to the required conditions (in descending order)
zrevrangebyscore key min max withscores 
#Example: zrevangebyscore student 40 60 whitscores indicates students who have obtained 40-60

#After the summation operation of the intersection of sets, the query will be displayed. Note: only the data of all sets will be summated
zinterstore destination numkeys key key1
#Example: zinterstore destination (name of newly saved set) numkeys (number of sets to operate) key (key name of operation) key1 

Delete data

#Delete data through key
zrem key member member1...  

#Delete data by condition
zremrangbyrank key start stop
zremrangebyscore key min max

Sort data

#Sort in ascending order by index
zrank key member
#For example: zrank student kobe will output the ranking of kobe (the return form is index)

#Descending by index
zreverank key member

Keywords: Database Redis

Added by transformationstarts on Wed, 16 Feb 2022 18:55:36 +0200