This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

👉 comes first

  • In this article, we will summarize the common data types and related common commands in Redis

Commands:

www.redis.cn/commands.ht…

Redis-Key

EXPIRE key10

10 s is overdue

ttl key

Query the remaining expiration time

type key

Check the type

Move name 1(1 indicates the current database)

Remove the current key

hodgepodge

127.0.0.1:6379 >keys * # check all keys
(empty list or set)
127.0.0.1:6379> set name kuangshen # set key
OK
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> set age 1
OK
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> EXISTS name Check whether the current key exists
(integer) 1
127.0.0.1:6379> EXISTS name1
(integer) 0
127.0.0.1:6379> move name 1 Remove the current key
(integer) 1
127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379> set name qinjiang
OK
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> clear
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> get name
"qinjiang"
127.0.0.1:6379> EXPIRE name 10 Set the key expiration time in seconds
(integer) 1
127.0.0.1:6379> ttl name # check the remaining time of the current key
(integer) 4
127.0.0.1:6379> ttl name
(integer) 3
127.0.0.1:6379> ttl name
(integer) 2
127.0.0.1:6379> ttl name
(integer) 1
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> type name # check a type of the current key!
string
127.0.0.1:6379> type age
string
Copy the code

String

Appen and strlen

127.0.0.1:6379 >set key1 v1 # set values
OK
127.0.0.1:6379> get key1 # get value
"v1"
127.0.0.1:6379> keys * Get all keys
1) "key1"
127.0.0.1:6379> EXISTS key1 Check whether a key exists
(integer) 1
127.0.0.1:6379> APPEND key1 "hello" # append string, equivalent to setkey if current key does not exist
(integer) 7
127.0.0.1:6379> get key1
"v1hello"
127.0.0.1:6379> STRLEN key1 Get the length of the string!
(integer) 7
127.0.0.1:6379> APPEND key1 ",kaungshen"
(integer) 17
127.0.0.1:6379> STRLEN key1
(integer) 17
127.0.0.1:6379> get key1
"v1hello,kaungshen"
Copy the code

Incr and DECR increment and decR increment

# i++
I + = # step length127.0.0.1:6379 >set views 0 # initial page views are 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views # increase 1 pageviews to 1
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> get views
"2"
127.0.0.1:6379> decr views # Decrease page views by 1
(integer) 1
127.0.0.1:6379> decr views
(integer) 0
127.0.0.1:6379> decr views
(integer) -1
127.0.0.1:6379> get views
"1"
127.0.0.1:6379> INCRBY views 10 # you can set the step size, specify the increment!
(integer) 9
127.0.0.1:6379> INCRBY views 10
(integer) 19
127.0.0.1:6379> DECRBY views 5
Copy the code

GetRange String range setRange replaces the string starting at the specified position

# string range127.0.0.1:6379 >set key1 "hello,kuangshen" # set key1
OK
127.0.0.1:6379> get key1
"hello,kuangshen"
127.0.0.1:6379> GETRANGE key1 0 3 # intercept string [0,3]
"hell"
127.0.0.1:6379> GETRANGE key1 0 -1 # Get the entire string as well as get key
"hello,kuangshen"
# replace!
127.0.0.1:6379> set key2 abcdefg
OK
127.0.0.1:6379> get key2
"abcdefg"
127.0.0.1:6379> SETRANGE key2 1 xx # Replace the string at the beginning of the specified position!
(integer) 7
127.0.0.1:6379> get key2
"axxdefg"
Copy the code

Set expiration time setex setnx

# setex (set with expire) # set expiration time
# setnx (set if not exist) #127.0.0.1:6379 >setex key3 30 "hello" # Set key3 to hello and expire after 30 seconds
OK
127.0.0.1:6379> ttl key3
(integer) 26
127.0.0.1:6379> get key3
"hello"
127.0.0.1:6379> setnx mykey "redis" If mykey does not exist, create mykey
(integer) 1
127.0.0.1:6379> keys *
1) "key2"
2) "mykey"
3) "key1"
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379> setnx mykey "MongoDB" # if mykey exists, create failed!
(integer) 0
127.0.0.1:6379> get mykey
Copy the code

Mset /get sets/gets multiple values simultaneously

Msetnx is an atomic operation that either succeeds together or together

