Redis data types and operations

Source: Internet
Author: User
Redis data types and operations. redis strings can contain any data, such as JPG images or serialized objects. From the perspective of internal implementation, string can be viewed as B

Redis data types and operations. redis strings can contain any data, such as jpg images or serialized objects. From the perspective of internal implementation, string can be viewed as B

Strings type

The string type is binary secure.

Redis strings can contain any data, such as jpg images or serialized objects. From the perspective of internal implementation, the string can be regarded as a byte array, and the maximum value is 1 GB.
Struct sdshdr {
Long len; // The length of the buf
Long free; // number of available bytes of the buf
Char buf []; // actual string content
};


1: set

Set the value corresponding to the key (string type)

> Set name larry

OK

2: setnx

That is, set not exist. If the key already exists, 0 is returned.

> Set name larry

OK

> Setnx name larry

(Integer) 0

3: setex
Store key-value pairs and set the validity period

> Setex name 10 larry
OK

> Get name

"Larry"

10 seconds later...

> Get name

(Nil)

4: setrange

If yes, replace it with \ x00. If no, replace the missing character with \ x00. A number indicates the subscript to be replaced.
> Get name
"Larry"
> Setrange name 0 lv

(Integer) 5
> Get name
"Lvrry"

> Setrange name 10 lv

"Lvrry \ x00 \ x00 \ x00 \ x00 \ x00lv"

5: mset

Set multiple key values each time

> Mset key1 value1 key2 value2

OK

6: msetnx

If multiple key values are set at a time, they are atomic and both succeed or fail. If the operation fails, 0 is returned, and all operations are rolled back.

7: get

Returns a value based on the key if no value exists (nil)

> Get asdasds

(Nil)

8: getset

Get the old value and set a new value. If no value exists, return (nil)

> Get name

"Larry"

> Getset name lv

"Larry"

> Get name

"Lv"

> Getset dsadasd lv

(Nil)

9: getrange

Obtain the value based on the specified subscript. A negative number indicates that the value starts from the right.

> Getrange 0 1 name

"La"

10: mget

Multiple values are returned, and nil is returned if no value exists)

> Get name sdadasd

1) "larry"

2) (nil)

11: incr

Add operation. If the character is not int type, an error is returned (error) ERR value is not an integer or out of range

> Set age 20
OK
Incr age
(Integer) 21

12: incrby

Add operation

> Get age
"21"
> Incrby age 5
(Integer) 26

13: decr

Subtraction operation

14: decrby

Subtraction

15: append

Append operation, returns the string length

> Append name lv

(Integer) 15

> Get name

"Larrylv"

16: strlen

Returns the length of a value.

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.