Detailed explanation of operation commands of five data types of redis

redis basic operation commands

1. Switch database select

# redis has 16 databases by default, and the 0 database is used by default
# select database sequence
127.0.0.1:6379> select 2  #Switch to the second database
OK
127.0.0.1:6379[2]> DBSIZE  #View data size
(integer) 0
127.0.0.1:6379[2]> set ss 123
OK
127.0.0.1:6379[2]> DBSIZE
(integer) 1
127.0.0.1:6379[2]> keys *   #View all key s in the current database
1) "ss"

2. Clear the current database flushdb

127.0.0.1:6379[2]> flushdb    #Empty the current database
OK
127.0.0.1:6379[2]> keys *
(empty array)

3. Clear all databases flush all

127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> keys *
1) "counter:{tag}:__rand_int__"
2) "mylist:{tag}"
3) "key:{tag}:__rand_int__"
4) "myhash:{tag}:__rand_int__"
5) "aa"
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> keys *
(empty array)
127.0.0.1:6379[2]> flushall  # Flush clear all databases
OK
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> keys *
(empty array)

4. Use of key

commanddescribe
existsCheck whether the current key exists
expireSet expiration time of current key
ttlView the remaining expiration time of the current key
moveRemove current key
typeView the type of current key
127.0.0.1:6379[2]> set ss 123
OK
127.0.0.1:6379[2]> exists ss  #Check whether the key exists
(integer) 1
127.0.0.1:6379[2]> exists sss
(integer) 0
127.0.0.1:6379[2]> get ss
"123"
127.0.0.1:6379[2]> expire ss 20 # expire set expiration time 20s
(integer) 1
127.0.0.1:6379[2]> ttl ss  # ttl view the remaining expiration time of the current key
(integer) 12
127.0.0.1:6379[2]> ttl ss
(integer) 8
127.0.0.1:6379[2]> ttl ss
(integer) 3
127.0.0.1:6379[2]> ttl ss
(integer) -2
127.0.0.1:6379[2]> get ss
(nil)
127.0.0.1:6379[2]> keys *
1) "ss"
127.0.0.1:6379[2]> move ss 1  # move removes the current key  
(integer) 1
127.0.0.1:6379[2]> keys *
(empty array)
127.0.0.1:6379[2]> type ss  # type key view the type of the current key
string

5. Five data types

5.1 String

  • Set new key value set key value

  • get gets the value of the current key

  • Append append string. If the current key does not exist, it is equivalent to adding the current key(set operation) append key value

  • strlen view the length of the value of the current key

127.0.0.1:6379[2]> set key v1 #Add key value
OK
127.0.0.1:6379[2]> get key   #Get the value of the current key
"v1"
127.0.0.1:6379[2]> append key hello,word! # Append string. If the current key does not exist, it is equivalent to adding the current key
(integer) 13
127.0.0.1:6379[2]> strlen key #View the length of the value of the current key
(integer) 13
127.0.0.1:6379[2]> get key
"v1hello,word!"
127.0.0.1:6379[2]> keys *
1) "key"
2) "ss"
127.0.0.1:6379[2]> append cc 22aa #If the current key does not exist, it is equivalent to adding the current key(set operation)
(integer) 4
127.0.0.1:6379[2]> keys *
1) "cc"
2) "key"
3) "ss"
  • Incr auto increment is equivalent to i++ incr key
  • Decr auto decrement is equivalent to i-- decr key
  • Incrby sets the step size, which is equivalent to i+=n incrby key step size
  • Decrby sets the step size, which is equivalent to i-=n decrby key step size
127.0.0.1:6379[4]> set v1 0
OK
127.0.0.1:6379[4]> incr v1 #Auto increment operation
(integer) 1
127.0.0.1:6379[4]> incr v1
(integer) 2
127.0.0.1:6379[4]> incr v1
(integer) 3
127.0.0.1:6379[4]> get v1
"3"
127.0.0.1:6379[4]> decr v1 #Self subtraction operation
(integer) 2
127.0.0.1:6379[4]> decr v1
(integer) 1
127.0.0.1:6379[4]> get v1
"1"
127.0.0.1:6379[4]> incrby v1 5 #Set the step size, increasing by 5 at a time
(integer) 11
127.0.0.1:6379[4]> get v1
"6"
127.0.0.1:6379[4]> incrby v1 5
(integer) 16
127.0.0.1:6379[4]> incrby v1 5
(integer) 21
127.0.0.1:6379[4]> decrby v1 8 #Set the step size, subtracting 8 at a time
(integer) 13
127.0.0.1:6379[4]> decrby v1 8
(integer) 5
127.0.0.1:6379[4]> get v1
"5"
  • Getrange intercepts string getrange key start end
  • Setrange replacement string setrange key start xxx