mset
mget
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 # set multiple values simultaneously
OK
127.0.0.1:6379> keys *
1) "k1"
2) "k2"
3) "k3"
127.0.0.1:6379> mget k1 k2 k3 Get multiple values at once
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 v1 k4 v4 # msetnx is an atomic operation that either succeeds or fails together!
(integer) 0
127.0.0.1:6379> get k4
(nil)
Copy the code

Object Set user:1:name XXX User :1:age XXX

Key =user:1:name; key=user:1:name

# object
set user:1 {name:zhangsan,age:3} # Set a user:1 object value to json character to save an object!
Filed} user:{id} {filed} user:{id} {filed}127.0.0.1:6379 >mset user:1:name zhangsan user:1:age 2
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "2"
Copy the code

Get the set at the same time

getset Get then set
127.0.0.1:6379> getset db redis # If no value exists, return nil
(nil)
127.0.0.1:6379> get db
"Redis 127.0.0.1:6379> getset db mongodbredis"127.0.0.1:6379> get db"mongodb"
Copy the code

Usage scenarios

String is used in a similar way: value can be our number as well as our String!

  • counter
  • Count the number of multiple units
  • Number of fans
  • Object cache storage!

Hash(similar to String)

Map collection, key-map! When this value is a map set! Not much different in nature from String, a simple key-vlaue! Hash stores frequently changing data user name age, especially information such as user information that changes frequently! Hash is better for objects and String is better for strings!

hset/get

Hdel (String like del)

Hkeys /hvals(only all keys/ Val)

You can also specify delta, you can hsetnx

127.0.0.1:6379 >hset myhash field1 kuangshen # set a specific key-vlaue
(integer) 1
127.0.0.1:6379> hget myhash field1 Get a field value
"kuangshen"
127.0.0.1:6379> hmset myhash field1 hello field2 world # set multiple key-vlaue
OK
127.0.0.1:6379> hmget myhash field1 field2 Get multiple field values
1) "hello"
2) "world"
127.0.0.1:6379> hgetall myhash Get all the data,
1) "field1"
2) "hello"
3) "field2"
4) "world"
127.0.0.1:6379> hdel myhash field1 Hash hash hash hash hash hash The corresponding value disappears!
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "field2"
2) "world"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
hlen
127.0.0.1:6379> hmset myhash field1 hello field2 world
OK
127.0.0.1:6379> HGETALL myhash
1) "field2"
2) "world"
3) "field1"
4) "hello"
127.0.0.1:6379> hlen myhash Hash table hash table hash table
(integer) 2
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
127.0.0.1:6379> HEXISTS myhash field1 # check whether the specified hash field exists!
(integer) 1
127.0.0.1:6379> HEXISTS myhash field3
(integer) 0
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Get only all fields
Get only all values
127.0.0.1:6379> hkeys myhash Get only all fields
1) "field2"
2) "field1"
127.0.0.1:6379> hvals myhash Get only all values
1) "world"
2) "hello"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
incr decr
127.0.0.1:6379> hset myhash field3 5 # specify increment!
(integer) 1
127.0.0.1:6379> HINCRBY myhash field3 1
(integer) 6
127.0.0.1:6379> HINCRBY myhash field3 -1
(integer) 5
127.0.0.1:6379> hsetnx myhash field4 hello If it does not exist, it can be set
(integer) 1
127.0.0.1:6379> hsetnx myhash field4 world # If it exists, it cannot be set
(integer) 0
Copy the code

List

Can play into two-way queues, stacks, etc

  • The bottom layer is a bidirectional linked list. The insertion and deletion efficiency of two segments is relatively high, while the insertion and change of intermediate index values are relatively slow, because the query is slow

Lpush and Rpush LRange check interval values (0 to 1 check all values)

It’s like a stack if you just go in on the left,

127.0.0.1:6379 >LPUSH list one Insert one or more values into the head of the list (left)
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1 Get the value from list!
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LRANGE list 0 1 Get a specific value from the interval!
1) "three"
2) "two"
127.0.0.1:6379> Rpush list righr Insert one or more values to the end of the list (right)
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "righr"
Copy the code

Lpop and Rpop

LPOP
RPOP
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "righr"
127.0.0.1:6379> Lpop list Remove the first element of the list
"three"
127.0.0.1:6379> Rpop list Remove the last element of the list
"righr"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
Copy the code

Lindex(get a value in a list by subscript)

Lindex
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lindex list 1 Get a value from a list by subscript!
"one"
127.0.0.1:6379> lindex list 0
"two"
Copy the code

