Summary of 16 commands commonly used in Redis string type data

Source: Internet
Author: User
Tags redis

Summary of 16 commands commonly used in Redis string type data


Description: String type is the simplest type, and a key corresponding to a value,string type is binary safe.         A Redis string can contain any data, such as a JPG picture or a serialized object. Common methods:
1, set Method:
Value for set <key> <value> set key
(1) Use set for nonexistent key:

Redis 127.0.0.1:6379> set name ' Zhangsan '
OK
redis 127.0.0.1:6379> get name
' Zhangsan '
(2) Use set for existing key and replace key value:
Redis 127.0.0.1:6379> set name ' Lisi '
OK
redis 127.0.0.1:6379> get name
' Lisi '
2,setnx Method:

Setnx <key> <value> When key does not exist, set key to value, set success, return 1, set failed, return 0.

Redis 127.0.0.1:6379> setnx name ' Wangwu '
(integer) 0
redis 127.0.0.1:6379> get name
"Lisi"
Redis 127.0.0.1:6379> setnx name2 ' Wangwu '
(integer) 1
redis 127.0.0.1:6379> get name2
"Wangwu"
3,setex Method:
Setex <key> <seconds> <value> Set key values to value and expire in seconds (in seconds). If the key already exists,
The value is overwritten. After expiration, the return is nil.
Redis 127.0.0.1:6379> Setex name 3 ' Zhangsan '
OK
redis 127.0.0.1:6379> get name
' Zhangsan '
Redis 127.0.0.1:6379> get name
(nil)
Redis 127.0.0.1:6379> setex name ' Zhangsan '
OK
Redis 127.0.0.1:6379> Get name
"Zhangsan"
redis 127.0.0.1:6379> setex name ' Lisi '
OK
Redis 127.0.0.1:6379> Get name
"Lisi"
4,setrange Method:
SetRange <key> <offset> <value> uses the value parameter to overwrite the string value stored by the given key, starting at 0 with the offset starting at offset. If key does not exist, it is treated as a blank string. This command ensures that the string is long enough to set the value at the specified offset. If the given key originally stores a smaller string length than the offset (for example, if the string is only 5 characters long, but the offset you set is 10), then the white space between the original character and the offset will be 0 bytes ( Zerobytes, "\x00") to fill. Note that the maximum offset you can use is 2^29-1 (536870911) because the size of the Redis string is limited to 512 megabytes (megabytes). If you need to use more space than this, you can use more than one key.
Redis 127.0.0.1:6379> set name ' Zhangsan '
OK
redis 127.0.0.1:6379> get name
' Zhangsan '
Redis 127.0.0.1:6379> setrange name 5 ' _si '
(integer) 8
redis 127.0.0.1:6379> get name
"Zhang_si"
Redis 127.0.0.1:6379> get Name3
(nil) Redis 127.0.0.1:6379> setrange name3
4 ' Lisi '
(integer) 8
Redis 127.0.0.1:6379> Get name3
"\x00\x00\x00\x00lisi"
Redis 127.0.0.1:6379>

5,mset Method:
Mset <key> <value> [<key> <value> ...] Sets multiple Key-value pairs at the same time, overwriting previous values if the key already exists.
Redis 127.0.0.1:6379> Get name
"Zhang_si"
redis 127.0.0.1:6379> mset name ' Zhangsan ' name2 ' Lisi ' Name3 ' WA Ngwu '
OK
redis 127.0.0.1:6379> get name
"Zhangsan" Redis
127.0.0.1:6379> get name2
"Lisi"
Redis 127.0.0.1:6379> Get name3
"Wangwu"
Redis 127.0.0.1:6379>

6,msetnx Method:
Msetnx <key> <value> [<key> <value> ...] Similar to the Mset method, which sets multiple key-value pairs if and only if the given key does not exist. If the operation succeeds, it returns 1, and the failure returns 0.
Redis 127.0.0.1:6379> set age
OK
redis 127.0.0.1:6379> get Age ""
Redis 127.0.0.1:6379> Msetnx age1 age2
(integer) 0
redis 127.0.0.1:6379> get age1
(nil)
Redis 127.0.0.1:6379> ; Get age
""
redis 127.0.0.1:6379> msetnx key1 ' hello ' key2 ' word '
(integer) 1
redis 127.0.0.1:6379> get key1
"Hello"
redis 127.0.0.1:6379> get key2
"word"
Redis 127.0.0.1:6379>

7,append Method:
Append <key> <value> Appends value to the original key value and, if the key does not exist, creates a Key-value pair by default. The operation returned 1 successfully, and the failure returned 0.
Redis 127.0.0.1:6379> set name ' Zhangshan '
OK
redis 127.0.0.1:6379> get name
' Zhangshan '
Redis 127.0.0.1:6379> Append name ' is '.
(integer)
redis 127.0.0.1:6379> get name
"Zhangshan."
Redis 127.0.0.1:6379> Get addr
(nil) Redis 127.0.0.1:6379>-Append
' env. '
(integer) 7
redis 127.0.0.1:6379> get addr
"env."
Redis 127.0.0.1:6379>