127.0.0.1:6379[4]> flushdb
OK
127.0.0.1:6379[4]> set b1 "hello word!"
OK
127.0.0.1:6379[4]> get b1
"hello word!"
127.0.0.1:6379[4]> getrange b1 0 3 #Intercept string
"hell"
127.0.0.1:6379[4]> getrange b1 0 -2 #If the end point is negative, all strings are obtained
"hello word"
127.0.0.1:6379[4]> getrange b1 0 0
"h"
127.0.0.1:6379[4]> getrange b1 0 -1
"hello word!"
127.0.0.1:6379[4]> setrange b1 2 zzz #Modify the string and replace it from the specified position
(integer) 11
127.0.0.1:6379[4]> get b1
"hezzz word!"
127.0.0.1:6379[4]> setrange b1 5 zzzzzzzzzzzzzzzz  #The excess part is automatically appended
(integer) 21
127.0.0.1:6379[4]> get b1
"hezzzzzzzzzzzzzzzzzzz"
  • Setex set expiration time setex key x value
  • setnx key value is created when the current setnx key does not exist
127.0.0.1:6379[4]> setex k1 50 value1  #Set expiration time
OK
127.0.0.1:6379[4]> ttl k1
(integer) 35
127.0.0.1:6379[4]> keys *
1) "c1"
2) "b1"
3) "k1"
127.0.0.1:6379[4]> ttl k1
(integer) 18
127.0.0.1:6379[4]> ttl k1
(integer) -2
127.0.0.1:6379[4]> get k1
(nil)
127.0.0.1:6379[4]> keys *
1) "c1"
2) "b1"
127.0.0.1:6379[4]> set k2 aal
OK
127.0.0.1:6379[4]> keys *
1) "k2"
2) "c1"
3) "b1"
#Create when the current key does not exist. Failed to create when the current key exists
127.0.0.1:6379[4]> setnx k2 lslsl 
(integer) 0
127.0.0.1:6379[4]> get k2
"aal"
127.0.0.1:6379[4]> setnx k3 sssl
(integer) 1
127.0.0.1:6379[4]> keys *
1) "k3"
2) "k2"
3) "c1"
4) "b1"
127.0.0.1:6379[4]> get k3
"sssl"
  • mset adds multiple values at the same time mset k1 v1 k2 v2 k3 v3
  • Mget gets multiple values at the same time mget k1 k2 k3
  • msetnx processes multiple keys at the same time. msetnx k1 v1 k2 v2 is created when the current key does not exist (it is an atomic operation. When the creation of one key fails, the whole operation fails)
127.0.0.1:6379[4]> keys *
(empty array)
127.0.0.1:6379[4]> mset k1 v1 k2 v2 k3 v3 #Add multiple values at the same time
OK
127.0.0.1:6379[4]> mget k1 k2 k3  #Get multiple values at the same time
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379[4]> msetnx k4 v4
(integer) 1
127.0.0.1:6379[4]> keys *
1) "k4"
2) "k3"
3) "k2"
4) "k1"
127.0.0.1:6379[4]> msetnx k5 v5 k6 v6
(integer) 1
127.0.0.1:6379[4]> keys *
1) "k2"
2) "k6"
3) "k4"
4) "k3"
5) "k5"
6) "k1"
# Process multiple keys at the same time. Create 'msetnx k1 v1 k2 v2' when the current key does not exist (it is an atomic operation. When one key fails to be created, the whole operation fails)
127.0.0.1:6379[4]> msetnx k5 v5 k8 v8 
(integer) 0
127.0.0.1:6379[4]> keys *
1) "k2"
2) "k6"
3) "k4"
4) "k3"
5) "k5"
6) "k1"
Set the design of an object user:1 {name:zhangsan,gender:nan}, key user: {ID}: {file}
127.0.0.1:6379[4]> mset user:1:name zhangsan user:1:gender nan
OK
127.0.0.1:6379[4]> mget user:1:name user:1:gender
1) "zhangsan"
2) "nan"
  • getset get before set

    1. If the current key does not exist, return null first, and then create it

    2. If the current key exists, first return the current value, and then reset the value of the current key