Llen Gets the length

Llen
127.0.0.1:6379> Lpush list one
(integer) 1
127.0.0.1:6379> Lpush list two bilibili: Java(integer)2
127.0.0.1:6379> Lpush list three
(integer) 3
127.0.0.1:6379> Llen list Return the length of the list
(integer) 3
Copy the code

Lrem removes count of specified values

Notice that we delete it from the left

Removes the specified value! Take offuid
Lrem
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> lrem list 1 one # remove the specified number of values from the list
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379> lrem list 1 three
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
127.0.0.1:6379> Lpush list three
(integer) 3
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
Copy the code

Trim the interception

By subscripting the specified length, it is understood that only the elements of the truncated interval are retained

trimPruning. ; Cutting the list!127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> Rpush mylist "hello"
(integer) 1
127.0.0.1:6379> Rpush mylist "hello1"
(integer) 2
127.0.0.1:6379> Rpush mylist "hello2"
(integer) 3
127.0.0.1:6379> Rpush mylist "hello3"
(integer) 4
127.0.0.1:6379> ltrim mylist 1 2 The list has been changed and truncated by subscripting the specified length, leaving only the truncated elements!
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"
Copy the code

Rpoplpush removes the specified value and moves it to a new list

rpoplpush Remove the last element of the list and move it to the new list!
127.0.0.1:6379> rpush mylist "hello"
(integer) 1
127.0.0.1:6379> rpush mylist "hello1"
(integer) 2
127.0.0.1:6379> rpush mylist "hello2"
(integer) 3
127.0.0.1:6379> rpoplpush mylist myotherlist Remove the last element of the list and move it to the new list!
"hello2"
127.0.0.1:6379> lrange mylist 0 -1 # View the original list
1) "hello"
2) "hello1"
127.0.0.1:6379> lrange myotherlist 0 -1 # Check the target list, there is indeed a change value!
1) "hello2"
Copy the code

Lset Modifies the value of the specified subscript

lset Replace the index value in the list with another value, update operation
127.0.0.1:6379> EXISTS list Check whether the list exists
(integer) 0
127.0.0.1:6379> lset list 0 item If the list does not exist, an error will be reported when we try to update it
(error) ERR no such key
127.0.0.1:6379> lpush list value1
(integer) 1
127.0.0.1:6379> LRANGE list 0 0
1) "value1"
127.0.0.1:6379> lset list 0 item Update the value of the current subscript if it exists
OK
127.0.0.1:6379> LRANGE list 0 0
1) "item"
127.0.0.1:6379> lset list 1 other # if it does not exist, an error is reported!
(error) ERR index out of range
Copy the code

Insert Inserts a value before or after the specified value

linsert Insert a specific value before or after an element in your column!
127.0.0.1:6379> Rpush mylist "hello"
(integer) 1
127.0.0.1:6379> Rpush mylist "world"
(integer) 2
127.0.0.1:6379> LINSERT mylist before "world" "other"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
127.0.0.1:6379> LINSERT mylist after world new
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
4) "new"
Copy the code

Set

The bottom is a hash table and the query is O(1)

Members of the relevant

Sadd Adds a member

Smember Displays all members

SIsMember Checks whether it is a member

Scard gets the number of elements

Srem Deletes the specified value

SrandMember Gets random members (no pop)

Spop pops up randomly

hodgepodge

