Redis database is often designed in our daily use. Whether it is used for caching or de-duplication, it will be designed to perform related operations of this database. Then let's take a look at the related operations of redis data. .
select (0-15): By default, it will contain 16 databases, and then you can use the select keyword to select any one of them to operate
command | effect |
keys * | View all keys |
type key | Type of key |
exists key | Whether the key exists |
random key | Randomly take out any key |
set key | Create a new key and assign a value to the key |
get key | View the value corresponding to the key |
of the key | Delete key |
rename key | Rename key |
renamenx key | Rename a key that will not be repeated |
move key 0 | Move the key to a corresponding library |
ttl key(Pttl) | Query life cycle (ms) |
expire key(Pexpire) | Set the life cycle of the key |
persist key | Permanent lifetime of the key |
keys pattern (regular) | Find the corresponding key according to the situation |
flushdb | Clear a database (current) |
The above are a few commands about the basic operation of the key, the following summarizes the various operations of the redis database on various data types
1. String
set key value ex | px nx xx: Assign a value to the key and set the life cycle. When nx does not exist, xx must exist only when the key exists
mset: set multiple at once
mget: one to get the value of multiple keys
setrange key offset value: replace the original value according to the offset
append key value: append the value to the original value
getrange key start stop: Get the value in the range of [start stop] in the string
start>=length: return an empty string
stop>=length: intercept to the end of the string
If the position of start is to the right of stop, an empty string is returned
getset key newvalue: Get and return the old value, and assign the new value
incrby key 5: increase by 5
decrby key 3: Decrease 3
incrbyfloat key 0.5: Increase by 0.5
setbit key offset 2: Set the binary value corresponding to the offset
2. Link linked list structure (list)
lpush key value: insert the value into the head of the linked list
rpop key: return and delete the last element of the linked list
lrange key start stop: Take out the value specified in the linked list
lpop key: return and delete the first element of the linked list
lrem key count value: delete from the left, count indicates how many to delete, and value indicates the value or element to be deleted
ltrim key start stop: Intercept from start to stop, and re-assign the intercepted result to the key, the original value changes
lindex key index: returns the value corresponding to the index
llen key: the length of the value corresponding to the key
linsert key after|before search value: Insert a value before or after the found value, and it will operate on the first value found
rpoplpush key1 key2: transfer the first from the right of key1 to the left of key2
brpop | blpop key timeout: Wait for the tail|head element of the key to be popped up. Timeout is the waiting timeout period. If timeout is 0, it is waiting forever.
3. Set collection data structure
The characteristics of the set: disorder, uniqueness, certainty
sadd key value1 value2: add elements to the collection
srem key value1 value2: delete element
spop key: return and delete a random element in the set key
srandmember key: returns a random element in the set key
sismember key value: Determine whether the value exists in the key, if yes, return 1, otherwise return 2
smembers key: returns all elements in the collection
scard key: returns the number of elements in the collection
smove source dest value: delete the elements in source and add them to the dest collection
sinter key1 key2 key3: Find the intersection of three sets and return
sinterstore dest key1 key2 key3: Assign the intersection of the three sets to dest
sunion key1 key2 key3: find the union of three sets
sdiff key1 key2: the difference between key1 and key2 (that is, the value that exists in key1 but does not exist in key2)
4, order set ordered set
ps: The brackets refer to optional
Ordered collection: declare the weight of the elements at the same time as the collection (used for sorting)
zadd key score1 value1 score2 value2 .. . . . : Create an ordered set
zrem key value1 value2: delete elements in the collection
zremrangebyscore key min max (limit offset N): delete elements according to the value of score, delete elements with a score value between min and max
zremrangebyrank key start end: delete elements according to rank, delete elements with ranks between [start end]
zrank key member: Query the rank of member
zrevrank key member: Query the rank of member, sorted in descending order
zrange key start stop (withscores): After sorting the set, the elements returning to the noun (start stop) are sorted in ascending order by default, and withscores is to print out the value of score
zcard key: count the number of elements in the collection
zcount key min max: Returns the number of elements in the range of [min max] in the key
zinterstore destination numkeys key1[key2.....] [weight][min |max|sum]:eg:zinterstore result 2 lisi wang aggregate sum
Of course, weight can also be added.
eg:zinterstore result 2 lisi wang weights 2 1 aggregate sum
After setting the weight, the result will be multiplied by the weight on the basis of no weight as the final result
5. Hash data structure
hset key field value: Set the value of the field field in the key to value, if there is no field, add it directly, if there is, overwrite the original value
hmset key field1 value1 field2 value2 field3 value3: Set the value of multiple fields at once
hget key field: returns the value of the field field in the key
hmget key field1 field2 field3: Get the value of multiple fields at once
hgetall key: Get the values of all fields in the key at one time
hlen key: returns the number of elements in the key
hdel key field: delete the field in the key
hexists key field: Determine whether a certain field exists
hincrby key field value: Increase the value of the field field in the key to the integer value value
hincrbyfloat key field value: Increase the value of the field field in the key to the floating-point value value
hkeys key: return all keys
hvals key: return all values
The above are the commands for various types of operations in redis. If you have not summarized them, you can leave a message to discuss.