Redis data types and operation commands

The five most commonly used data types in Redis are: String, Hash, List, Set, and Sorted Set.

 

1. String

 String is the most commonly used data type in Redis. The data structure of String is key/value type, and String can contain any data.

 Common commands: set, get, decr, incr, mget, etc.

new
set sets a new value for a key, overwriting the old value
mset batch setting key
getset set value, get value at the same time
setex sets the expiration time of the specified key, and the value can be obtained during the survival time
setnx If the key does not exist, set a new value for the key

delete
del deletes an existing key

Revise
The incr value is increased by 1. If the key does not exist, the key is created, the initial value is set to 0, and the result after the increase is 1
append If the key does not exist, return the length of the current Value; if the key already exists, return the length of the value after appending
Decrby value decreases the specified value

Inquire
get Get the Value corresponding to the Key
mget batch get keys  
exists to determine whether the key exists, returns 1 if it exists, otherwise returns 0
strlen gets the character length of the specified Key
ttl View the remaining survival time of the specified Key (seconds)

 

2. Hash

 Hash type can be regarded as a Map container whose key/value are String.

 Common commands: hget, hset, hgetall, etc.

new
hset If it does not exist, create the key and its associated Hashes, if it exists, it is invalid
hmset sets multiple fields at once

delete
hdel deletes the specified field in the specified key
del delete key

Revise  
hincrby adds 1 to the value of the specified field

Inquire
hget gets the value of the specified field in the specified key value
hlen gets the number of fields for the specified key
hexists determines whether a field exists in the specified key
hmget batch get fields
hgetall returns all fields and their values ​​for the specified key
hkeys Get the names of all fields in the specified key
hvals get the values ​​of all fields in the specified key

 

3. List

  List is used to store an ordered list of strings. Common operations are to add elements to both ends of the queue or to get a certain segment of the list.

  Common commands: lpush, rpush, lpop, rpop, lrange, etc.

new
lpush adds elements to the left end of the list. Example: lpush key value
linsert inserts an element into a list. Example: linsert key BEFORE|AFTER privot value, look for the first element whose value is privot from the left, and then decide to insert the value before or after the element according to whether the second parameter is BEFORE or AFTER
rpush adds elements to the right end of the list. Example: rpush key value
rpoplpush pops the tail element of one list and inserts it at the head of another list (atomic operation). Example: rpoplpush source destination

delete
del deletes an existing key. Example: del key
lrem removes the specified value from the list. Example: lrem key count value deletes the first count elements in the list whose value is value. When count>0, counting starts from the left, when count<0, starts counting from the right, and when count=0, all elements with value are deleted.
ltrim retains the specified range of the list, and deletes the rest. Example: ltrim key begin end includes begin end

Revise
lset sets the value of the specified index. Example: lset key index value

Inquire
lrange Gets the elements of the specified range in the list. Example: lrange key begin end index starts from 0, -1 means the last element.
lpop Pops elements from the left end of the list. Example: lpop key
rpop pops elements from the right end of the list. Example: rpop key
lindex Gets the element value at the specified index. Example: lindex key index
llen gets the number of elements in the list. Example: llen key

 

4. Set

 Set can be understood as an unordered set of characters. The same elements in the Set will not appear repeatedly, and only one of the same elements is retained.

 Common commands: sadd, spop, smembers, sunion, etc.

new
sadd adds elements. Example: sadd key value1 [value2 value3 ...]

delete
spop pops a random element from the collection. Example: spop key
srem deletes an element, for example: srem key value1 [value2 value3 ...]

Revise
smove moves elements to another collection. Example: remove key1 key2 value

Inquire
sismember judges whether the element is in the set, and the return value is 1 to indicate that it exists. Example: sismember key value   
smembers gets all the elements in the collection. Example: smembers key
scard gets the number of elements in the collection. Example: scard key
sdiff performs a subtraction operation on sets. Example: sdiff key1 key2 [key3 ...], first calculate the difference between key1 and key2, and then use the result and key3 to make the difference
sinter performs the intersection operation on sets. Example: sinter key1 key2 [key3...]
sunion performs a union operation on sets. Example: sunion key1 key2 [key3 ...]
sdiffstore performs a difference operation on sets and stores the result. Example: sdiffstore destination key1 key2 [key3 ...]
sinterstore performs the intersection operation on sets and stores the result. Example: sinterstore destination key1 key2 [key3 ...]
sunionstore performs a union operation on sets and stores the result. Example: sunionstore destination key1 key2 [key3 ...]
srandmember randomly gets the elements in the collection.

 

Five, Sorted Set (ordered collection)

  An ordered set is to associate a score for each element on the basis of the set, and Redis sorts the members of the set by the score.

  Common commands: zadd, zrange, zrem, zcard, etc.

new
zadd adds elements. Example: zadd key score1 value1 [score2 value2 score3 value3 ...]

delete
zrem deletes one or more member variables, returning the number of deletions. Example: zrem key value1 [value2 ...]

Revise
Zincrby increases a member's score and returns the member's updated score. Example: zincrby key increment value

Inquire
zrange Get members ranked in the specified range, WITHSCORES option indicates whether to return member scores. Example: zrange key start stop [WITHSCORE]
zrank Gets the index value of the position of the member in positive order in the collection, 0 means the first position. Example: zrank key value
zcard gets the number of elements in the collection. Example: zcard key
zcount Gets the number of elements within the specified score range. Example: zcount key min max
zscore Gets the element's score. Example: zscore key value
zrangebyscore Gets elements within the specified score range. Example: zrangebyscore key min max

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326595116&siteId=291194637