127.0.0.1:6379[4]> keys *
1) "k2"
2) "user:1:name"
3) "k6"
4) "k4"
5) "user:1:gender"
6) "k3"
7) "k5"
8) "k1"
127.0.0.1:6379[4]> flushdb
OK
127.0.0.1:6379[4]> keys *
(empty array)
127.0.0.1:6379[4]> getset v1 hello  # If the current key does not exist, return null first, and then create the key
(nil)
127.0.0.1:6379[4]> get v1
"hello"
127.0.0.1:6379[4]> getset v1 word  # If the current key exists, first return the current value, and then reset the value of the current key
"hello"
127.0.0.1:6379[4]> get v1
"word"

5.2 List

  • lpush inserts one or more values into the list header (left)
  • rpush inserts one or more values at the end (right) of the list
  • lrange gets the value of the list (the value of the list interval can be obtained)
  • lpop removes the elements of the list, starting on the left
  • rpop removes the elements of the list, starting on the right
  • lindex index
  • llen gets the length of the list
  • lrem removes the specified number of value s for exact matching
  • trim intercepts the value of the element interval
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> lpush list one two three  #Insert one or more values into the list header (left)
(integer) 3
127.0.0.1:6379> lrange list 0 -1 #Get all values of the list
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lrange list 0 1  #Gets the value of the list interval
1) "three"
2) "two"
127.0.0.1:6379> rpush list aa bb cc  #Insert one or more values at the end (right) of the list
(integer) 6
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "aa"
5) "bb"
6) "cc"
127.0.0.1:6379> lpop list  #Remove the first element of the list, starting on the left
"three"
127.0.0.1:6379> rpop list   #Remove the first element of the list, starting on the right
"cc"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "aa"
4) "bb"
127.0.0.1:6379> lindex list 0  #Gets the first element of the list
"two"
127.0.0.1:6379> lindex list 1
"one"
127.0.0.1:6379> lindex list 2
"aa"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "aa"
4) "bb"
127.0.0.1:6379> llen list   #Gets the length of the list
(integer) 4
127.0.0.1:6379> lpush list one one one two
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "one"
4) "one"
127.0.0.1:6379> lrem list 1 one #Remove the specified number of value s and match them exactly
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "one"
127.0.0.1:6379> lrem list 2 one 
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "two"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> lpush li one two three four five
(integer) 5
127.0.0.1:6379> lrange li 0 -1
1) "five"
2) "four"
3) "three"
4) "two"
5) "one"
127.0.0.1:6379> ltrim li 1 2 #Intercept the value of the element interval
OK
127.0.0.1:6379> lrange li 0 -1
1) "four"
2) "three"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> rpush li one two three four five
(integer) 5
127.0.0.1:6379> lrange li 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"
127.0.0.1:6379> ltrim li 1 2
OK
127.0.0.1:6379> lrange li 0 -1
1) "two"
2) "three"

  • Rpop lpush removes the last element of the list and moves it to the new list
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> lpush li one two three
(integer) 3
127.0.0.1:6379> rpoplpush li ki
"one"
127.0.0.1:6379> lrange li 0 -1
1) "three"
2) "two"
127.0.0.1:6379> lrange ki 0 -1
1) "one"
  • lset replaces the specified index of the list with another value
127.0.0.1:6379> lpush list aa
(integer) 1
127.0.0.1:6379> rpop list
"aa"
127.0.0.1:6379> exists list
(integer) 0
127.0.0.1:6379> lset list 0 it #When the list has no value, the replacement fails
(error) ERR no such key
127.0.0.1:6379> lpush list hello
(integer) 1
127.0.0.1:6379> lrange list 0 -1 
1) "hello"
127.0.0.1:6379> lset list 0 it  #Replace the first value with the specified value
OK
127.0.0.1:6379> lrange list 0 -1 
1) "it"
127.0.0.1:6379> lset list 1 it #The replacement fails when the list index does not exist
(error) ERR index out of range
  • linsert inserts a specific value before and after an element in the list
127.0.0.1:6379> rpush list aa bb cc
(integer) 3
127.0.0.1:6379> linsert list before bb dd #Inserts a specific value before an element in the list
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "aa"
2) "dd"
3) "bb"
4) "cc" 
127.0.0.1:6379> linsert list after bb ff  #Inserts a specific value after an element in the list
(integer) 5
127.0.0.1:6379> lrange list 0 -1
1) "aa"
2) "dd"
3) "bb"
4) "ff"
5) "cc"

5.3 Set

Unordered, non repeating sets;

  • sadd adds elements to the collection
  • smembers view all values for the specified collection
  • sismember determines whether a value is in this collection
  • scard views the number of elements in the collection
  • srem removes the specified element from the collection
  • srandmember randomly selects elements (the number can be specified)
  • spop randomly deletes some elements in the collection, and multiple can be specified
