Operations for Redis String types

Source: Internet
Author: User

Set key value [ex seconds]/[px milliseconds]  [NX]/[xx]

such as: Set a 1 ex 10, 10 seconds valid

Set a 1 PX 9000, 9 sec active

Note: If EX,PX is written at the same time, the following validity period shall prevail

If set a 1 ex 9000 px, the actual validity is 9000 milliseconds

NX: Indicates that the key does not exist when the operation is performed

XX: Indicates that the key exists, the operation is performed

Mset  Multi Set, setting multiple key values at once

Example: Mset key1 v1 key2 v2 ....

Get key

Function: Gets the value of key

Mget Key1 Key2. Keyn

Function: Gets the value of multiple keys

SetRange Key Offset value

Function: Change the offset byte of the string to value

Redis 127.0.0.1:6379> Set Greet Hello

Ok

Redis 127.0.0.1:6379> SetRange greet 2 x

(integer) 5

Redis 127.0.0.1:6379> Get Greet

"Hexlo"

Note: If offset > character length, the character automatically complements 0x00

Redis 127.0.0.1:6379> SetRange greet 6!

(integer) 7

Redis 127.0.0.1:6379> Get Greet

"Heyyo\x00!"

Append key value

Function: Append value to the original value of key

GetRange Key Start stop

Function: Gets the value of the [Start, stop] range in the string

Note: For the subscript of a string, the left number starts at 0 and the right number starts at-1

Redis 127.0.0.1:6379> set title ' Chinese '

Ok

Redis 127.0.0.1:6379> GetRange Title 0 3

"Chin"

Redis 127.0.0.1:6379> getrange Title 1-2

"Hines"

Attention:

1:start>=length, the empty string is returned

2:stop>=length, it is truncated to the end of the character

3: Returns an empty string if start is located on the right side of stop

Getset Key NewValue

Function: Gets and returns the old value, sets the new value

Redis 127.0.0.1:6379> Set CNT 0

Ok

Redis 127.0.0.1:6379> Getset CNT 1

"0"

Redis 127.0.0.1:6379> Getset CNT 2

"1"

INCR key

Function: The value of the specified key is added 1, and the value after the plus 1 is returned

Attention:

1: Non-existent key as 0, then incr operation

2: The range is 64 signed

Incrby Key number

Redis 127.0.0.1:6379> Incrby Age 90

(integer) 92

Incrbyfloat Key Floatnumber

Redis 127.0.0.1:6379> Incrbyfloat Age 3.5

"95.5"

DECR key

Redis 127.0.0.1:6379> Set age 20

Ok

Redis 127.0.0.1:6379> DECR Age

(integer) 19

Decrby Key number

Redis 127.0.0.1:6379> Decrby Age 3

(integer) 16

Getbit Key Offset

Function: Gets the binary representation of the value, the value on the corresponding bit (from left, numbering from 0)

Redis 127.0.0.1:6379> Set Char A

Ok

Redis 127.0.0.1:6379> getbit Char 1

(integer) 1

Redis 127.0.0.1:6379> Getbit Char 2

(integer) 0

Redis 127.0.0.1:6379> Getbit Char 7

(integer) 1

Setbit  Key Offset value

Set the value on offset corresponding to bits

Return: The old value on this bit

Attention:

1: If offset is too large, 0 is filled in the middle,

2:offset Maximum to what size

3:offset maximum 2^32-1, can be launched the largest string of 512M

Bitop Operation Destkey Key1 [Key2 ...]

To Key1,key2. Keyn as operation, and save the results to Destkey.

Operation can be and, or, not, XOR

Redis 127.0.0.1:6379> setbit Lower 7 0

(integer) 0

Redis 127.0.0.1:6379> setbit Lower 2 1

(integer) 0

Redis 127.0.0.1:6379> Get Lower

" "

Redis 127.0.0.1:6379> Set Char Q

Ok

Redis 127.0.0.1:6379> Get Char

"Q" L

Redis 127.0.0.1:6379> bitop or char char lower

(integer) 1

Redis 127.0.0.1:6379> Get Char

"Q"

Note: For not operations, key cannot be multiple

Operations for Redis String types

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.