Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
String
1,set / get / append / strlen
set key value #Store data get key value #get data append key value #The append command of Redis is to append value to a key. If there is no key, create one and set value strlen key #Gets the character length of the specified Key
Example:
exists test
append test "hello"
append test " world"
get test
set test 123456789
get test
strlen test
2,incr / decr / incrby / decrby
incr key #The value of the Key is incremented by 1 decr key #The value of the Key is decremented by 1 incrby key increment #Increments the specified integer decrby key increment #Decrements the specified integer
3,getset
getset key value # gets the original value of the counter and sets it to a new value. These two operations are atomic and complete at the same time
Example: set test2 10 getset test2 0 get test2
4,setex
setex key seconds value #Set the expiration time of the specified Key to xx seconds ttl key #View the remaining lifetime of the key
Example:
setex test2 10 'hello'
ttl test2
get test2
Wait for 10s
get test2
ttl test2
set test2 'hello'
ttl test2
5,setnx
setnx key value #Create the specified key. If the key exists, it will not be executed. If it does not exist, it will be executed
Example: exists test3 setnx test3 "zhangsan" setnx test3 "lisi" get test3
Example: pandas is a NumPy based tool created to solve data analysis tasks.
List data type
1,lpush / lpushx / lrange
lpush key value #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 key value #This command inserts the value value into the header only when the key exists lrange key start stop #Returns the elements within the specified interval in the list. 0 represents the first element and 1 represents the second element
2,lpop / llen
lpop key #Remove and return the first element, starting from scratch llen key #View the number of elements in the list
3,lrem / lset / lindex / ltrim
lrem key count value #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 key index value #Set the element value with index value xxx to the new value xxx lindex key index #Gets the element value with index value xxx. ltrim key start stop #Only elements with index values xxx to xxx are retained
4,linsert
linsert key BEFORE|AFTER pivot value #Insert a new element before and after the xxx element of the key
5,rpush / rpushx / rpop / rpoplpush
rpush key value #Insert values from left to right at the end of the table rpushx key value #Execute when the specified key exists, otherwise do not execute rpop key #Removes and returns the first element of the key, starting at the end rpoplpush source destination #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
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. 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 key field value #Set the field to xxx and the value to xxx for the xxx key hget key field #Get the xxx key, and the field is the value of xxx hdel key field #Delete the xxx field of the xxx key, and 1 is returned successfully hexists key field #Judge whether the xxx field in the xxx key exists, and return 1 if it exists hlen key #Gets the number of fields for the xxx key hsetnx key field value #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
2,hincrby
hincrby key field increment # adds x to the xxx field value of the xxx key
Example: HEXISTS myhash2 field1 hincrby myhash2 field1 5 hincrby myhash2 field1 -10 hget myhash2 field1
4, set data type
Application scope:
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.
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 products at the same time, the intersection command of Set can give full play to its advantages of convenience and efficiency.
/114103162