Redis Data type: Strings

Source: Internet
Author: User
Tags bulk insert

String is the simplest data type, a key corresponding to a value,string type is binary safe, Redis string can contain any data,

For example, JPG images or serialized objects.

Set Method:

Sets the value of the key corresponding to string type value, for example, we add a

The key-value pair for the name=xiaoming.

[Email protected] bin]# redis-cli-h 127.0.0.1-p 6379-a lh801309
127.0.0.1:6379> Set Name Xiaoming
Ok
127.0.0.1:6379> Get Name
"Xiaoming"
127.0.0.1:6379>

If the same key in the set is overwritten, what if you do not want to overwrite it?

Can be used with the Setnx method, if no addition exists, there is a return of 0

127.0.0.1:6379> setnx name Xiaohong
(integer) 0

Setex method: Specifies the validity period of the set key:

127.0.0.1:6379> Setex age 10 10
Ok
127.0.0.1:6379> Get Age
"10"
127.0.0.1:6379> Get Age
(nil)

SetRange method: Replacing substrings

127.0.0.1:6379> set mail [email protected]
Ok
127.0.0.1:6379> SETRANGE Mail 4 @gmail. com
(integer) 14
127.0.0.1:6379> Get Mail
"[Email protected] @gmail. com"
127.0.0.1:6379>

Mset Method: Bulk Insert

127.0.0.1:6379> MSET key1 1 key2 2 Key3 3
Ok
127.0.0.1:6379> Get Key1
"1"
127.0.0.1:6379> Get Key2
"2"
127.0.0.1:6379> Get Key3
"3"

Msetnx set the value of multiple keys at once, successful return OK means all values are set,

Failure to return 0 means that no value is set, but does not overwrite the existing key

127.0.0.1:6379> msetnx Key4 4 key5 5 Key3 0
(integer) 0
127.0.0.1:6379> Get Key4
(nil)

As long as one does not succeed, it returns 0 because Key3 already exists.

Getset method: Get the old value and set the new value

127.0.0.1:6379> Get Key1
"1"
127.0.0.1:6379> Getset Key1 100
"1"
127.0.0.1:6379> Get Key1
"100"

GetRange method: Gets the specified substring

127.0.0.1:6379> Get Mail
"[Email protected] @gmail. com"
127.0.0.1:6379> GETRANGE Mail 0 6
"[Email protected] @gm"

Mget method: Bulk Fetch

127.0.0.1:6379> mget key1 Key2 Key3
1) "100"
2) "2"
3) "3"

Incr method: Perform a Gaga operation on the value of key and return the new value

127.0.0.1:6379> Get Key1
"100"
127.0.0.1:6379> INCR Key1
(integer) 101
127.0.0.1:6379> INCR Key1
(integer) 102

Incrby method: With INCR, add the specified value, key does not exist when you set key back,

and thinks the original value is 0.

127.0.0.1:6379> Get Key1
"102"
127.0.0.1:6379> Incrby Key1 5
(integer) 107
Setting a positive number is plus, setting a negative value is minus

Decr method: The value of key is reduced and reduced.

Decrby method: Similar to DECR, minus the specified value

Append method: Append value to the specified key

127.0.0.1:6379> Get Mail
"[Email protected] @gmail. com"
127.0.0.1:6379> Append mail. Net
(integer) 18
127.0.0.1:6379> Get Mail
"[Email protected] @gmail. Com.net"
127.0.0.1:6379>

Strlen gets the value of the specified key

127.0.0.1:6379> Get Mail
"[Email protected] @gmail. Com.net"
127.0.0.1:6379> STRLEN Mail
(integer) 18

Redis Data type: Strings

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.