Redis's String ultra-detailed API usage and application scenarios introduction

  • j3_liuliang
  • Redis commonly used API is the start series of application scenarios (String). If you find it useful, you can follow the blogger and update it from time to time!

1. String

1.1 SETNX(setnx)

Only set the value of the key when the key does not exist.

SETNX redis ( the SET IF N OT E X- ISTs) when a given command key does not exist, setting the specified value key.

语法

127.0.0.1:6379> SETNX KEY_NAME VALUE

可以版本:>= 1.0.0

返回值:The setting is successful, and 1 is returned. The setting fails, and 0 is returned.

案例

127.0.0.1:6379> flushall					#清空数据库
OK
127.0.0.1:6379> exists j3-liuliang 18		#查看key是否存在
(integer) 0
127.0.0.1:6379> setnx j3-liuliang 18		#设置key并赋值
(integer) 1
127.0.0.1:6379> setnx j3-liuliang 28		#尝试覆盖key
(integer) 0									#覆盖失败
127.0.0.1:6379> get j3-liuliang				#key没有变化
"18"
127.0.0.1:6379> 

1.2 GETRANGE (getrange)

Returns the sub-characters of the string in key

The Redis Getrange command is used to get the substring of the string stored in the specified key. The interception range of the string is determined by the two offsets of start and end (including start and end).

语法

127.0.0.1:6379> GETRANGE KEY_NAME start end

可以版本:>= 2.4.0

返回值:Intercept the obtained substring.

案例

 127.0.0.1:6379> flushall								#清空数据库
OK
127.0.0.1:6379> set j3-liuliang "This is my test key"	#设置key并赋值
OK
127.0.0.1:6379> getrange j3-liuliang 0 3				#截取key部分内容
"This"
127.0.0.1:6379> getrange j3-liuliang 6 9
"s my"
127.0.0.1:6379> getrange j3-liuliang 0 -1				#截取key得所有内容
"This is my test key"
127.0.0.1:6379> 

1.3 MSET (mset)

Set one or more key-value pairs at the same time.

The Redis Mset command is used to set one or more key-value pairs at the same time.

语法

127.0.0.1:6379> MSET key1 value1 key2 value2 .. keyN valueN 

可以版本:>= 1.0.1

返回值:Always returns OK.

案例

127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3	#同时设置多个key value对
OK
127.0.0.1:6379> get k1					#获取值
"v1"
127.0.0.1:6379> get k2
"v2"
127.0.0.1:6379> get k3
"v3"
127.0.0.1:6379> 

1.4 SETEX(setex)

Associate the value value to the key, and set the expiration time of the key to seconds (in seconds).

The Redis Setex command sets the value and expiration time for the specified key. If the key already exists, the SETEX command will replace the old value.

语法

127.0.0.1:6379> SETEX KEY_NAME TIMEOUT VALUE

可以版本:>= 2.0.0

返回值:Return OK when the setting is successful.

案例

127.0.0.1:6379> flushall					#清空数据库
OK
127.0.0.1:6379> set j3-liuliang 18			#设置key并赋值
OK
127.0.0.1:6379> get j3-liuliang				#获取可以得值
"18"
127.0.0.1:6379> setex j3-liuliang 60 28		#设置key如果key存在就覆盖,且设置过期时间60秒,新值28
OK
127.0.0.1:6379> ttl j3-liuliang				#查看剩余时间
(integer) 53
127.0.0.1:6379> get j3-liuliang				#获取可以得值
"28"
127.0.0.1:6379> 

1.5 SET(set)

Set the value of the specified key

The Redis SET command is used to set the value of a given key. If the key already stores other values, SET overwrites the old value, regardless of the type.

语法

127.0.0.1:6379> SET KEY_NAME VALUE

可以版本:>= 1.0.0

返回值:

Before Redis 2.6.12, the SET command always returned OK.

Starting from Redis 2.6.12, SET only returns OK when the setting operation is successfully completed.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set j3-liuliang 18		#设置key并赋值
OK
127.0.0.1:6379> get j3-liuliang			#获取key得值
"18"
127.0.0.1:6379> set j3-liuliang 28		#设置key并赋值,key存在则覆盖旧值
OK
127.0.0.1:6379> get j3-liuliang			#获取值
"28"	#被覆盖了
127.0.0.1:6379> 

1.6 GET(get)

The Redis Get command is used to get the value of the specified key. If the key does not exist, return nil. If the value stored in key is not a string type, an error is returned.

语法

127.0.0.1:6379> GET KEY_NAME

可以版本:>= 1.0.0

