Beckham _ redis string type learning, _ redisstring
RedisString type
1. View string-type commands
Ii. string operation example
1. View string-type commands
1. Open the redis client and enter help
127.0.0.1: 6379> help
Redis-cli2.8.19
Type: "help @ <group>" to get a list of commands in <group>
"Help <command>" for helpon <command>
"Help <tab>" to get a listof possible help topics
"Quit" to exit
Note: help is a help command,
2. Enter help @ string
127.0.0.1: 6379> help @ string
APPEND key value
Summary: Append a value to a key
Since: 2.0.0
BITCOUNT key [start] [end]
Summary: Count set bits in a string
Since: 2.6.0
BITOP operation destkey key [key...]
Summary: Perform bitwise operations betweenstrings
Since: 2.6.0
........
Note: because the length is too large, it is omitted.
Ii. string operation example
1. Description of string commands
Command name |
Description |
Format |
Set |
Associate the string value with the key. If the key already holds another value, SET overwrites the old value, regardless of the type. |
Set key value |
Get |
Returns the string value associated with the key. If the key does not exist, a special value nil is returned. |
Get key |
Setnx |
Set the key value to value if and only if the key does not exist. If the given key already exists, SETNX does not take any action |
Setnx key value |
Setex |
Associate the value with the key and set the survival time of the key to seconds (in seconds ). |
Setex key seconds value |
Setrange |
Overwrite the string value stored by the given key using the value parameter, starting from offset. The key that does not exist is treated as a blank string. |
Setrange key offset value |
Mset |
Set one or more key-value pairs at the same time. If a key with the same name exists, MSET overwrites the old value with the new value. |
Mset key value [key value...] |
Mget |
Returns the string value associated with multiple keys. If the key does not exist, a special value nil is returned. |
Mget key1 key2... |
Append |
If the key already exists and is a string, the append Command appends the value to the original value of the key. If the key does not exist, append simply sets the given key as value, just like executing set key value. |
Append key value |
Msetnx |
Set one or more key-value pairs if and only if the key does not exist. Even if only one key already exists, msetnx rejects all key settings. |
Msetnx key value [key value...] |
Getrange |
Returns the substring of the string value in the key. The Truncation range of the string is determined by the offset of start and end (including start and end ). The offset of a negative number indicates that the string is counted from the end,-1 indicates the last character,-2 indicates the second to the end, and so on. |
Getrange key start end |
Getset |
Set the value of the given key to value and return the old value of the key. If the key exists but is not of the string type, an error is returned. |
Getset key value |
Strlen |
Returns the length of the string value stored in the key. If the key is not stored as a string value, an error is returned. |
Strlen key |
Decr |
Reduce the number value stored in the key by one. If the key does not exist, use 0 as the initial value of the key and then perform the decr operation. |
Decr key |
Decrby |
Subtract the decrement value of the key. If the key does not exist, use 0 as the initial value of the key and then perform the decrby operation. |
Decrby key decrement |
Incr |
Add one of the number values stored in the key. If the key does not exist, use 0 as the initial value of the key and then perform the incr operation. |
Incr key |
Incrby |
Add incremental increment to the value stored in the key. If the key does not exist, use 0 as the initial value of the key and then perform the incrby operation. |
Incrby key increment |
|
|
|
2. single string operation
2.1 set the key to name and its value to xiaobei.
127.0.0.1: 6379> setname xiaobei
OK
2.2. Obtain the value of key as name.
127.0.0.1: 6379> get name
"Xiaobei"
2.3. Get the length of the character whose key is name
127.0.0.1: 6379> strlen name
(Integer) 7
2.4. The key is name, And the append character is 'is good .'
127.0.0.1: 6379> append name is good'
(Integer) 15
127.0.0.1: 6379> get name
"Xiaobeiis good"
2.5 Replace the key with good in name with great
127.0.0.1: 6379> setrange name 11 great
(Integer) 16
127.0.0.1: 6379> get name
"Xiaobeiis great"
3. Set multiple characters in batches
3.1 set key to name and age respectively, and their values correspond to xiaobei, 24
127.0.0.1: 6379> mset name xiaobei age 24
OK
3.2. Obtain the value of key name and age.
127.0.0.1: 6379> mget name age
1) "xiaobei"
2) "24"
4. Auto-increment or auto-increment operations
4.1 auto-increment operation
127.0.0.1: 6379> incr c
(Integer) 1
127.0.0.1: 6379> get c
"1"
127.0.0.1: 6379> incr c
(Integer) 2
127.0.0.1: 6379> get c
"2"
4.2. Auto-subtraction operation
127.0.0.1: 6379> decr c
(Integer) 1
127.0.0.1: 6379> get c
"1"
127.0.0.1: 6379> decr c
(Integer) 0
127.0.0.1: 6379> get c
"0"
4.3. Auto-increment by 10
127.0.0.1: 6379> incrby c 10
(Integer) 10
127.0.0.1: 6379> get c
"10"
127.0.0.1: 6379> incrby c 10
(Integer) 20
127.0.0.1: 6379> get c
"20"
4.4. perform auto-reduction based on 10
127.0.0.1: 6379> decrby c 10
(Integer) 10
127.0.0.1: 6379> get c
"10"
127.0.0.1: 6379> decrby c 10
(Integer) 0
127.0.0.1: 6379> get c
"0"
5. Common Errors
5.1 auto-increment or auto-increment can only be used for keys of numeric strings or keys that do not exist
127.0.0.1: 6379> set name '1'
OK
127.0.0.1: 6379> get name
"1"
127.0.0.1: 6379> incr name
(Integer) 2
127.0.0.1: 6379> set name 'xiaobei'
OK
127.0.0.1: 6379> get name
"Xiaobei"
127.0.0.1: 6379> incr name
(Error) ERR value is not an integer or out of range
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.