Get the App
SLTechnology News&Howtos  ›  Database  › 

String type and operation of Redis

Shulou Source: shulou.com Published: 2022-06-01 06:02:08 09月29日 Update

set: Set the value corresponding to key to string type value.

127.0.0.1:6379> set name stone

OK

127.0.0.1:6379> get name

"stone"

127.0.0.1:6379> set name stone1

OK

127.0.0.1:6379> get name

"stone1"

setnx: Set the value corresponding to key to string type value, if the key already exists, return 0, nx means not exist.

127.0.0.1:6379> get name

"stone1"

127.0.0.1:6379> setnx name stone

(integer) 0

127.0.0.1:6379> get name

"stone1"

setex: Set the value corresponding to key to string type value, and specify the validity period corresponding to this key value.

127.0.0.1:6379> setex haircolor 10 red

OK

127.0.0.1:6379> get haircolor

"red"

127.0.0.1:6379> get haircolor

(nil)

setrang: A substring that sets the value of the specified key.

127.0.0.1:6379> set email stone@163.com

OK

127.0.0.1:6379> setrange email 6 qq.com

(integer) 13

127.0.0.1:6379> get email

"stone@qq.comm"

mset: Set multiple key values at once, success returns ok means all values are set, failure returns 0 means none is set.

127.0.0.1:6379> mset key1 stone1 key2 stone2

OK

127.0.0.1:6379> get key1

"stone1"

127.0.0.1:6379> get key2

"stone2"

msetnx: Set multiple key values at once, success returns ok means all values are set, failure returns 0 means no values are set, but does not overwrite existing keys.

127.0.0.1:6379> msetnx key1 s1 key2 s2 key3 ston3

(integer) 0

127.0.0.1:6379> get key1

"stone1"

127.0.0.1:6379> get key2

"stone2"

127.0.0.1:6379> get key3

(nil)

127.0.0.1:6379> msetnx key3 stone3 key4 stone4

(integer) 1

127.0.0.1:6379> get key3

"stone3"

127.0.0.1:6379> get key4

"stone4"

get: Get the string value corresponding to the key, if the key does not exist, return nil.

getset: Sets the value of key and returns the old value of key.

127.0.0.1:6379> get key4

"stone4"

127.0.0.1:6379> getset key4 stone44

"stone4"

127.0.0.1:6379> get key4

"stone44"

getrange: Gets the substring of the value of key.

127.0.0.1:6379> get key4

"stone44"

127.0.0.1:6379> getrange key4 0 1

"st"

127.0.0.1:6379> getrange key4 0 0

"s"

mget: Get multiple key values at once. If the corresponding key does not exist, nil will be returned.

127.0.0.1:6379> mget key1 key2 key3 key4 key5

1) "stone1"

2) "stone2"

3) "stone3"

4) "stone44"

5) (nil)

incr: Adds the value of key and returns the new value.

incrby: similar to incr, add the specified value, key will be set when it does not exist, and think that the original value is 0.

127.0.0.1:6379> set num1 10

OK

127.0.0.1:6379> incr num1

(integer) 11

127.0.0.1:6379> get num1

"11"

127.0.0.1:6379> get num2

(nil)

127.0.0.1:6379> incrby num2 2

(integer) 2

127.0.0.1:6379> incrby num2 2

(integer) 4

127.0.0.1:6379> get num2

"4"

Decr: Subtract or subtract the value of key.

Decrby: Similar to decr, minus the specified value.

127.0.0.1:6379> get num1

"11"

127.0.0.1:6379> decr num1

(integer) 10

127.0.0.1:6379> get num1

"10"

127.0.0.1:6379> get num2

"4"

127.0.0.1:6379> decrby num2 2

(integer) 2

127.0.0.1:6379> get num2

"2"

append: appends value to the string of the specified key and returns the length of the new string value.

127.0.0.1:6379> get name

"stone1"

127.0.0.1:6379> append name @qq.com

(integer) 13

127.0.0.1:6379> get name

"stone1@qq.com"

strlen: Takes the length of the value of the specified key.

127.0.0.1:6379> strlen name

(integer) 13

Tags: Character string type multiple success length validity meaning time validity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB Shulou Information Linux Xiaomi