返回值:Returns the value of the key, if the key does not exist, returns nil. If the key is not a string type, then an error is returned.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set j3-liuliang 18		#设置key并赋值
OK
127.0.0.1:6379> get j3-liuliang			#获取key
"18"
127.0.0.1:6379> del j3-liuliang			#删除key
(integer) 1
127.0.0.1:6379> lpush names j3-liuliang zhangsan	#设置list类型key
(integer) 2
127.0.0.1:6379> get names	#尝试获取list类型key
(error) WRONGTYPE Operation against a key holding the wrong kind of value
#“对持有错误类型值的键执行错误类型操作”,可以简单理解为就是命令和要执行得key类型不匹配
127.0.0.1:6379> 

1.7 GETBIT(getbit)

Obtain the bit at the specified offset for the string value stored in key.

The Redis Getbit command is used to obtain the bit at the specified offset from the string value stored in the key.

语法

127.0.0.1:6379> GETBIT KEY_NAME OFFSET

可以版本:>= 2.2.0

返回值:

The string value specifies the bit (bit, 0 or 1) at the offset.

When the offset OFFSET is greater than the length of the string value, or the key does not exist, 0 is returned.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> setbit bitkey 1 1	#给bitkey的key在 1 位上设置值 1(默认为 0 )
(integer) 0
127.0.0.1:6379> setbit bitkey 2 1	#给bitkey的key在 2 位上设置值 1(默认为 0 )
(integer) 0
127.0.0.1:6379> setbit bitkey 3 1	#给bitkey的key在 3 位上设置值 1(默认为 0 )
(integer) 0
127.0.0.1:6379> getbit bitkey 2		#获取bitkey在 2 位上的值(默认为 0 )
(integer) 1
127.0.0.1:6379> getbit bitkey 3		#获取bitkey在 3 位上的值(默认为 0 )
(integer) 1
127.0.0.1:6379> getbit bitkey 4		#获取bitkey在 4 位上的值(默认为 0 )
(integer) 0
127.0.0.1:6379> 

1.8 SETBIT(setbit)

For the string value stored in the key, set or clear the bit at the specified offset.

The Redis Setbit command is used to set or clear the bit at the specified offset for the string value stored in the key.

语法

127.0.0.1:6379> Setbit KEY_NAME OFFSET

可以版本:>= 2.2.0

返回值:Specifies the original stored bit of the offset.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> setbit bitkey 1 1	#给bitkey的key在 1 位上设置值 1(默认为 0 )
(integer) 0
127.0.0.1:6379> setbit bitkey 2 1	#给bitkey的key在 2 位上设置值 1(默认为 0 )
(integer) 0
127.0.0.1:6379> setbit bitkey 3 1	#给bitkey的key在 3 位上设置值 1(默认为 0 )
(integer) 0
127.0.0.1:6379> getbit bitkey 2		#获取bitkey在 2 位上的值(默认为 0 )
(integer) 1
127.0.0.1:6379> getbit bitkey 3		#获取bitkey在 3 位上的值(默认为 0 )
(integer) 1
127.0.0.1:6379> getbit bitkey 4		#获取bitkey在 4 位上的值(默认为 0 )
(integer) 0
127.0.0.1:6379> 

1.9 DECR(decr)

Decrease the numeric value stored in the key by one.

The Redis Decr command reduces the value stored in the key by 数字值one.

If the key does not exist, the value of the key will be initialized to 0 first, and then the DECR operation will be executed.

If the value contains the wrong type, or the value of the string type cannot be represented as a number, then an error is returned.

This operation is limited to a value 64 位(bit)有符号数字within the represented.

语法

127.0.0.1:6379> DECR KEY_NAME 

可以版本:>= 1.0.0

返回值:The value of key after executing the command.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set money 100		#对存在数字值得key进行 decr
OK
127.0.0.1:6379> decr money			#decr操作
(integer) 99
127.0.0.1:6379> decr money
(integer) 98
127.0.0.1:6379> exists math			#对不存在得key进行decr
(integer) 0
127.0.0.1:6379> decr math
(integer) -1
127.0.0.1:6379> set name j3-liuliang	#对不是数字值得key进行decr
OK
127.0.0.1:6379> decr name
(error) ERR value is not an integer or out of range	#出错
127.0.0.1:6379> 

1.10 DECRBY(decrby)

The value stored in key minus the given decrement value (decrement).

The Redis Decrby command subtracts the specified decrement value from the value stored in the key.

If the key does not exist, the value of the key will be initialized to 0 first, and then the DECRBY operation will be executed.

