Redis - common instructions
1. Common commands for key
- del key1 key2 delete one or more key values
- rename key newkey Give the original key a new name
- move key
- set key value Add a key and corresponding value value
- get key Get the corresponding value according to the key
- select index switches the warehouse number of the key storage. By default redis divides the memory for storing keys into 16 warehouses ranging from 0 to 15.
- keys s* (query all key values starting with s) Fuzzy query keys stored in redis
- randomkey returns a random key
- exists key Enter a key and return whether the key exists
- type key View the type of a key value
- expire key 6 Set the key value to be valid for 6 seconds
- ttl key View the validity period of the key
2. String manipulation
- mset key value key value.... Set multiple key values and corresponding value values
- mget key1 key2 Get the values of multiple keys at the same time
- setrange key offset value Change the value corresponding to the key. (setrange str 3 s means that the value corresponding to str is shifted back to the fourth position and changed to s)
- append key value Append the value to the original value of the value
- The key value specified by incr key is incremented by 1, and the value after incrementing by 1 is returned.
Three, list linked list structure
- lpush key value inserts the value into the head of the value corresponding to the key
- rpush key value inserts the value at the end of the value corresponding to the key
- lrange key start stop Returns a range of values in the linked list.
- lpop key pops up the leftmost value of the value value
- rpop key pops up the rightmost value of the value value
- LREM KEY_NAME COUNT VALUE delete n identical value values corresponding to the key (lrem mylist 2 hi)
- LREM KEY_NAME COUNT VALUE (lrem mylist -1 hi) means that the index deletes 1 hi string from the right
- lindex key index (lindex mylist 2) query the corresponding value value according to the index value
- llen key calculates the length of this list
Four, set collection
- A Redis Set is an unordered collection of type String. Collection members are unique, which means that duplicate data cannot appear in the collection.
- Collections in Redis are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).
- The maximum number of members in a collection is 232 - 1 (4294967295, each collection can store more than 4 billion members).
- sadd key member1 member2 add one or more members to the set
- smembers key returns all members in the collection
- srandmember key [count] Returns one or more random numbers in the collection
- scardkey gets the number of members of the set
- spop key removes a random element from the set
- sismemberkey member Determines whether an element is a member of the set key
- sinterkey1 [key2] returns the intersection of all sets given
- sunion key1 [key2] returns the union of all the given sets
- sdiff key1 [key2] returns the difference of all sets given
5. Ordered collection
- zadd key score member score member (zadd myset 1 hello 2 nihao) Add a member to the sorted set and set the member's serial number at the same time
- ZCARD KEY_NAME The Redis Zcard command is used to count the number of elements in a collection.
- ZCOUNT key min max The Redis Zcount command is used to count the number of members in the specified score interval in an ordered set.
- ZRANGE key start stop [ WITHSCORES ] Returns an ordered set with members in the specified range. ( The positions of the members are sorted by increasing fractional value (from small to large). )
- ZRANK key member If the member is a member of the sorted set key, return the rank of the member
- The ZREM key member is used to remove one or more members from the sorted set, non-existing members will be ignored.
- ZSCORE key member returns a sorted set, the member's score value.
- ZSCAN key cursor iterates over the elements in the sorted set (including element members and element scores)
Six, hash data structure
- HSET KEY_NAME FIELD VALUE (hset key name field field name value value name) hset book name The Legend of Condor Heroes hset book auth Jin Yong
- HGET KEY_NAME FIELD_NAME returns the value of the specified field in the hash table
- HGETALL KEY_NAME returns all fields and values in the hash table
- HEXISTS KEY_NAME FIELD_NAME Check whether the specified field of the hash table exists
- HKEYS KEY_NAME get all field names in the hash table
- HLEN KEY_NAME get the number of fields in the hash table
- HMGET KEY_NAME FIELD1 ... FIELDN returns the value of one or more of the given fields in a hash table.
- HMSET KEY_NAME FIELD1 VALUE1 ... FIELDN VALUEN simultaneously sets multiple field-value pairs into a hash table
- HVALS KEY_NAME returns the values of all fields in the hash table.
Seven, Redis global operation
- flushdb clears the contents of the current database, but does not clear the data of other databases.
- flushall flushes the contents of all databases.
- scan (Use the keys* command to list all keys. If there are many, it will generate a high value for memory IO, which will seriously lead to downtime. Therefore, the scan command is used to obtain the corresponding key in the form of a cursor instead of taking it out at one time.)
- scan 0 math k* Find the key starting with k, it will list the data that will not be, and give the position of the next offset.