First, key pattern query the corresponding key
(1) Redis allows the fuzzy query key to have 3 wildcard characters * 、?、 []
(2) Randomkey: Return random key
(3) Type key: Returns the type of the key store
(4) Exists key: To determine whether a key exists
(5) Del key: Delete key
(6) Rename key newkey: renamed
(7) Renamenx key Newkey: If Newkey does not exist, the modification succeeds
(8) Move key 1: Moves key to 1 database
(9) TTL key: Query key life cycle (seconds)
(a) Expire key integer value: Set the life cycle of a key in seconds
(one) pexpire key integer value: Set the life cycle of a key in milliseconds
(s) Pttl key: Query key life cycle (MS)
(perisist key: Sets the specified key to permanent validity
Two, string type of operation
(1) Set key value [ex seconds] [px number of milliseconds] [nx/xx]
If both ex and PX are written at the same time, the subsequent validity period shall prevail
NX: If the key does not exist, establish
XX: Modify the value if key exists
(2) Get key: Take value
(3) Mset key1 value1 Key2 value2 set multiple values at once
(4) Mget Key1 Key2: Get more than one value at a time
(5) SetRange key offset value: Change the offset offset byte of a string to value
If offset > string length, the character is automatically 0x00
(6) Append key value: Appends value to the original value of the key
(7) GetRange key start stop: Gets the value of the [Start, stop] range in the string
For the subscript of a string, the left number starts at 0 and the right number starts at-1
Note: When Start>length, an empty string is returned
When Stop>=length, it is intercepted to the end of the string
Returns an empty string if start is located at the right of stop
(8) Getset key nrevalue: Gets and returns the old value, sets the new value
(9) incr key: Since the increment, returns the new value, if INCR is not an int value then returns an error, incr a nonexistent key, then sets the key to 1
(a) Incrby key 2: Jump 2 self-increase
(one) Incrbyfloat by 0.7: Self-increasing floating-point number
(setbit key Offset value: Sets the value on the offset binary to return the old value on that bit
Note: If the offset is too large, it will be populated in the middle 0
What is the maximum offset?
2^32-1, you can launch the largest string of 512M
(Bitop operation Destkey Key1 [Key2..] Do opecation on Key1 Key2 and save the results on Destkey
Opecation can be and OR not XOR
(strlen key: The length of the value value of the specified key
(Setex Key Time value: Sets the value of the key, and sets the validity period to time seconds
Third, linked list operation
The list type of Redis is actually a two-way list of string types for each child element, and the maximum length of the list is 2^32. The list can be used either as a stack or as a queue.
The pop operation of List also has a blocked version, mainly to avoid polling
(1) Lpush key value: Inserts a value into the list header
(2) Rpush key value: Inserts a value into the tail of the list
(3) Lpop key: Returns and deletes the header element of the list
(4) Rpop key: Returns and Deletes the tail element of the list
(5) Lrange key start stop: Returns elements in [Start, stop] in a list
(6) Lrem key count value: Deletes the value value from the list and ends after deleting the absolute value of Count
Count > 0 Remove Count < 0 from the header remove count=0 Delete all
(7) LTrim key start stop: Cut key corresponding link, cut [Start, stop] a section and reset the restructuring to key
(8) Lindex key index: Returns the value on index indexes
(9) Llen key: Calculating the number of elements in a linked list
(a) Linsert key After|before Search value: Search for search in key list and insert value before | Search Value |
(one) Rpoplpush source dest: Take the end of source out, put it on the Dest head, and return the cell value
Scenario: Task + bak double linked list complete secure queue
Business logic: Rpoplpush Task bak
Receive return value and do business processing
If successful, Rpop bak cleanup task, if not successful, next fetch from Bak table task
(Brpop,blpop key timeout: the tail/head element that waits for the key to pop up
Timeout is waiting for timeout, and if timeout is 0, wait for it.
Application scenario: Long polling ajax, available when chatting online
Iv. types and operation of hashes
The Redis hash is a string-type field and value mapping table, and its addition and deletion operations are O (1) (average). Hashing is especially useful for storing objects, storing an object in a hash type consumes less memory and makes it easy to access the entire object.
Configuration: Hash_max_zipmap_entries #配置字段最多64个
Hash_max_zipmap_value #配置value最大为512字节
(1) Hset myhash field value: Set Myhash field to value
(2) Hsetnx myhash field value: Setting Myhash field to value if no exists
(3) Hmset myhash field1 value1 field2 value2: Set multiple field at the same time
(4) Hget myhash field: Get the specified hash field
(5) Hmget myhash field1 field2: Get more than one field 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 field number of the hash
(9) Hdel myhash field: Deleting the specified field
(a) Hkeys Myhash: Back to hash all field
(one) Hvals Myhash: return hash All value
(Hgetall Myhash: Gets all field and value in a hash
V. Collection structure operation
Characteristics: disorder, certainty, uniqueness
(1) Sadd key value1 value2: Add elements to the collection
(2) Smembers key: Gets all elements of the collection
(3) Srem key value: Deleting a collection element
(4) Spop key: Returns and Deletes 1 random elements in the collection (you can take the lottery and not repeat it to someone)
(5) Srandmember key: Randomly take an element
(6) Sismember key value: Determines whether a collection has a value
(7) SCard key: Returns the number of elements in the collection
(8) Smove source Dest Value: Moves the value of source to the Dest collection
(9) Sinter Key1 Key2 Key3: Finding the intersection of Key1 Key2 Key3
(a) Sunion Key1 Key2: Seeking the collection of Key1 Key2
(one) Sdiff key1 Key2: The difference set of Key1 Key2
(a) Sinterstore res key1 Key2: Find the intersection of Key1 Key2 and exist in res
Six, ordered set
Concept: It adds a sequential attribute on the basis of set, which can be specified when modifying elements are added, and Zset automatically adjusts the order of the new values each time it is specified. Can be understood as a two-column MySQL table, a column of storage value, a column of storage order, in operation Key is understood as Zset name.
Like set Sorted,sets is also a collection of string-type elements, and each element is associated with a double score. The implementation of the sorted set is a mixture of skip list and hash table.
When an element is added to the collection, the mapping of an element to score is added to the hash table, so the cost of getting score for a given element is O (1). Another score to the element's map is added to the skip list and sorted by score so that the elements in the collection can be obtained in an orderly manner. Add, delete operation costs are both O (logn) and skip list the same cost, Redis's skip list implementation is a two-way list, so you can reverse from the tail element. Sorted set is used most often as an index, we can store the field to be sorted as score, and the ID of the object is stored as an element.
(1) Zadd key score1 value1: adding elements
(2) Zrange key start Stop [Withscore]: After sorting the collection, the element that returns the rank [Start,stop] is the default is the ascending arrangement withscores is to print the score Also
(3) Zrank key: Query member's rank (ascending 0 starts)
(4) Zrangebyscore key min Max [withscores] limit offset N: Set (Ascending) sort, then take score element in [Min, Max] and skip the offset and remove n
(5) Zrevrank Key Member: Query member rank (descending 0 start)
(6) Zremrangebyscore key min Max: Delete elements according to score, delete score between [Min, Max]
(7) Zrem key value1 value2: Deleting elements from a collection
(8) Zremrangebyrank key start end: Deletes the element by rank, deletes the rank between [start, end]
(9) Zcard key: Returns the number of elements in the collection
(Ten) Zcount key min Max: Returns the number of elements in the [min, Max] Interval
(one) Zinterstore dest Numkeys Key1[key2 ...] [WEIGHTS weight1 [weight2 ...]] [AGGREGATE sum| min| MAX]
To find the intersection of Key1,key2, the weights of Key1,key2 are weight1,weight2
Polymerization method with Sum|min|max
Aggregation result Save Child dest Collection
Note: Weights,aggregate How to understand?
Answer: If there is intersection, the intersection element has score,score how to deal with? Aggregate num->score Add, Min min Score,max maximum score, in addition, you can set different key weights by weights, when the intersection score*weight
Vii. Server-related commands
(1) Ping: Determine whether the connection is alive
(2) Echo: Print some content at the command line
(3) Select: Choose a Database
(4) Quit: Exit connection
(5) Dbsize: Returns the number of key keys in the current database
(6) Info: Get server information and statistics
(7) Monitor: Real-time dump of requests received
(8) Config get configuration entry: Getting Server configuration information
Config set configuration item value: Setting Configuration item Information
(9) Flushdb: Delete all key in the current selection database
(a) Flushall: Delete all keys in all databases
Time: Display server time, timestamp (seconds), microsecond number
(in) bgrewriteaof: background save RDB snapshots
(bgsave): Background save RDB snapshots
() Save: Save Rdb Snapshots
(Lastsave): Last Save time
(shutdown) [Save/nosave]
Note: If you accidentally run the Flushall, immediately shutdown Nosave, shut down the server, then manually edit AoF file, remove the Flushall related lines in the file, and then open the server, you can rewind the original data. If the system happens to be bgwriteaof after the Flushall, the aof is emptied and the data is lost.
(Showlog): Show slow Query
Q: How slow does it call slow?
A: By Slowlog-log-slower-than 10000, to specify (in microseconds)
Q: How many slow query records does the server store?
Answer: By Slowlog-max-len 128, to limit