If the value contains the wrong type, or the value of the string type cannot be represented as a number, then an error is returned.

The value of this operation is limited to 64-bit (bit) signed number representation.

语法

127.0.0.1:6379> DECRBY KEY_NAME DECREMENT_AMOUNT

可以版本:>= 1.0.0

返回值:After subtracting the specified decrement value, the value of key.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set money 100		#对存在数字值得key进行 decr
OK
127.0.0.1:6379> decrby money 10			#decr操作
(integer) 90
127.0.0.1:6379> decrby money 10
(integer) 80
127.0.0.1:6379> exists math			#对不存在得key进行decr
(integer) 0
127.0.0.1:6379> decrby math 10
(integer) -10
127.0.0.1:6379> set name j3-liuliang	#对不是数字值得key进行decr
OK
127.0.0.1:6379> decrby name 10
(error) ERR value is not an integer or out of range	#出错
127.0.0.1:6379>

1.11 STRLEN(strlen)

Returns the length of the string value stored in key.

The Redis Strlen command is used to obtain the length of the string value stored in the specified key. When the key is not a string value, an error is returned.

语法

127.0.0.1:6379> STRLEN KEY_NAME

可以版本:>= 2.2.0

返回值:The length of the string value. When the key does not exist, 0 is returned.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set name j3_liuliang	#设置key并赋值
OK
127.0.0.1:6379> strlen name				#获取存在key的value长度
(integer) 11
127.0.0.1:6379> strlen notkey			#获取不存在key的value传长度
(integer) 0
127.0.0.1:6379> 

1.12 MSETNX(msetnx)

Set one or more key-value pairs at the same time, if and only if all the given keys do not exist. ( 原子操作,全部成功才成功)

The Redis Msetnx command is used to set one or more key-value pairs at the same time when all the given keys do not exist.

语法

127.0.0.1:6379> MSETNX key1 value1 key2 value2 .. keyN valueN 

可以版本:>= 1.0.1

返回值:When all keys are successfully set, 1 is returned. If all the given keys fail to set (at least one key already exists), then return 0.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> msetnx k1 v1 k2 v2 k3 v3	#一次性设置多个不存在的key value
(integer) 1
127.0.0.1:6379> keys *
1) "k1"
2) "k3"
3) "k2"
127.0.0.1:6379> msetnx k1 v1 m1 n1 m2 n2	#当设置得key中有存在的key则全部设置失败(表明是原子操作)
(integer) 0
127.0.0.1:6379> 

1.13 INCR (incr)

Increase the numeric value stored in the key by one

The Redis Incr command increases the numeric value stored in the key by one.

If the key does not exist, the value of the key will be initialized to 0 first, and then the INCR operation will be executed.

If the value contains the wrong type, or the value of the string type cannot be represented as a number, then an error is returned.

The value of this operation is limited to 64-bit (bit) signed number representation.

语法

127.0.0.1:6379> INCR KEY_NAME 

可以版本:>= 1.0.0

返回值:The value of key after executing the INCR command.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set money 100				#设置key并设置数字值
OK
127.0.0.1:6379> incr money					#加一
(integer) 101
127.0.0.1:6379> incr money
(integer) 102
127.0.0.1:6379> get money					#获取值
"102"
127.0.0.1:6379> exists math					#判断key是否存在
(integer) 0
127.0.0.1:6379> incr math					#给不存在的key加一,先创建然后加一
(integer) 1
127.0.0.1:6379> get math					#获取值
"1"
127.0.0.1:6379> set name j3_liuliang		#创建不是数字值得key
OK
127.0.0.1:6379> incr name					#加一操作失败
(error) ERR value is not an integer or out of range
127.0.0.1:6379> 

1.14 INCRBY(incrby)

Add the value stored in the key to the given increment (increment).

The Redis Incrby command adds the number stored in the key to the specified increment value.

If the key does not exist, the value of the key will be initialized to 0 first, and then the INCRBY command will be executed.

If the value contains the wrong type, or the value of the string type cannot be represented as a number, then an error is returned.

The value of this operation is limited to 64-bit (bit) signed number representation.

语法

127.0.0.1:6379> INCRBY KEY_NAME INCR_AMOUNT

可以版本:>= 1.0.0