127.0.0.1:6379> sadd myset aa bb cc  #Add elements to the collection
(integer) 3
127.0.0.1:6379> smembers myset   #View all values for the specified collection
1) "cc"
2) "aa"
3) "bb"
# Judge whether a value is in this collection. If it returns 1, it does not return 0
127.0.0.1:6379> sismember myset bb
(integer) 1
127.0.0.1:6379> sismember myset dd
(integer) 0
127.0.0.1:6379> scard myset  # View the number of elements in the collection
(integer) 3
127.0.0.1:6379> smembers myset
1) "cc"
2) "aa"
3) "bb"
127.0.0.1:6379> sadd myset cc  #set type, value cannot be repeated
(integer) 0
127.0.0.1:6379> srem myset aa  #Removes the specified element from the collection
(integer) 1
127.0.0.1:6379> smembers myset
1) "cc"
2) "bb"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> sadd myset aa bb cc dd ff ee
(integer) 6
127.0.0.1:6379> srandmember myset  #Randomly select 1 element
"aa"
127.0.0.1:6379> srandmember myset 
"dd"
127.0.0.1:6379> srandmember myset 
"bb"
127.0.0.1:6379> srandmember myset 
"bb"
127.0.0.1:6379> srandmember myset 2  #Randomly select 2 elements
1) "dd"
2) "cc"
127.0.0.1:6379> srandmember myset 2
1) "cc"
2) "bb"
127.0.0.1:6379> srandmember myset 2
1) "ff"
2) "ee"
127.0.0.1:6379> srandmember myset 3  #Randomly select 3 elements
1) "dd"
2) "bb"
3) "ee"
127.0.0.1:6379> srandmember myset 3
1) "aa"
2) "cc"
3) "ff"
127.0.0.1:6379> smembers myset
1) "dd"
2) "aa"
3) "bb"
4) "cc"
5) "ff"
6) "ee"
127.0.0.1:6379> spop myset #Randomly delete 1 element in the collection
"bb"
127.0.0.1:6379> smembers myset
1) "dd"
2) "aa"
3) "cc"
4) "ff"
5) "ee"
127.0.0.1:6379> spop myset 2  #Randomly delete 2 elements in the collection
1) "ff"
2) "dd"
127.0.0.1:6379> smembers myset
1) "aa"
2) "cc"
3) "ee"
  • smove moves a specified value to another collection
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> sadd li aa bb cc dd
(integer) 4
127.0.0.1:6379> sadd li1 tt
(integer) 1
127.0.0.1:6379> smembers li
1) "dd"
2) "cc"
3) "aa"
4) "bb"
127.0.0.1:6379> smembers li1
1) "tt"
127.0.0.1:6379> smove li li1 aa  #Moves a specified value to another collection
(integer) 1
127.0.0.1:6379> smembers li
1) "dd"
2) "cc"
3) "bb"
127.0.0.1:6379> smembers li1
1) "tt"
2) "aa"
  • sdiff takes the difference set of two sets
  • sinter takes the intersection of two sets
  • sunion takes the union of two sets
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> sadd set1 a b c
(integer) 3
127.0.0.1:6379> sadd set2 c d e
(integer) 3
127.0.0.1:6379> sdiff set1 set2  #Difference set 
1) "b"
2) "a"
127.0.0.1:6379> sinter set1 set2 #intersection
1) "c" 
127.0.0.1:6379> sunion set1 set2 #Union
1) "a"
2) "c"
3) "b"
4) "e"
5) "d"

5.4 Hash

Map set, key map;

  • hset adds a new key map
  • hmset adds multiple key maps
  • hget gets a key map
  • hmget getting multiple key maps
  • hgetall get all key maps
  • hdel deletes the key field specified by hash, and the corresponding value will also be deleted
  • Helen checks the number of elements in the current hash
127.0.0.1:6379> hset myhash k1 v1 #Add a key map
(integer) 1
# Add multiple key maps
127.0.0.1:6379> hmset myhash k1 v1 k2 v2 k3 v3
OK
# Get a key map
127.0.0.1:6379> hget myhash k1
"v1"
#Get multiple key maps
127.0.0.1:6379> hmget myhash k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
# Get all key maps
127.0.0.1:6379> hgetall myhash
1) "k1"
2) "v1"
3) "k2"
4) "v2"
5) "k3"
6) "v3"
# Delete the key field specified in the hash, and the corresponding value will also be deleted
127.0.0.1:6379> hdel myhash k1
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "k2"
2) "v2"
3) "k3"
4) "v3"
# View the number of elements in the current hash
127.0.0.1:6379> hlen myhash
(integer) 2
  • hexists determines whether the specified key exists in the current hash
  • hkeys gets all the keys of the current hash
  • hvals gets all value s of the current hash
  • hincrby set increment
  • hsetnx determines whether this key exists in the current hash. If it does not exist, it will be created. The creation fails
