This article mainly introduces the summary of Redis operation commands. This article explains key pattern query corresponding key, string type operation, linked list operation, hashes type and operation, collection structure operation, ordered collection, server related commands, etc. Friends who need it You can refer to the following
1. Key pattern query corresponding key
(1) redis allows fuzzy query key with 3 wildcards *,?, []
(2) randomkey: returns a random key
(3) type key: return the type of key storage
(4) exists key: determine whether a key exists
(5) del key: delete key
(6) rename key newkey: Rename
(7) renamenx key newkey: If the newkey does not exist, the modification is successful
(8) move key 1: move key to 1 database
(9) ttl key: query key life cycle (seconds)
(10) expire key integer value: set the life cycle of the key in seconds
(11) pexpire key integer value: set the life cycle of the key in milliseconds
(12) pttl key: query the life cycle of the key (milliseconds)
(13) perisist key: set the specified key to be permanently valid
Second, the operation of the string type
(1) set key value [ex seconds] [px milliseconds] [nx/xx]
If ex and px are written at the same time, the later validity period shall prevail
nx: create if the key does not exist
xx: If the key exists, modify its value
(2) get key: value
(3) mset key1 value1 key2 value2 set multiple values at a time
(4) mget key1 key2: Get multiple values at a time
(5) setrange key offset value: Change the offset byte of the string to value
If the offset > string length, the character will be automatically filled with 0x00
(6) append key value: append the value to the original value of the key
(7) getrange key start stop: Get the value of the [start, stop] range in the string
For the subscript of the string, the left number starts from 0, and the right number starts from -1
Note: when start>length, an empty string is returned
When stop>=length, intercept to the end of the string
Returns an empty string if start is located to the right of stop
(8) getset key nrevalue: get and return the old value, and set the new value
(9) incr key: self-increment, return a new value, if incr is a value that is not an int, an error will be returned, if incr is a key that does not exist, set the key to 1
(10) incrby key 2: Jump 2 auto-increment
(11) incrbyfloat by 0.7: self-incrementing floating-point number
(12) setbit key offset value: set the offset corresponding to the value on the binary, and return the old value on this bit
Note: If the offset is too large, 0 will be filled in the middle
What is the maximum offset
2^32-1, you can launch the largest string is 512M
(13) bitop operation destkey key1 [key2..] Do opecation on key1 key2 and save the result on destkey
opecation can be AND OR NOT XOR
(14) strlen key: Take the length of the value of the specified key
(15) setex key time value: set the value corresponding to the key, and set the validity period to time seconds
3. Linked list operation
The list type of Redis is actually a doubly linked list in which each sub-element is a string type, and the maximum length of the linked list is 2^32. A list can be used as both a stack and a queue.
The pop operation of list also has a blocking version, mainly to avoid polling
(1) lpush key value: Insert the value into the head of the linked list
(2) rpush key value: insert the value at the end of the linked list
(3) lpop key: return and delete the head element of the linked list
(4) rpop key: return and delete the tail element of the linked list
(5) lrange key start stop: returns the elements in [start, stop] in the linked list
(6) lrem key count value: delete the value value from the linked list, delete the absolute value of count value and end
count > 0 delete from the head count < 0 delete from the end count=0 delete all
(7) ltrim key start stop: cut the link corresponding to the key, cut a section of [start, stop] and reassign the reformation to the key
(8) lindex key index: returns the value on the index index
(9) llen key: Calculate the number of elements in the linked list
(10) linsert key after|before search value: Search for search in the key linked list, and insert value before|after the search value
(11) rpoplpush source dest: Take out the end of source, put it in the head of dest, and return the unit value
Application scenario: task + bak double linked list completes the security queue
Business logic: rpoplpush task bak
Receive the return value and do business processing
If it succeeds, rpop bak clears the task, if not, take the task from the bak table next time
(12) brpop, blpop key timeout: wait for the tail/head element of the pop-up key
timeout is the waiting timeout time, if timeout is 0, it will wait forever
Application scenario: long polling ajax, which can be used in online chat
4. Hashes type and operation
Redis hash is a mapping table of string type fields and values, and its addition and deletion operations are O(1) (average). Hash is especially suitable for storing objects. Storing an object in the hash type will take up less memory, and the entire object can be accessed conveniently.
Configuration: hash_max_zipmap_entries 64 #Configuration fields up to 64
hash_max_zipmap_value 512 #Configure value up to 512 bytes
(1) hset myhash field value: set the field of myhash to value
(2) hsetnx myhash field value: If it does not exist, set the field of myhash to value
(3) hmset myhash field1 value1 field2 value2: set multiple fields at the same time
(4) hget myhash field: Get the specified hash field
(5) hmget myhash field1 field2: Get multiple fields at a time
(6) hincrby myhash field 5: the specified hash field plus the given value
(7) hexists myhash field: Test whether the specified field exists
(8) hlen myhash: returns the number of hash fields
(9) hdel myhash field: delete the specified field
(10) hkeys myhash: returns all hash fields
(11) hvals myhash: returns all values of hash
(12) hgetall myhash: Get all fields and values in a hash
Five, collection structure operation
Features: disorder, determinism, uniqueness
(1) sadd key value1 value2: add elements to the collection
(2) smembers key: Get all the elements of the collection
(3) srem key value: delete an element of the collection
(4) spop key: return and delete a random element in the set (you can sit in the lottery, and you will not draw someone repeatedly)
(5) srandmember key: Randomly select an element
(6) sismember key value: determine whether the set has a certain value
(7) scard key: returns the number of collection elements
(8) move source dest value: move the value of source to the dest collection
(9) sinter key1 key2 key3: find the intersection of key1 key2 key3
(10) sunion key1 key2: find the union of key1 key2
(11) sdiff key1 key2: find the difference of key1 key2
(12) sinterstore res key1 key2: Find the intersection of key1 key2 and store it in res
Six, ordered collection
Concept: It adds an order attribute on the basis of set. This attribute can be specified when adding and modifying elements. After each specification, zset will automatically adjust the order according to the new value. It can be understood as a mysql table with two columns, one column stores the value, and the other column stores the sequence. The key in the operation is understood as the name of the zset.
Like set, sorted, sets are also a collection of string type elements, the difference is that each element will be associated with a double type score. The implementation of sorted set is a hybrid of skip list and hash table.
When an element is added to the set, an element-to-score mapping is added to the hash table, so the cost of obtaining a score given an element is O(1). Another mapping from score to element is added to the skip list and sorted by score, so the elements in the collection can be obtained in an orderly manner. The overhead of adding and deleting operations is O(logN) and the overhead of the skip list is the same. The implementation of the skip list of redis is a doubly linked list, so that elements can be removed from the tail in reverse order. The most common way to use sorted set is to use it as an index. We can store the field to be sorted as a score and the ID of the object as an element.
(1) zadd key score1 value1: add element
(2) zrange key start stop [withscore]: After sorting the collection, return the elements with the rank [start, stop] The default is ascending sequence withscores is to print out the score
(3) zrank key member: Query the ranking of members (starting from 0 in ascending order)
(4) zrangebyscore key min max [withscores] limit offset N: After sorting the set (in ascending order), take the elements whose score is within [min, max], skip the offset ones, and take out N ones
(5) zrevrank key member: Query member ranking (starting from 0 in descending order)
(6) zremrangebyscore key min max: delete elements according to the score, and delete the score between [min, max]
(7) zrem key value1 value2: Delete elements in the collection
(8) zremrangebyrank key start end: delete elements by rank, delete the rank between [start, end]
(9) zcard key: returns the number of collection elements
(10) zcount key min max: returns the number of elements in the interval [min, max]
(11)zinterstore dest numkeys key1[key2..] [WEIGHTS weight1 [weight2...]] [AGGREGATE SUM|MIN|MAX]
Find the intersection of key1 and key2, the weights of key1 and key2 are weight1 and weight2 respectively
The aggregation method uses sum|min|max
The aggregation result is saved in the sub-dest collection
Note: how to understand weights and aggregate?
Answer: If there is an intersection, and the intersection elements have a score, how to deal with the score? Aggregate num->score addition, min minimum score, max maximum score, in addition, you can set the weight of different keys through weights, when the intersection is score*weight
7. Server related commands
(1) ping: Determine whether the connection is alive
(2) echo: print something on the command line
(3) select: select the database
(4) quit: exit the connection
(5) dbsize: returns the number of keys in the current database
(6) info: Get server information and statistics
(7) monitor: dump received requests in real time
(8) config get configuration item: Get server configuration information
config set configuration item value: set configuration item information
(9) flushdb: delete all keys in the currently selected database
(10) flushall: Delete all keys in all databases
(11) time: display server time, timestamp (seconds), microseconds
(12) bgrewriteaof: Save rdb snapshots in the background
(13) bgsave: Save rdb snapshots in the background
(14) save: save rdb snapshot
(15) lastsave: last save time
(16)shutdown [save/nosave]
Note: If you accidentally run flushall, shut down nosave immediately, shut down the server, then manually edit the aof file, delete the lines related to flushall in the file, and then turn on the server to rewind the original data. If after flushall, the system happens to bgwriteaof, then aof will be cleared and the data will be lost.
(17) showlog: display slow queries
Q: How slow is slow?
Answer: specified by slowlog-log-slower-than 10000 (in microseconds)
Q: How many slow query records does the server store
Answer: by slowlog-max-len 128, to do the limit
Reposted from: Micro reading https://www.weidianyuedu.com