REDIS Data Structures

Source: Internet
Author: User

Objective

The biggest difference between Redis and memcached is that, in addition to supporting data persistence, Redis supports more data types than just simple key-value structure data records. Redis also supports more server-side operations on these data types, which can also reduce network IO times and data volumes, which is also more convenient and efficient than memcached, just for this ah, want to spray the upper left corner group spray. The last 20 minutes to teach you to be a memcached great God

There are 5 main types of data supported by Redis: String, Hash, List, set, and Sorted set.

Here's a detailed demonstration of the data types supported by Redis for your understanding of memory.

Redis Data type String

The string type is the most common type in Redis, and value stores a maximum data volume of 512M, which can hold JSON data, image data, and so on.

Follow the Redis command to manipulate the code of string type data.

Redis Data Write command set, equivalent to data insertion

Redis 127.0.0.1:6379> Set name ZLH--return value: OK, the insertion was successful. Overrides replace the original value value if the current name exists.

Redis Data Read command get, get data

Redis 127.0.0.1:6379> Get Name--return value: "ZLH", returns null if the current key has no value

Redis data Append command append, append data

Redis 127.0.0.1:6379> Append name ' Is my friend '--return value: ' Zlh is my friend ' if the value of the current key is appended to the original string, if none is written.

Redis data Read and write Operation command Getset, obtaining the original value value and writing the new value value

1. Redis 127.0.0.1:6379>getset name ZLH--return value: "Zlh is my Friend", which returns the value of the original name, and also sets the new value ZLH for the value of name. The value of name at this time is true value ZLH

2, Redis 127.0.0.1:6379>get name--return value: ZLH, because the value above Getset to name is set to ZLH.

Redis data addition calculation command Incr,incrby, data addition operation, INCR for +1 built-in operation, Incrby for +n self-set n operation

1. Redis 127.0.0.1:6379>INCR Name---Return value: "The data is not integer or the data exceeds the 64-bit signed shape data range", because the original name value is "ZLH", so it cannot be converted to an integral type, so the report is abnormal.

2, Redis 127.0.0.1:6379>INCR age----Return value: 1, because there is no key and value of age, but the default age is a key value of 0 for the +1 operation.

3, Redis 127.0.0.1:6379>INCR Age---Return value: 2, because the previous line of code to the age assignment is 1, here incr command +1 operation, the return value is 2.

4, Redis 127.0.0.1:6379>incrby Age---Return value: 12, because the original age is 2, here +10 is 12.

Redis Data Subtraction Calculation command Decr,decrby, data subtraction operation, DECR-1 built-in operation, Decrby to subtract N to set n operation

1. Redis 127.0.0.1:6379>DECR Name-return value: "The data is not integer or the data exceeds the 64-bit signed shape data range", because the original name value is "ZLH", it cannot be converted to an integer type, so the report is abnormal.

2, Redis 127.0.0.1:6379>DECR age--Return from: 11, because the original age value of 12, here decr is self-minus 1 meaning, it is 11.

3, Redis 127.0.0.1:6379>decrby age 10--return value: 1, because the original age is 11, here-10, therefore 1.

Redis gets a string-length command strlen

Redis 127.0.0.1:6379>strlen Name--return value: 3, because the value of name is ZLH, the length is 3, and if the key or value does not exist, 0 is returned.

Redis set value value and set Expiration Time command Setex (in seconds)

Redis 127.0.0.1:6379>setex Sex Male---return value: OK, set the value of key to sex as male, and the cache expiration time is 20s.

Redis 127.0.0.1:6379>DDL Sex---Return value: The remaining expiration time, 0 is expired, and 1 is never expired.

Redis 127.0.0.1:6379>get Sex---Return value: male, stating that there is no period at this time when the returned data is null when it has expired.

Redis assignment to determine whether the original value exists, there is no assignment, return 0; not present to assign value, return 1; command setnx

Redis 127.0.0.1:6379>setnx name Tom---Return value: 0, because the original value of name is ZLH, the existence of values is not assigned.

Redis 127.0.0.1:6379>gete Name---return value: ZLH, because there is a value, so the above is assigned to Tom failure, return 0.

Redis 127.0.0.1:6379>setnx Phone 18501733702---Return value: 1, the assignment was successful because the key and value of the phone did not exist.

Redis 127.0.0.1:6379>get Phone---Return value: 18501733702, indicating that the above Setnx assignment was successful.

Redis string substitution assignment, starting at the specified position, command SetRange

Redis 127.0.0.1:6379>setrange Phone 9 123--return value: 12,12 is the length of the string and the 11-bit number becomes 12 bits. Since the replacement from the 9th bit, replaced with the last one of the original string has not been replaced, so the last side added a set of 3.

The Redis 127.0.0.1:6379>get Phone---Return value is: 185017337123.

Redis 127.0.0.1:6379>set Phone 1---Here for setrange other features, set the phone to 1.

Redis 127.0.0.1:6379>setrange Phone 3 AAA---Return value is: 6, because the original phone value is 1, less than three bits, replace 2 bits with 0*00, so to add 2 to be able to replace the third bit after the value of AAA.

Redis 127.0.0.1:6379>get Phone---return value: 1\*00\*00aaa. Understand why this is the return value here, knowing the action above.

Redis intercepts strings, from subscript N to N or n+1, similar to substring in C #, commands GetRange

Redis 127.0.0.1:6379>set Phone 18501733701----easy operation

Redis 127.0.0.1:6379>getrange Phone 1 5---Return value: 85017, because GetRange is truncated from subscript 1 to intercept to subscript 5, which contains a value of subscript 5.

Redis 127.0.0.1:6379>getrange Phone 0 0---Return value: 1, intercept from subscript 0, intercept to subscript 0, including subscript 0. Therefore, the return value is 1.

Redis 127.0.0.1:6379>getrange Phone 10 13--Return value is: 1, this number is subscript until 10 of the 11-digit number, from 10 to intercept, intercept to 13th bit, the last 2 bits do not exist ignore, only return 10th bit. So return to clean 1.

Redis Bulk Operations Modify and read string data, command mget, bulk read, command mset, batch assignment, command msetnx, with transactional assignment, find a key already exists, all transactions regression, do not do assignment processing operation

Redis 127.0.0.1:6379>mset name ZLH Age---Return value: OK, this sets the value of key to name and age respectively to zlh,30

Redis 127.0.0.1:6379>mget name ZLH---return value: 1>zlh 2>30.

Redis 127.0.0.1:6379>msetnx name Jim address China---return value is: 0, no modification is made because key is already present.

Redis 127.0.0.1:6379>mget Name Address---return value: 1>zlh 2>null

Redis 127.0.0.1:6379>msetnx Address China,hobbies Sports--Return value: 1, insert successful

Redis 127.0.0.1:6379>mget Address Hobbies---Return value: 1>china 2>sports

Talk about Windows environment installation and download

1, the above is the title of the nonsense.

2. Several other types of data, write again tomorrow night.

3, here too late, I do not write, or Die Beg, installation and download, and Redis from the beginner to proficient I will be a little tidy up, I hope you can join the upper left corner of the group, we do communication, good night.

REDIS Data Structures

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.