Abstract: Share some frequently used commands and usage scenarios summary, and how to use CMD command line to operate the five data types in Redis.
This article is shared from Huawei cloud community “Redis Operation five data types common commands Parsing”, author: Grey Xiaoape.
Several common Redis commands
Database switching
We know that Redis has 16 databases by default, and the default is the 0th database, so if we need to switch the database, we can use the following command:
Run the following command to switch
SELECT index
Copy the code
Clear data in the current database
If you want to clear data for a specified database
flushdb
Copy the code
Clear all database data
flushall
Copy the code
View all keys in the database
This next command is probably the most commonly used
keys *
Copy the code
Determine if a key exists
In normal development, we also need to constantly check the key to see if it exists
exists key
Copy the code
Set the expiration time and view the remaining time
Because the cache data we set is not always permanent, we need to set the expiration time when we store the data.
127.0.0.1:6379> expire test01 10
(integer) 1
127.0.0.1:6379> ttl test01
(integer) -2
Copy the code
View the current key type
type keyname
Copy the code
String data type
The string type is one of the five basic data types in Redis, which is also the most commonly used data type. All a lot of friends’ understanding and operation of Redis only stay in the operation level of Redis, but whether you know the relevant command in the string type, or there are very many practical
Basic String access operations
Let’s take a look at the basic string storage and retrieval commands.
127.0.0.1:6379> set key1 v1 # set key and value OK 127.0.0.1:6379> get key1 # set key to value "v1" 127.0.0.1:6379> keys * 1) "key1" 2) "mykey" 3) "hxy" 4) "site-list" 5) "hxy2" 6) "huixiaoyuan" 127.0.0.1:6379> exists key1 1 127.0.0.1:6379> AppEnd key1 Hello # Append (integer) 7 127.0.0.1:6379> strlen key1 to the value of the current key # Obtain the length of value (integer) 7Copy the code
Increment and decrement operation
If the contents of a string are numbers, we can also add or subtract them. Redis automatically adds and subtracts strings. The command is as follows:
127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> decr views
(integer) 1
127.0.0.1:6379> incrby views 10
(integer) 9
127.0.0.1:6379> decrby view 9
(integer) -9
127.0.0.1:6379> get views
"9"
127.0.0.1:6379> decrby views 9
(integer) 0
127.0.0.1:6379>
Copy the code
Setex and SETNx
-
Setex sets the expiration time
-
Setnx does not exist and cannot be set if it exists. Often used for distributed locks
Set key3 to hello, 127.0.0.1:6379> setex key3 30 Hello OK 127.0.0.1:6379> TTL key3 (integer) 21 127.0.0.1:6379> get key3 “hello” 127.0.0.1:6379> setnx mykey2 redis (integer) 1 127.0.0.1:6379> setnx mykey2 hxy (integer) 0 127.0.0.1:6379> get mykey2 “Redis 127.0.0.1” : 6379 >
Mset and MGET for batch Settings
With msetnx, one or more key-value pairs are set simultaneously if and only if none of the given keys exist.
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx k1 v1 k4 v4
(integer) 0
127.0.0.1:6379> get k4
(nil)
127.0.0.1:6379>
Copy the code
The getset command is used
Getset getset getset getset getset getset
127.0.0.1:6379> getSet k5 v5 (nil) 127.0.0.1:6379> get k5 "v5" if there is a value, 127.0.0.1:6379> getSet k5 VV5 "V5" 127.0.0.1:6379> get k5 "VV5"Copy the code
To summarize the similar usage scenarios of string:
-
counter
-
Count the number of multiple units
-
Object cache storage
-
Number of fans
List data type
List Access basic operations
There are two commands that need to be distinguished when using the list type for access:
-
Lpush: Inserts elements from the left
-
Rpush: Inserts elements from the right
127.0.0.1:6379> lpush list1 v1 (integer) 1 127.0.0.1:6379> lpush list1 v2 (integer) 2 127.0.0.1:6379> lpush list1 v3 (integer) 3 127.0.0.1:6379> lrange list1 0 1
- “v3”
- “v2”
127.0.0.1:6379> rpush list1 v4 (integer) 4 127.0.0.1:6379> lrange list1 0 4
- “v3”
- “v2”
- “v1”
- “v4”
The list type removes elements
-
Lpop is removed from the left
-
Rpop is removed from the right
-
Lrange keyName 0-1 gets all elements in the list
127.0.0.1:6379> LPOP list1 “v3” 127.0.0.1:6379> RPOP list1 “V4” 127.0.0.1:6379> lrange list1 0-1
- “v2”
- “v1”
127.0.0.1:6379 >
Note: only pop and push can be divided into lists
Conclusion:
-
The list in Redis is actually a linked list. Before node after, left, and right can all be inserted
-
If the key does not exist, a new list is created
-
If the key exists, add content
-
If you remove all the values, an empty list also doesn’t exist
-
It is most efficient to insert or change values on both sides, while middle elements are relatively inefficient
The set collection
Set sets access basic operations
127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> sadd myset hello2
(integer) 1
127.0.0.1:6379> sadd myset hello3
(integer) 1
127.0.0.1:6379> smembers myset
1) "hello"
2) "hello3"
3) "hello2"
127.0.0.1:6379> sismember myset hello
(integer) 1
127.0.0.1:6379> sismember myset world
(integer) 0
127.0.0.1:6379>
Copy the code
Gets the number of elements in a set
127.0.0.1:6379> scard myset
(integer) 3
Copy the code
Removes the specified element from the set collection
127.0.0.1:6379> srem myset hello
(integer) 1
127.0.0.1:6379> scard myset
(integer) 2
Copy the code
Selects a specified number of elements at random
127.0.0.1:6379> srandMember mySet "hello3" 127.0.0.1:6379> srandMember mySet "hello3"Copy the code
Delete an element at random
127.0.0.1:6379 > spop myset "hello2"Copy the code
SDIFF difference set SUNION of SINTER intersection
SDIFF keyname1 keyname2
Copy the code
Summary Set sets are generally used in scenarios where elements are not repeated, such as lottery systems, rotations, etc
Hash
Hash accesses basic operations
When using a hash set, note that a hash is actually a Map set, and key-map values are stored as a Map set, similar to Java hashmap.
#set a specific key-value 127.0.0.1:6379> hset myhash fieid1 v1 (integer) 1 127.0.0.1:6379> hget myhash fieid1 "v1" #set multiple key-values 127.0.0.1:6379> hmset myhash fieid1 v1 fieid2 v2 OK #get multiple values 127.0.0.1:6379> hmget myhash fieid1 fieid2 1) "v1" 2) "v2"Copy the code
HVALS get all values, HKEYS get all keys, and HGETALL get all keys
HKEYS myhash 1) "fieID1" 2) "fieid2" 127.0.0.1:6379> HKEYS myhash 1) "fieid1" 2) "fieid2" HGETALL myhash 1) "fieid1" 2) "v1" 3) "fieid2" 4) "v2"Copy the code
Conclusion:
Hash can be used to store changed data, such as user, name, age, etc., especially user information. Hash is more suitable for storing objects, and string is more suitable for storing strings.
Zset (Ordered set)
Add a sequence number to the set set for sorting
Zset ordered collection access basic operation
127.0.0.1:6379> ZADD Salary 2500 xiaohong (integer) 1 127.0.0.1:6379> ZADD Salary 6000 dahui (integer) 1 127.0.0.1:6379> ZADD Salary 1000 wanggang (INTEGER) 1 # Specifies that the output range is negative infinity to positive infinity, in order from smallest to largest, 127.0.0.1:6379> ZRANGEBYSCORE salary - INF + INF 1) "wanggang" 2) "xiaohong" 3) "dahui" # sort from large to small 127.0.0.1:6379> ZREVRANGE salary 0 -1 1) "dahui" 2) "xiaohong" 3) "wanggang"Copy the code
ZRANGEBYSCORE uses syntax
ZRANGEBYSCORE key min max
Copy the code
All data is displayed, with keys and values displayed simultaneously
127.0.0.1:6379> ZRANGEBYSCORE salary - INF + INF withscores 1) "wanggang" 2) "1000" 3) "xiaohong" 4) "2500" 5) "dahui" 6) "6000"Copy the code
Gets the number of elements in the collection
127.0.0.1:6379> ZCARD salary
(integer) 3
Copy the code
conclusion
-
Set sort, store class grades, salary table sort
-
Common message 1, important message 2, with weights to judge
-
Implementation of leaderboard application
These are some common command operations when accessing the five data types. For other commands, you can view them directly on the official website.
Click to follow, the first time to learn about Huawei cloud fresh technology ~