1, Redis global command operation
1. View all keys
keys * -- Fuzzy search queries can also be used, but generally they are not pushed keys s*
2. View the total number of keys
dbsize
3. Check if the key exists
exists key
4. Delete key to return the number of deleted entries
del key
5. Key expiration
Return the remaining expiration time: - 1 means no expiration is set, - 2 means expired, and a positive number means the remaining expiration time (seconds)
expire key seconds -- Expire after setting timestamp expireat key time stamp -- See how long it expires ttl key
6. View the data structure type of the key
type key
7. Key rename
If the newkey exists, it will be forcibly overwritten
rename oldkey newkey
2, Five common data structure operations of Redis
String, Hash, List, Set, ZSET
String (String)
1. Set value
set key value -- key It must not exist before it can be set successfully for adding setnx key value -- key Must exist for setup to succeed for update set key value xx --Batch settings mset key1 value1 key2 value2 Example: set age 23 ex 10 setnx name test set age 25 xx
The set command has several options:
-
ex seconds: sets the second expiration time for the key.
-
px milliseconds: sets the millisecond expiration time for the key.
-
nx: the key must not exist before it can be successfully set for addition.
-
xx: Contrary to nx, the key must exist before it can be successfully set for update.
set key value ex seconds
2. Get value
get key --Batch acquisition mget key1 key2 Example: Batch setting: mset country china city beijing Batch acquisition: mget country city address
3. lncr digital operation
The lncr command is used to automatically increment the value. The returned results are divided into three cases:
- Value is not an integer, error returned
- The value is an integer and returns the result after self increment
- If the key does not exist, it will increase automatically according to the value of 0, and the return result is 1
-- Self increasing incr key -- Self subtraction decr key -- Self incrementing specified number incrby number key -- Self subtracting specified number decrby number key -- Self increasing floating point number incrbyfloat number key Example: incr age //Must be an integer self incrementing 1, non integer returns an error, and no age key returns 1 from 0 decr age //Integer age minus 1 incrby age 2 //Integer age+2 decrby age 2//Integer age -2 incrbyfloat score 1.1 //Floating point score+1.1
4. Append append instruction
append key Appended string Example: set name hello append name world //Add to helloworld
5. strlen string length, returns the string length
strlen key Example: set hello ""World" strlen hello
6. getset sets and returns the original value
getset sets the same value as set, but the difference is that it also returns the original value of the key
getset key value
7. setrange sets the character at the specified position, and the subscript is calculated from 0
setrange key Value of subscript setting
8. getrange intercept string
getrange intercepts a part of the string to form a substring. It needs to indicate the offset of the start and end. The intercepted range is a closed interval
getrange key Start subscript end subscript Example: set name helloworld ; getrange name 2 4//Return llo
Hash
id | name | age |
---|---|---|
1 | xiaoming | 18 |
-- use String Set value(Preventing key name conflicts in command space: business name:Object name:id:attribute) set user:1:name xiaoming set user:1:age 18 -- use Hash Set value hmset user:1 name xiaoming age 18
hash is a mapping table of field and value of string type. hash is especially suitable for storing objects
command hset key field value Set value: hset user:1 name xiaoming Value: hget user:1 name Delete value: hdel user:1 age Number of calculations: hset user:1 name xiaoming; hset user:1 age 23; hlen user:1 Batch setting: hmset user:2 name xiaoming age 23 sex boy Batch value: hmget user:2 name age sex judge field Is there: hexists user:2 name Get all field: hkeys user:2 obtain user:2 All value: hvals user:2 obtain user:2 All field And value: hgetall user:2 Add 1: hincrby user:2 age 1 hincrbyfloat user:2 age 2
List
The list type is used to store multiple ordered strings. The five elements a, b, c, d and e form an ordered list from left to right. Each string in the list is called element. A list can store up to 2-1 elements. In Redis, you can insert (push) and pop (pop) at both ends of the list, obtain the element list of the specified range, obtain the elements of the specified index subscript, etc.
1. lrange gets the list of elements within the specified range
Index subscript features: 0 to N-1 from left to right
lrange 0 -1 Command can get all the elements of the list from left to right rpush Insert from right to left lpush Insert from left to right linsert Insert a new element before or after an element lpop Pop up from the left side of the list rpop Pop up from the right side of the list
2. lrem deletes the specified element
The lrem command will find an element equal to value from the list and delete it. There are three cases according to the count:
- Count > 0, delete up to count elements from left to right.
- Count < 0, delete up to count absolute value elements from right to left.
- count=0, delete all.
lrem enjoylist 4 x
3. ltirm prunes the list by index range
For example, you want to keep the 0 to 1 elements in the list
ltrim enjoylist 0 1
4. lset modifies the element of the specified index subscript
lset enjoylist 1 web
5. lindex gets the element of the index subscript specified in the list
lindex enjoylist 1
6. llen get list length
llen enjoylist
Set
The set type is also used to store multiple string elements, but unlike the list type,
Duplicate elements are not allowed in the collection, and the elements in the collection are unordered. Elements cannot be obtained through index subscripts.
1. sadd add element
Multiple elements can be added, and the returned result is the number of elements added successfully
sadd key member
2. srem delete element
Multiple elements are allowed to be deleted, and the returned result is the number of successfully deleted elements
srem key member
3. scard calculates the number of elements
scard key
4. sismember determines whether an element is in the collection
If the given element element returns 1 within the collection, otherwise it returns 0
sismember key member
5. srandmember randomly returns the specified number of elements from the collection
If the specified number is not written, the default value is 1
srandmember key count
6. spop randomly ejects elements from the collection
You can specify the number. If you do not write, the default is 1. After the spop command is executed, the element will be deleted from the collection, while the srandmember command will not delete the element
spop key count
7. smembers get all elements
The returned results are unordered
smembers key
8. Finding the intersection of multiple sets by sinter
sinter key1 key2
9. sunion finding the union of multiple sets
sunion key1 key2
10. sdiff finding the difference set of multiple sets
sdiff key1 key2
11. Save the results of intersection, union and difference sets
Operations between sets are time-consuming when there are many elements, so redis provides the following three commands (original command + store) to save the results of intersection, union and difference between sets in the destination key
sinterstore destination key [key ...] suionstore destination key [key ...] sdiffstore destination key [key ...]
Ordered set (ZSET)
Ordered set retains the feature that the set cannot have duplicate members, but the difference is that the elements in the ordered set can be sorted. It uses index subscripts as the sorting basis compared with the list. The difference is that the ordered set sets a score for each element as the sorting basis.
The elements of an ordered set cannot be repeated, but the score can be repeated, just as the student number of a classmate cannot be repeated, but the score can be repeated.
The ordered set provides functions such as sub obtaining specified scores and element range query, calculating member ranking, etc.
1. zadd adds members, and the returned result represents the number of members successfully added
Add user test01 and his score 98 to the ordered set u:kaoshi:
zadd u:kaoshi 98 test01
The zadd command also has four options nx, xx, ch, incr
- nx: member must not exist before it can be set successfully for adding
- xx: member must exist before it can be successfully set for update
- ch: returns the number of ordered set elements and scores changed after this operation
- incr: increase the score, which is equivalent to zincrby
2. zcard calculates the number of members
zcard key
3. zscore calculates the score of a member
nil if the member does not exist
zscore key member
4. zrank calculates the ranking of members
zrank is the score from low to high, and zrevrank is the score from high to low
zrank key member zrevrank key member
5. zrem delete member
Multiple members can be deleted at one time, and the returned result is the number of successfully deleted members
zrem key member
6. zincrby increases members' scores
zincrby key score member
7. zrange and zrevrange return members of the specified ranking range
Ordered sets are ranked according to scores. zrange returns from low to high. On the contrary, if the WithCores option is added, the scores of members will be returned at the same time
zrange key min max zrange key min max withscores zrevrange key min max zrevrange key min max withscores
8. zrangebyscore returns the members of the specified score range
zrangebyscore returns from low to high according to the score, and zrevangebyscore returns vice versa.
zrangebyscore key min max [withscores] [limit offset count] zrevrangebyscore key max min [withscores][limit offset count]
9. zcount returns the number of members in the specified score range
zcount key min max
10. zremrangebyrank deletes the elements in the specified ranking in ascending order
zremrangebyrank key start end
11. zremrangebyscore deletes members of the specified score range
zremrangebyscore key min max
12. Zintermore intersection
zinterstore destination numkeys key [key ...] [weights weight [weight ...]] [aggregate sum / min / max]
destination: save the intersection calculation results to this key.
numkeys: the number of keys for intersection calculation.
key [key...]: the key for intersection calculation.
weights weight [weight...]: the weight of each key, and the weight of each key in the intersection calculation
member will multiply its score by this weight, and the weight of each key is 1 by default.
aggregate sum/ min |max: after calculating the intersection of members, the score can be calculated according to sum (and) and min (maximum)
Small value) and max (maximum value) are summarized. The default value is sum
13. zunionstore Union
zunionstore destination numkeys key [key ...] [weights weight [weight ...]] [ aggregate sunmI minl max]
intersection
zinterstore destination numkeys key [key ...] [weights weight [weight ...]] [aggregate sum / min / max]
destination: save the intersection calculation results to this key.
numkeys: the number of keys for intersection calculation.
key [key...]: the key for intersection calculation.
weights weight [weight...]: the weight of each key, and the weight of each key in the intersection calculation
member will multiply its score by this weight, and the weight of each key is 1 by default.
aggregate sum/ min |max: after calculating the intersection of members, the score can be calculated according to sum (and) and min (maximum)
Small value) and max (maximum value) are summarized. The default value is sum.