返回值:After adding the specified increment value, the value of key.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set money 100		#设置key并设置数字值
OK
127.0.0.1:6379> incrby money 20		#给key值增加 20
(integer) 120
127.0.0.1:6379> incrby money 20
(integer) 140
127.0.0.1:6379> get money			#获取key得值
"140"
127.0.0.1:6379> exists day			#判断key是否存在
(integer) 0
127.0.0.1:6379> incrby day 10		#给不存在得key进行增加 10 操作,它会先创建对于得key然后加 10
(integer) 10
127.0.0.1:6379> get day				#获取key得值
"10"
127.0.0.1:6379> set name j3_liuliang	#创建不是数字值得key
OK
127.0.0.1:6379> incrby name 10			##加操作失败
(error) ERR value is not an integer or out of range
127.0.0.1:6379> 

1.15 INCRBYFLOAT(incrbyfloat)

Add the value stored in the key to the given floating point increment (increment).

The Redis Incrbyfloat command adds the specified floating point increment value to the value stored in the key.

If the key does not exist, INCRBYFLOAT will first set the value of the key to 0, and then perform the addition operation.

语法

127.0.0.1:6379> INCRBYFLOAT KEY_NAME INCR_AMOUNT

可以版本:>= 2.6.0

返回值:The value of key after executing the command.

案例

127.0.0.1:6379> flushall
OK
# 值和增量都不是指数符号
127.0.0.1:6379> set money 100.50
OK
127.0.0.1:6379> incrbyfloat money 0.3
"100.8"
# 值和增量都是指数符号
127.0.0.1:6379> set pai 314e-2
OK
127.0.0.1:6379> get pai					# 用 SET 设置的值可以是指数符号
"314e-2"
127.0.0.1:6379> incrbyfloat pai 0		 # 但执行 INCRBYFLOAT 之后格式会被改成非指数符号
"3.14"
127.0.0.1:6379> get pai
"3.14"
127.0.0.1:6379> incrbyfloat pai 0.1
"3.24"

# 可以对整数类型执行
127.0.0.1:6379> set money 100
OK
127.0.0.1:6379> incrbyfloat money 0.1
"100.1"
127.0.0.1:6379> set km 3.0
OK
127.0.0.1:6379> get km			# SET 设置的值小数部分可以是 0
"3.0"
127.0.0.1:6379> incrbyfloat km 1.000000000000000 # 但 INCRBYFLOAT 会将无用的 0 忽略掉,有需要的话,将浮点变为整数
"4"
127.0.0.1:6379> get km
"4"
127.0.0.1:6379> 

1.16 SETRANGE (setrange)

Use the value parameter to overwrite the string value stored in the given key, starting from the offset offset.

The Redis Setrange command overwrites the string value stored in the given key with the specified string. The overwritten position starts from the offset offset.

语法

127.0.0.1:6379> SETRANGE KEY_NAME OFFSET VALUE

可以版本:>= 2.2.0

返回值:The length of the modified string.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set mykey "the is a good key"	#设置key
OK
127.0.0.1:6379> setrange mykey 9 AAA			#从指定位置开始覆盖key得值
(integer) 17
127.0.0.1:6379> get mykey						#获取覆盖后得值
"the is a AAAd key"
127.0.0.1:6379> 

1.17 PSETEX(psetex)

This command is similar to the SETEX command, but it sets the lifetime of the key in milliseconds instead of seconds as in the SETEX command.

The Redis Psetex command sets the key lifetime in milliseconds.

语法

127.0.0.1:6379> PSETEX key1 EXPIRY_IN_MILLISECONDS value1 

可以版本:>= 2.6.0

返回值:Return OK when the setting is successful.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> psetex name 20000 j3_liuliang	#以毫秒单位设置key得过期时间
OK
127.0.0.1:6379> pttl name						#查看key得过期时间,毫秒值
(integer) 16577
127.0.0.1:6379> get name						#在未过期时间内获取key
"j3_liuliang"
127.0.0.1:6379> pttl name						#过期后
(integer) -2
127.0.0.1:6379> get name						#获取过期后的key值
(nil)
127.0.0.1:6379> 

1.18 APPEND(append)

If the key already exists and is a string, the APPEND command appends the value to the end of the original value of the key.

The Redis Append command is used to append a value to the specified key.

If the key already exists and is a string, the APPEND command appends the value to the end of the original value of the key.

If the key does not exist, APPEND simply sets the given key to value, just like executing SET key value.

语法

127.0.0.1:6379> APPEND KEY_NAME NEW_VALUE

可以版本:>= 2.0.0