8, Get Method:
Get <key> Remove Key's value.
9, Mget Method:
Mget <key> [<key> ...] takes out the value of multiple keys. If a key does not exist in a given key, the key returns a special value of nil.
Redis 127.0.0.1:6379> mget name name1  name2 name3
1) "Zhangshan is."
2) (nil)
3) "Lisi"
4) "Wangwu"
Redis 127.0.0.1:6379>

Ten, GetRange method:
GetRange <key> <start> <end> Gets the substring of the key corresponding value, from the string (including start and end) between the offset start and the last, the index of the leftmost character of value is 0 and so on , the index of the last character is minus one for the total length of the string, which in turn is similar. This is similar to slicing operations in Python. For example, the string "Zhangsan" corresponds to the subscript in the following figure, as shown in subscript two in the following figure.

Subscript One: 0 1 2 3 4 5 6 7
Z H A N G S A N
Subscript Two: 0 -7 -6 -5 -4 -3 -2 -1
Redis 127.0.0.1:6379> set name ' Zhangsan '
OK
redis 127.0.0.1:6379> getrange name 0 4
"Zhang"
Redis 127.0.0.1:6379> getrange name 0
"Zhangsan" "Redis 127.0.0.1:6379> getrange
name 0-1
" Zhangsan "
Redis 127.0.0.1:6379>

11,getset Method:
Getset <key> <value> Sets the value of the key and returns the old value of the key, returning nil when the key's old value does not exist.
Redis 127.0.0.1:6379> Get name
"Zhangsan"
redis 127.0.0.1:6379> getset name ' Lisi '
"Zhangsan"
redis 127.0.0.1:6379> Get name
"Lisi"
redis 127.0.0.1:6379> Get code
(nil)
Redis 127.0.0.1:6379> getset code ' hello '
(nil)
Redis 127.0.0.1:6379> get code
"Hello"
redis 127.0.0.1:6379>

The Strlen method:
Strlen <key> Returns the length of the key and returns 0 if the value of the key does not exist.
Redis 127.0.0.1:6379> Get name
"Lisi"
redis 127.0.0.1:6379> strlen  name
(integer) 4
Redis 127.0.0.1:6379> get Key0
(nil)
Redis 127.0.0.1:6379> strlen key0
(integer) 0

13,incr Method:
INCR <key> adds the value of key to 1. If the value of key does not exist, the default setting key is 0, called once, incremented once, and an error is returned if the key's value is not an integer type.
Redis 127.0.0.1:6379> Get name
"Lisi"
redis 127.0.0.1:6379> incr name
(Error) ERR value are not a Integ Er or out of range
Redis 127.0.0.1:6379> set name 5
OK
redis 127.0.0.1:6379> incr name
(integer ) 6
redis 127.0.0.1:6379> get name
"6"
redis 127.0.0.1:6379> get Key0
(nil)
Redis 127.0.0.1:6379> incr key0
(integer) 1
redis 127.0.0.1:6379> incr key0
(integer) 2
Redis 127.0.0.1:6379> get Key0
"2"
Redis 127.0.0.1:6379>

Decr method:
DECR <key> similar to the INCR method, the value of the key is reduced by 1. If the value of key does not exist, the default setting key is 0, called once, minus once, if the value of the key is not an integer type, an error is returned.
Redis 127.0.0.1:6379> Get name
"6"
redis 127.0.0.1:6379> decr name
(integer) 5
Redis 127.0.0.1:6379> decr name
(integer) 4
redis 127.0.0.1:6379> decr key9
(integer)-1
Redis 127.0.0.1:6379>

15,incrby Method:
Incrby<key> <increment> similar to the DECR method, add the value of the key to the increment increment. Similarly, the wrong type still returns an error. If the key value does not exist, the default is 0 after the operation.
Redis 127.0.0.1:6379> Set Count
ok
redis 127.0.0.1:6379> get Count
"
Redis" 127.0.0.1:6379> Incrby Count
(integer)
redis 127.0.0.1:6379> Get Count "a
"
Redis 127.0.0.1:6379> get Count1
(nil)
Redis 127.0.0.1:6379> incrby count1
(integer)
Redis 127.0.0.1:6379> get count1
"30"
16,decrby Method:
Decrby <key> <decrement> Similar to the DECR method, subtracts the value of the key from the increment decrement. Similarly, the wrong type still returns an error. If the key value does not exist, the default is 0 after the operation.
Redis 127.0.0.1:6379> Set Count
OK
redis 127.0.0.1:6379> Decrby count 4
(integer)
Redis 127.0.0.1:6379> get Count2
(nil)
Redis 127.0.0.1:6379> decrby count2
(integer) -10
Redis 127.0.0.1:6379>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.