Common Commands (String)

“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

string

Set – Sets key/value pairs

Set a pair of key values

Set key value [NX] [XX] [EX

] [PX [millseconds]]

This parameter is mandatory

  • Set: command
  • Key: indicates the key to be set
  • Value: specifies the value of the key

Optional Parameter Description

  • NX: The value is set only when the key does not exist. If the key does exist, NULL is returned
  • XX: The value is set only when the key exists. If the key does not exist, NULL is returned
  • EX seconds: Specifies the expiration time, which is accurate in seconds
  • PX millSecond: Set the expiration time to be exact in milliseconds

Set: command

127.0. 01.:6379> set user1 '{"id":1,"username":"ljw01","password":"123456","sex":1}'
OK
127.0. 01.:6379> get user1
"{"id": 1,"username":"ljw01","password":"123456","sex": 1}"
Copy the code

NX: The value is set only when the key does not exist. If the key does exist, NULL is returned

127.0. 01.:6379> set user1 '{"id":1,"username":"ljw01","password":"123456","sex":1}'NX (nil) failed to set because user1 already exists, returns nilCopy the code

XX: The value is set only when the key exists. If the key does not exist, NULL is returned

127.0. 01.:6379> set user1 '{"id":2,"username":"ljw01","password":"123456","sex":1}' XX
OK
127.0. 01.:6379> get user1
"{"id": 2,"username":"ljw01","password":"123456","sex": 1}"
Copy the code

EX seconds: specifies the expiration time. The expiration time is accurate in seconds. Use TTL to view the remaining expiration time

127.0. 01.:6379> set user1 '{"id":2,"username":"ljw01","password":"123456","sex":1}' EX 600
OK
Copy the code

PX millSecond: Set the expiration time to be exact in milliseconds

127.0. 01.:6379> set user1 '{"id":2,"username":"ljw01","password":"123456","sex":1}' PX 600000
OK
Copy the code

Setnx – Set if it exists

Syntax: setnx key value

All parameters are mandatory. SET a pair of key values. If keys exist, the setting fails, which is the same as SET key value NX

Setex – Sets expiration second key value pairs

Syntax: setex key expire value

All parameters are mandatory. SET a pair of key values and the expiration time (unit: second), which is the same as SET key value EX expire

Psetex – Sets the expiration millisecond key value pair

Psetex key expire value

All parameters are mandatory. SET a pair of key values and expire time, in milliseconds, the same as SET key value PX expire

Mset – Batch set value

Function: Batch set value

Mset key1 value1 [key2 value2 key3 value3…

All parameters are mandatory. There must be at least one key and value pair. This command is used to set multiple key-value pairs.

Mget – Batch value

Function: Batch value

Mget key1 [key2 key3…]

All parameters are mandatory. The key value must be at least one. The value of multiple keys is obtained

127.0. 01.:6379> mset k1 v1 k2 v2 k3 v3
OK
127.0. 01.:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
Copy the code

Getset – Check first and change later

Function: First check the value of key and then modify the new value

Syntax: getSet key value

All parameters are mandatory. Obtain the value of the specified key and set the value of the key to the new value value

127.0. 01.:6379> getset user1 ljw1
"{"id": 2,"username":"ljw01","password":"123456","sex": 1}"
127.0. 01.:6379> get user1
"ljw1"
Copy the code

Setrange – Modifies the value of the specified offset

Function: indicates a key and changes the value of offset to value

Syntax: setrange key offset value

All parameters are mandatory. Set key, offset to value, and offset to the length of value. Offset cannot be less than 0

127.0. 01.:6379> set user 123456
OK
127.0. 01.:6379> setrange user 2 ljw
(integer) 6
127.0. 01.:6379> get user
"12ljw"
Copy the code

Append – string concatenation

Function: string concatenation

Syntax: append key STR

127.0. 01.:6379> set user 123
OK
127.0. 01.:6379> append user abc
(integer) 6
127.0.0.1:6379> get user
"123abc"
Copy the code

Getrange – Intercepts a string

Action: Intercepts a string

Syntax: getrange key start end

All parameters are mandatory. Obtain the value of the range specified by the specified key. Start and end can be negative values

127.0. 01.:6379> get user
"123abc"
127.0. 01.:6379> getrange user 0 3
"123a"
127.0. 01.:6379> getrange user 1 3
"23a"
127.0. 01.:6379>
Copy the code

Substr – String interception

Function: String interception

Syntax: substr key arg arg arg

127.0. 01.:6379> get user
"123abc"
127.0. 01.:6379> substr user 0 3
"123a"
127.0. 01.:6379> substr user 1 3
"23a"
127.0. 01.:6379>
Copy the code

The integer

Incr – plus 1

Function: counter

Syntax: incr key

All parameters are mandatory. Add 1 to key. The value corresponding to key must be an integer. Otherwise, an error message is returned. After the operation succeeds, the value after the operation is returned

127.0. 01.:6379> incr product01
(integer) 1
127.0.0.1:6379> get product01
"1"
127.0.0.1:6379> incr product01
(integer)2 127.0.0.1:6379 > incrproduct01
(integer) 3
127.0.0.1:6379> get product01
"3"
Copy the code

Decr – minus 1

Syntax: decr key

All parameters are mandatory. Specify key to subtract 1. The value corresponding to key must be an integer. Otherwise, an error message is returned. After the operation succeeds, the value after the operation is returned. Is the inverse operation of DECR.

127.0. 01.:6379> get product01
"3"
127.0. 01.:6379> decr product01
(integer)2 127.0.0.1:6379 > decrproduct01
(integer) 1
127.0.0.1:6379> get product01
"1"
Copy the code

Incrby – addition

Function: addition

Syntax: incrby key data

All parameters are mandatory. When you specify key to add data, the value of key and data must be integers. Otherwise, an error message is returned

127.0. 01.:6379> set product01 100
OK
127.0. 01.:6379> get product01
"100"
127.0. 01.:6379> incrby product01 20
(integer) 120
127.0. 01.:6379> get product01
"120"
Copy the code

Decrby – subtraction

Action: subtraction

Syntax: decrby key data

All parameters are mandatory. When the key is specified to subtract data, the value and data corresponding to the key must be integers. Otherwise, an error message is returned

127.0. 01.:6379> get product01
"120"
127.0. 01.:6379> decrby product01 30
(integer) 90
127.0. 01.:6379> get product01
"90"
Copy the code

Floating point Numbers

Incrbyfloat – addition

Function: Adds a floating point number to an existing key

Syntax: incrByFloat KEY NUM

127.0. 01.:6379> set money 10.5
OK
127.0. 01.:6379> incrbyfloat money 2.2
"12.7"
Copy the code

Redis distributed cache family

  • Redis Distributed Cache (1) – Redis Installation (Linux and Docker)
  • Redis Distributed cache (ii) – RDB and AOF
  • SpringBoot integrates Mybatis-Plus,Redis and Swagger
  • Redis distributed cache (4) – SpringCache integrates redis
  • The article continues to be updated…