127.0.0.1:6379> hgetall myhash
1) "k2"
2) "v2"
3) "k3"
4) "v3"
# Judge whether the specified key exists in the current hash
127.0.0.1:6379> hexists myhash k2
(integer) 1
127.0.0.1:6379> hexists myhash k1
(integer) 0
127.0.0.1:6379> hkeys myhash
1) "k2"
2) "k3"
127.0.0.1:6379> hvals myhash
1) "v2"
2) "v3"
127.0.0.1:6379> hset nyhash k5 0  
(integer) 1
127.0.0.1:6379> hincrby myhash k5 3 #  Set the increment. If the increment is negative, it is equivalent to decr
(integer) 3
127.0.0.1:6379> hincrby myhash k5 3
(integer) 6
127.0.0.1:6379> hincrby myhash k5 -1
(integer) 5
127.0.0.1:6379> hsetnx myhash k6 aaa #Judge whether it exists or not
(integer) 1
127.0.0.1:6379> hsetnx myhash k6 bbb  #Judge whether it exists. Creation failed
(integer) 0

hash is more suitable for storing objects:

127.0.0.1:6379> hset user:1 name zhangsan age 15 gender nan email 858555@qq.com
(integer) 4
127.0.0.1:6379> hgetall user:1
1) "name"
2) "zhangsan"
3) "age"
4) "15"
5) "gender"
6) "nan"
7) "email"
8) "858555@qq.com"

5.5 Zset (ordered set)

  • zadd add element
  • zrange view elements from small to large by default
  • zrangebyscore displays elements according to the key of the current Zset
  • zrevrange view elements, sorted from large to small
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> zadd k 100 xx   #Add element
(integer) 1
127.0.0.1:6379> zadd k 101 xx1
(integer) 1
127.0.0.1:6379> zadd k 200 xx2
(integer) 1
127.0.0.1:6379> zrange k 0 -1  #View all elements
1) "xx"
2) "xx1"
4) "xx2"
127.0.0.1:6379> zrangebyscore k -inf +inf  #View all elements and sort them from small to large according to the key of the current Zset
1) "xx"
2) "xx1"
3) "xx2"
127.0.0.1:6379> zadd k 159 xx3
(integer) 1
127.0.0.1:6379> zrangebyscore k -inf +inf 
1) "xx"
2) "xx1"
3) "xx3"
4) "xx2"
#View all elements, sort them from small to large, and add the key of the current Zset
127.0.0.1:6379> zrangebyscore k -inf +inf withscores
1) "xx"
2) "100"
3) "xx1"
4) "101"
5) "xx3"
6) "159"
7) "xx2"
8) "200"
#View all elements whose key of the current Zset is less than 159, sort from small to large, and add the key of the current Zset
127.0.0.1:6379> zrangebyscore k -inf 159 withscores
1) "xx"
2) "100"
3) "xx1"
4) "101"
5) "xx3"
6) "159"
127.0.0.1:6379> zrange k 0 -1
1) "xx1"
2) "xx3"
3) "xx2"
127.0.0.1:6379> zrevrange k 0 -1 #Sort from large to small
1) "xx2"
2) "xx3"
3) "xx1"


  • zrem deletes the specified value of the current Zset
  • zcard the number of elements in the current Zset
  • zcount gets the number of key s in a certain interval of the current Zset
127.0.0.1:6379> zrange k 0 -1
1) "xx"
2) "xx1"
3) "xx3"
4) "xx2"
127.0.0.1:6379> zrem k xx  #Deletes the specified value of the current Zset
(integer) 1
127.0.0.1:6379> zrange k 0 -1
1) "xx1"
2) "xx3"
3) "xx2"
127.0.0.1:6379> zcard k  #Number of elements in the current Zset
(integer) 3
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> zadd k 1 aa 2 bb 3 cc 4 dd
(integer) 4
# Gets the number of key s in a certain interval of the current Zset
127.0.0.1:6379> zcount k 1 3
(integer) 3
127.0.0.1:6379> zcount k 1 2
(integer) 2
127.0.0.1:6379> zcount k 1 4
(integer) 4

Keywords: Redis

Added by jase01 on Mon, 17 Jan 2022 08:05:48 +0200