返回值:The length of the string in key after appending the specified value.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> exists name					#查看key是否存在
(integer) 0
127.0.0.1:6379> append name j3_liuliang		#给不存在的key 追加字符串
(integer) 11
127.0.0.1:6379> get name					#获取key对应得值
"j3_liuliang"
127.0.0.1:6379> set j3_liuliang 18			#设置key并赋值
OK
127.0.0.1:6379> append j3_liuliang 0		#给存在得key追加值
(integer) 3
127.0.0.1:6379> get j3_liuliang				#查看追加后得key的值
"180"
127.0.0.1:6379> 

1.19 GETSET (getset)

Set the value of the given key to value and return the old value of the key.

The Redis Getset command is used to set the value of the specified key and return the old value of the key.

语法

127.0.0.1:6379> GETSET KEY_NAME VALUE

可以版本:>= 1.0.0

返回值:

Returns the old value of the given key. When the key has no old value, that is, when the key does not exist, nil is returned.

When the key exists but is not a string type, an error is returned.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> exists name					#判断key是否存在
(integer) 0
127.0.0.1:6379> getset name j3_liuliang		#对不存在的key进行getset
(nil) #先返回nil然后给name设置值j3_liuliang
127.0.0.1:6379> get name					#获取key的值
"j3_liuliang"
127.0.0.1:6379> set mykey name:j3_liulaing	#设置key并赋值
OK
127.0.0.1:6379> getset mykey name:liuliang	#对存在的key进行 getset
"name:j3_liulaing" #先是返回key的值,然后将新值覆盖给key
127.0.0.1:6379> get mykey					#获得覆盖后的值
"name:liuliang"
127.0.0.1:6379> 

1.20 MGET(mget)

Get all (one or more) values ​​of a given key.

The Redis Mget command returns all (one or more) values ​​of a given key. If a key does not exist in the given key, then this key returns the special value nil.

语法

127.0.0.1:6379> MGET KEY1 KEY2 .. KEYN

可以版本:>= 1.0.0

返回值:A list containing all the values ​​of a given key.

案例

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set k1 v1		#设置key并赋值
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> mget k1 k2 k3	#一次性获取多个key的值,不存在的则返回nil
1) "v1"
2) "v2"
3) (nil)
127.0.0.1:6379> 

Insert picture description here

Two, application scenarios

  • Single value cache : commodity inventory, key=product id, value=stock quantity(set,get)

  • Object cache :

    1. set stores user information, key=user:id value=json format data
    2. msetBulk storage of user information, suitable for application scenarios where data is constantly changing,
      such as users 微信余额, convenient access and high efficiency
  • Distributed lock :
    Applicable scenario: In a cluster environment, when multiple web applications are used to snap up the same product and reduce inventory, the
    distributed lock
    SETNXcommand (SET if Not eXists)
    syntax may be used when oversold is possible : SETNX key value
    function: If and only if the key does not exist, set the value of the key to value and return 1;
    if the given key already exists, SETNX does nothing and returns 0.

  • Calculator : Article 访问量, every time the user visits, the number of readings increases by 1(incr)

  • Shared session : For the sake of load balancing, distributed services will balance the access of user information to different servers, and users may need to log in again after refreshing their access. To avoid this problem, redis can be used to centrally manage user sessions. In this mode, as long as the high availability and scalability of redis are guaranteed, every time a user update or query login information is obtained, it is directly obtained from redis.

  • **Speed ​​limit: **For security reasons, users are asked to enter the mobile phone verification code every time they log in. In order to prevent frequent access to the SMS interface, the frequency of the user to obtain the verification code every minute will be limited.

  • Distributed system global serial number :

    1. Applicable scenario: The primary key of a general database table uses an auto-increment sequence number. If the system is under high pressure and the back-end has a sub-database and sub-table,
      the auto_increment that comes with the database is not applicable. You can use the auto-increment of redis because Redis is a single process Single-threaded mode, using queue mode to change concurrent access to serial access, and there is no competition between multiple clients to connect to Redis.
      Incr orderId redis pressure is high,
      incr orderId 1000 improves performance and reduces redis pressure

    Suppose there are 3 machines accessing redis, using the characteristics of redis: single-threaded
    incrby orderid 1000 //Each machine takes 1000, 01000, 10012000, 2001~3000 each time,
    and then slowly process the 1000 capacity.

Concluding remarks

  • This article is written in conjunction with the practice cases of Redis Chinese website and bloggers, and the List type will be written in the next issue
  • As the bloggers are not very knowledgeable, there will inevitably be mistakes. If you find a mistake or prejudice, please leave a message to point it out, and I will correct it.
  • If you think the article is not bad, your forwarding, sharing, liking, and commenting are your greatest encouragement.
  • Thank you for reading, welcome and thank you for your attention.

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40399646/article/details/108906116