This article is shared with “Parsing common String Commands of Redis” in Huawei cloud community by Grey Xiaoape.

Can I ask you a question first? Do you know what Java data types are? Int, byte, string, etc.

Java data types include basic data types and reference data types. Basic data types include byte, short, int, long, double, float, char, Boolean, and reference data types are three other types, respectively class, interface, and array. Note that there is no string, this is an interview trap!

There are data types in Redis as well. Unlike Java, there are five data types in Redis: String, List, Set, Hash, and Zset. For each of these five data types, there are many different usage scenarios. What are the commands for manipulating strings in Redis?

String command operation

String is the most commonly used data type in Redis. It is also the data type that many programmers use most or only use in their daily development. As a result, many people only know to use String when using Redis and ignore the importance of other data types. So I also hope that after learning this article, you have a new understanding of Redis data types, do not only use the String type for Redis operations.

Note: before operating on the Redis client, make sure that the Redis server interface is open. Otherwise, the client will refuse access or cannot open it.Copy the code

Sets the value of the specified key

In Redis, the method of setting a key for string data is the most basic method of setting key-value.

The syntax is as follows: SET key value Key indicates the index. Value indicates the corresponding valueCopy the code

For example, we want to set the key-value pair data with index k3 and value v3.

127.0.0.1:6379> SET k3 v3
OK
Copy the code

If ok is returned, the setting is successful!

Gets the value of the specified key

If we have set the data in Redis, we can get it by the key of the data, and the syntax is as follows:

GET Key The key is the index of the value to be obtainedCopy the code

For example, if we get an index of k3 with a value of v3, it will return the value if the index exists, or nil if it doesn’t.

127.0.0.1:6379> GET k3
"v3"
127.0.0.1:6379> GET k4
(nil)
Copy the code

Returns a substring of the string value in key

This command is used to retrieve substrings that already exist in Redis, and we can specify a subscript index that specifies which character segment to retrieve. The command format is as follows:

GETRANGE key start end Key is the index of the string. Start is the start subscript of interception. End is the end subscript of interceptionCopy the code

For example, if we intercept the substring of data whose index is “mykey” and value is “Huixiaoyuan”,

127.0.0.1:6379> GETRANGE mykey 2 5
"ixia"
127.0.0.1:6379> GETRANGE mykey 2 -1
"ixiaoyuan"
127.0.0.1:6379> GETRANGE mykey 1 100
"uixiaoyuan"
Copy the code

To illustrate the above code, since the string starts with a subscript of 0, the second corresponding character is “I”. If you want to truncate the string from one character to the last substring, you can simply set the last parameter to -1.

If you set the range of the substring to larger than the original length of the string, only the last character of the string will be returned!

Gets the values of multiple given keys

Different from the GET command, the GET command can only obtain the value of one key. The GET command can be used to obtain the value of multiple keys in the following format:

MGET key1 [key2...] Multiple indexes can be appended to MGET, separated by SpacesCopy the code

If we get the values of mykey, mykey1, and mykey2,

127.0.0.1:6379> MGET mykey mykey1 mykey2
1) "huixiaoyuan"
2) "value1"
3) "value2"
Copy the code

Returns the length of the string corresponding to the key

This command is used to obtain the length of the string corresponding to the current index in the following format:

STRLEN Key The key is the index corresponding to the string 127.0.0.1:6379> STRLEN Mykey (integer) 11Copy the code

Sets one or more key-value pairs

We know that the SET command is used to SET a single key-value pair, but what if there are multiple key-value pairs SET simultaneously? At this point, you can use this command in the following format:

MSET key1 value1 [key2 value2 ...] Key1 Value1 is the first key/value pair to be set. Subsequent key/value pairs are separated by SpacesCopy the code

Here we set up three key-value pairs simultaneously

127.0.0.1:6379> MSET k1 v1 k2 v2 k3 v3
OK
Copy the code

This command can be set only if the key does not exist, otherwise all key-values will fail to be assigned.

MSETNX key1 value1 [key2 value2 ...] Key1 Value1 is the first key/value pair to be set. Subsequent key/value pairs are separated by SpacesCopy the code

If we set a value for k4 that already exists, then 0 is returned, indicating that the setting failed, and 1 is returned if the setting succeeds.

127.0.0.1:6379> MSETNX k1 v1 k4 v4
(integer) 0
127.0.0.1:6379> MSETNX k4 v4 k5 v5
(integer) 1
Copy the code

Increments the value stored in the key by one

This command increments the value of the stored characters by one and returns the sum, but if the key is not a number, an error message is returned in the following format:

INCR Key Key indicates the index of the number to be added by 1Copy the code