127.0.0.1:6379 >sadd myset "hello" # add constant speed to set
(integer) 1
127.0.0.1:6379> sadd myset "kuangshen"
(integer) 1
127.0.0.1:6379> sadd myset "lovekuangshen"
(integer) 1
127.0.0.1:6379> SMEMBERS myset Set to view all values of the specified set
1) "hello"
2) "lovekuangshen"
3) "kuangshen"
127.0.0.1:6379> SISMEMBER myset hello Check whether a value is in a set.
(integer) 1
127.0.0.1:6379> SISMEMBER myset world
(integer) 0
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
127.0.0.1:6379> scard myset Select * from set;
(integer) 4
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
rem
127.0.0.1:6379> srem myset hello Removes the specified element from the set
(integer) 1
127.0.0.1:6379> scard myset
(integer) 3
127.0.0.1:6379> SMEMBERS myset
1) "lovekuangshen2"
2) "lovekuangshen"
3) "kuangshen"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #Set unordered non-repeating collection. Take out random!127.0.0.1:6379> SMEMBERS myset
1) "lovekuangshen2"
2) "lovekuangshen"
3) "kuangshen"
127.0.0.1:6379> SRANDMEMBER myset Select an element at random
"kuangshen"
127.0.0.1:6379> SRANDMEMBER myset
"kuangshen"
127.0.0.1:6379> SRANDMEMBER myset
"kuangshen"
127.0.0.1:6379> SRANDMEMBER myset
"kuangshen"
127.0.0.1:6379> SRANDMEMBER myset 2 Select a specified number of elements at random
1) "lovekuangshen"
2) "lovekuangshen2"
127.0.0.1:6379> SRANDMEMBER myset 2
1) "lovekuangshen"
2) "lovekuangshen2"
127.0.0.1:6379> SRANDMEMBER myset Select an element at random
"lovekuangshen2"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #Delete certain key, randomly delete key!127.0.0.1:6379> SMEMBERS myset
1) "lovekuangshen2"
2) "lovekuangshen"
3) "kuangshen"
127.0.0.1:6379> spop myset # Randomly delete some elements from set!
"lovekuangshen2"
127.0.0.1:6379> spop myset
"lovekuangshen"
127.0.0.1:6379> SMEMBERS myset
1) "kuangshen"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #Move a specified value to another set!127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> sadd myset "world"
(integer) 1
127.0.0.1:6379> sadd myset "kuangshen"
(integer) 1
127.0.0.1:6379> sadd myset2 "set2"
(integer) 1
127.0.0.1:6379> smove myset myset2 "kuangshen" # Move a specified value to another setClose! (integer)1
127.0.0.1:6379> SMEMBERS myset
1) "world"
2) "hello"
127.0.0.1:6379> SMEMBERS myset2
1) "kuangshen"
2) "set2"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #Micro-blog, B station, common concern! (Union) Number set class: - difference set SDIFF - Intersection - Union127.0.0.1:6379> SDIFF key1 key2 # difference set
1) "b"
2) "a"
127.0.0.1:6379> SINTER key1 key2 # Crossing mutual friends can be done this way
1) "c"
127.0.0.1:6379> SUNION key1 key2 # and set
1) "b"
2) "c"
3) "e"
4) "a"
5) "d"
Copy the code

ZSet(Ordered set)

  • On the basis of set, a value is added
127.0.0.1:6379 >zadd myset 1 one # add a value
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three # add multiple values
(integer) 2
127.0.0.1:6379> ZRANGE myset 0 -1
1) "one"
2) "two"
3) "three"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #How to implement sorting127.0.0.1:6379> zadd salary 2500 xiaohong Add three users
(integer) 1
127.0.0.1:6379> zadd salary 5000 zhangsan
(integer) 1
127.0.0.1:6379> zadd salary 500 kaungshen
(integer) 1
# ZRANGEBYSCORE key min max
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf # display all users from small to large!
1) "kaungshen"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> ZREVRANGE salary 0 -1 # Sort from largest to largest!
1) "zhangsan"
2) "kaungshen"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores Display all users and attach
绩 1)
"kaungshen"
2) "500"
3) "xiaohong"
4) "2500"
5) "zhangsan"
6) "5000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 2500 withscores # display promotions for employees earning less than 2500Sort order!1) "kaungshen"
2) "500"
3) "xiaohong"
4) "2500"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Remove elements from REM
127.0.0.1:6379> zrange salary 0 -1
1) "kaungshen"
2) "xiaohong"
3) "zhangsan"
127.0.0.1:6379> zrem salary xiaohong Removes the specified element from the ordered collection
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "kaungshen"
2) "zhangsan"
127.0.0.1:6379> zcard salary Get the number in the ordered set
(integer) 2
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
127.0.0.1:6379> zadd myset 1 hello
(integer) 1
127.0.0.1:6379> zadd myset 2 world 3 kuangshen
(integer) 2
127.0.0.1:6379> zcount myset 1 3 Get the number of members in the specified range!
(integer) 3
127.0.0.1:6379> zcount myset 1 2
(integer) 2
Copy the code

💠 Next post

  • In the next installment, we’ll take a look at the Redis configuration file, and then we’ll look at Springboot integration as well as customizing RedisTemplate and utility classes.

reference

  • Silicon Valley Redis6 video
  • Crazy god says Redis video