Redis beginners: 5 (basic operations of the String type)

String type

The String type is the most basic type in Redis. The String type has the following characteristics:

  • The String type is binary safe, which means that the String type can contain any data, such as pictures, serialized objects, etc.;
  • The maximum size of a String type value in Redis is 512M

Common commands

Append string to value

append key 追加的内容

As shown in the figure below:

Insert picture description here


11 above refers to the length of value after the append operation is performed

Get the length of value

strlen key

As shown below:

Insert picture description here



Set the value of the key when the key does not exist

setnx key value

As shown in the figure below:
Set up successfully,

Insert picture description here


set up failed

Insert picture description here

String type numbers for addition and subtraction operations

Add string type numbers

1. Increase by one

incr key

As shown below:

Insert picture description here


return the value after adding one

2. Increment the specified value

incrby key 增加的值

As shown in the figure below:

Insert picture description here


PS: The increment of incr is an atomic operation, and it will not be interrupted by thread scheduling. The
so-called atomic operation is different from the atomicity in the transaction. The atomic operation here means that it will not be scheduled by the thread. Operation interrupted by the mechanism .
As we said earlier, Redis is a single-threaded + multiple IO multiplexing mechanism, so a single instruction of Redis is a single-threaded atomic operation.

Subtract string type numbers

1. Decrease one

decr key

As shown in the figure below:

Insert picture description here


2. Decrease the corresponding value

decrby key 减少的值

As shown below:

Insert picture description here



Operate multiple keys and values ​​at the same time

Set multiple key and value values ​​at the same time

1.mset can set multiple key values ​​at the same time

mset key1 value1 key2 value2 ...

As shown below:

Insert picture description here


2.msetnx is similar to setnx, but it can set multiple key values ​​at the same time, and only when these keys do not exist

msetnx key1 value1 key2 value2...

As shown in the figure below:

Insert picture description here


Note: Because of the atomicity in Redis , if a key value fails to be set when using msetnx, the other key values ​​will also fail to be set , as shown in the figure below:

Insert picture description here


The key 11 already exists, so use msetnx to set it. The key value failed, and the previous key44 and key55 also failed to be set, so the result of the check is empty

Obtain the value of multiple keys at the same time

mget key1 key2 key3 

Since there is mset, then mget cannot be less, mget can obtain the value of multiple keys at the same time

As shown in the figure below:

Insert picture description here


Get the values ​​stored in multiple keys at the same time.

Operate on values ​​within a certain range

1.getrange gets the value within a certain range

getrange <key> <起始位置> <结束位置>

As shown in the figure below:

Insert picture description here


we can find from the figure above that the operation of getrange is similar to the substring operation of Java, and it is also used to obtain the string in the specified range

2.setrange overwrites a string within a certain range

setrange <key> <起始位置> <value>

As shown below:

Insert picture description here



3.getset replaces the old value with the new value and gets the old value

getset <key> <value>

As shown below:

Insert picture description here



Set the expiration time while setting the key

setex can set the expiration time of the key while setting the key

setex <key> <过期时间> <value>

As shown below:

Insert picture description here