As we add 1 to just set the k8, then complains and resetting a numeric key/value pairs, then add 1, you can return to the corresponding data.

127.0.0.1:6379> INCR k8
(error) ERR value is not an integer or out of range
127.0.0.1:6379> SET num1 10
OK
127.0.0.1:6379> INCR num1
(integer) 11
Copy the code

The INCRBY command can add the specified increment to the data corresponding to the specified key in the following format:

INCRBY key Increment Key is the index increment value 127.0.0.1:6379> INCRBY num1 5 (integer) 16Copy the code

Subtract the value stored in the key by one

Since there are increased operations, there are corresponding decreased operations in the following format:

DECR Key Key indicates the index of the number to be added by 1Copy the code

In the previous step we added num1 by 5 to get 16. Now we subtract it by 1.

127.0.0.1:6379> DECR num1
(integer) 15
Copy the code

Corresponding to this command is the command to subtract the specified data in the following format:

DECRBY key Increment Key Decrement is a value to increase 127.0.0.1:6379> DECRBY NUM1 2 (INTEGER) 13Copy the code

String append

If the key already exists and is a string, the APPEND command appends the specified value to the end of the key’s original value. Returns the appended length in the following format:

APPEND Key Value Key indicates the index. Value indicates the string to be appendedCopy the code

If we append “hello” to mykey1-value1

127.0.0.1:6379> APPEND mykey1 hello
(integer) 11
127.0.0.1:6379> GET mykey1
"value1hello"
Copy the code

These are the common command operations for manipulating string characters in Redis. The second of Redis’s five data types, Hash, is a common command operation.

Hash type

A Hash is a mapping of String fields and values. It can be used to store data objects that we define. Therefore, it is simply a key.

Let’s take a look at some of the commands that Redis uses to Hash data types.

Set a Hash data

The command used to SET Hash data is not SET, but HMSET. H stands for Hash and M stands for Map. The format of this command is as follows:

HMSET key fieId1 value1 [fieId2 value2…]

  • Key is the unique index corresponding to the Hash data
  • Field is the key of a key-value pair stored below
  • Value indicates the value of the key

For example, if the key is set to “myhash”, the field stored in it is name-huixiaoyuan, sex-nan and age-3

127.0.0.1:6379> HMSET myhash name huixiaoyuan sex nan age 3
OK
Copy the code

Gets all the fields and values in the specified hash table

The HGETALL command to view all the fields and values in the specified hash table is HGETALL, which extracts all the data in the hash table in the following format

HGETALL key

  • Key is the index corresponding to the Hash data

Let’s look at the hash data we just set

127.0.0.1:6379> HGETALL myhash
1) "name"
2) "huixiaoyuan"
3) "sex"
4) "nan"
5) "age"
6) "3"
Copy the code

Gets the value of the specified field stored in the hash table

Select * from hash table; select * from hash table; select * from hash table;

HGET key field

  • Key Indicates the index of the hash table
  • Field Specifies the field corresponding to the value obtained

If we get the value of name in the hash table above

127.0.0.1:6379> HGET myhash name
"huixiaoyuan"
Copy the code

Deletes one or more hash table fields

The command to delete one or more hash table fields is HDEL. This command can delete the specified hash table fields and their corresponding values in the following format:

HDEL key field1 [field2…].

  • Key is the index of the specified hash table
  • Field is the field corresponding to the value to be deleted. If you want to delete multiple fields, separate them with Spaces

For example, if we want to delete the hash table whose index is “myHash”, the value is “3”, the corresponding field is “age”, and the value is “nan”, the corresponding field is “sex”.

127.0.0.1:6379> HGET myhash name
"huixiaoyuan"
127.0.0.1:6379> HDEL myhash age sex
(integer) 2
127.0.0.1:6379> HGETALL myhash
1) "name"
2) "huixiaoyuan"
Copy the code

Gets the number of fields in the hash table

To obtain the number of fields in a specified hash table, run the following command:

HLEN key

  • Key is the index of the specified hash table
127.0.0.1:6379> HLEN myhash
(integer) 1
Copy the code

Gets all fields in the hash table

This command is used to obtain all the fields in the specified hash table, but does not return the corresponding values of the fields. The format is as follows:

HKEYS key

  • The key for The index of the specified hash table
127.0.0.1:6379> HKEYS myhash
1) "name"
Copy the code

Gets all the values in the hash table

Select * from hash table; select * from hash table; select * from hash table;

HVALS key

  • The key for The index of the specified hash table
127.0.0.1:6379> HVALS myhash 1)"Copy the code

That’s how you use some of the most common commands for hash data in Redis.

Click to follow, the first time to learn about Huawei cloud fresh technology ~