REDIS Data Structures

Source: Internet
Author: User

Website
http://redis.io/


Similar to memcached, but solves the situation where the data is completely lost after a power outage, that is, the persistence of the data. and support for more value types, in addition to and string, there are several data types that support hash, lists (linked list), sets (collection), and sorted sets (ordered collections).


Redis currently offers five types of data

String is the simplest type, you can understand the same type as memcached, a key corresponds to a value, and the operation supported on it is similar to the operation of Memcached. But it's more versatile.
Example:
# REDIS-CLI Set Key1 "Aminglinux.com"
Ok

# REDIS-CLI Get Key1
"Aminglinux.com"


List is a linked list structure, the main function is push, pop, get all the values of a range and so on. Key in operation is understood as the name of the linked list. With the List structure, we can easily implement features such as the latest message ranking (such as the TimeLine of Sina Weibo). Another application of list is the message queue, which can take advantage of the *push operation of list, the task exists in list, and then the worker thread takes out the task with the POP operation to execute. Redis also provides an API to manipulate a section of the list, and you can directly query and delete elements of a section of the list.
Example:
[Email protected] ~]# redis-cli rpush mylist "aaaa bbb." (insert Lpush from right to insert from left)
(integer) 1
[Email protected] ~]# redis-cli rpush mylist "1 2 3."
(integer) 2
[Email protected] ~]# redis-cli rpush mylist "Aminglinux."
(integer) 3

[[email protected] ~]# redis-cli lrange mylist 0 2
1) "AAAA bbb."
2) "1 2 3."
3) "Aminglinux."
[[email protected] ~]# redis-cli lrange mylist 0 1
1) "AAAA bbb."
2) "1 2 3."

Set is a set, and we are similar to the concept of the set in mathematics, the operation of the set has added delete elements, there are multiple sets of orthogonal and poor operation. In Operation Key is understood as the name of the collection. For example, in a microblog application, you can have a collection of all the followers of a user, and a collection of all their fans. Because Redis is very human for the collection to provide the intersection, set, difference sets and other operations, it can be very convenient to achieve such as common concern, common preferences, two-degree friends and other functions, to all of the above collection operations, you can also use different commands to choose whether to return the results to the client or save set into a new collection. (Random sort)
[Email protected] ~]# redis-cli Sadd MySet A
(integer) 1
[Email protected] ~]# REDIS-CLI Sadd MySet b
(integer) 1
[Email protected] ~]# redis-cli Sadd MySet D
(integer) 1
[Email protected] ~]# redis-cli smembers MySet
1) "B"
2) "D"
3) "A"

Sorted set is an ordered set, it is more than set a weight parameter score, so that the elements in the collection can be ordered by score, such as a storage class of sorted sets, its collection value can be the student's school number, and score Can be their test scores, so that when the data is inserted into the collection, it has been a natural sort. Also can use Sorted sets to do with the weight of the queue, such as the normal message score is 1, the important message of the score is 2, and then the worker can choose to press score reverse order to get work tasks. Let important tasks take precedence.
[[email protected] ~]# redis-cli zadd aming "abc"
(integer) 1
[[email protected] ~]# redis-cli Zadd aming 2 "CDE 123"
(integer) 1
[[email protected] ~]# redis-cli zadd aming "123-aaa"
(integer) 1
[ [email protected] ~]# redis-cli zadd aming 4 "a123a"
(integer) 1
[[email protected] ~]# redis-cli zrange Aming 0-1 (positive)
1) "CDE 123"
2) "a123a"
3) "ABC"
4) "123-AAA"

Reverse order
[[email protected] ~]# REDIS-CLI zrevrange aming 0-1
1) "123-AAA"
2) "ABC"
3) "a123a"
4) "CDE 123"

Hash data type, in Memcached, we often package some structured information into HashMap, which is stored as a string value (usually in JSON format) after the client is serialized, such as the user's nickname, age, gender, integral, and so on. When you need to modify one of these items, you usually need to remove the string (JSON), then deserialize, modify the value of an item, and then serialize it back into a string (JSON). Simply modify a property to do so many things, the consumption must be very large, also does not apply to some possible concurrent operation of the occasion (such as two concurrent operations need to modify the integral). The Hash structure of Redis allows you to modify only one item property value just as you would Update a property in a database.
127.0.0.1:6379> hset aminglinux domain www.aminglinux.com
(integer) 1
127.0.0.1:6379> Hget Aminglinux domain
"www.aminglinux.com"
127.0.0.1:6379> hset aminglinux bbs www.lishiming.net
( Integer) 1
127.0.0.1:6379> hget aminglinux bbs
"www.lishiming.net"

127.0.0.1:6379> Hgetall Aminglinux
1) "Domain"
2) "www.aminglinux.com"
3) "BBS"
4) "Www.lishiming.net"

Storage structure and storage format
Redis uses two file formats: full-volume data and incremental requests. The full data format is to write in-memory data to disk, so that the next time to read the file to load, the incremental request file is to serialize the in-memory data into the operation request, to read the file for replay to get the data, the serialized operation includes set, Rpush, Sadd, Zadd.


The Redis storage is divided into memory storage, disk storage, and log files, which are configured with three parameters in the configuration file.

The Save seconds Updates,save configuration, indicating how many times the update operation is synchronized to the data file. This can be a combination of multiple conditions, such as the default configuration file settings, set three conditions.

AppendOnly yes/no, appendonly configuration, indicates whether logging occurs after each update operation and, if not turned on, may result in data loss over a period of time when power is lost. Because the Redis itself synchronizes data files in sync with the save conditions above, some data will only exist in memory for a period of time.

Appendfsync no/always/everysec, Appendfsync configuration, no indicates that the operating system data cache synchronization to disk, always indicates that each update operation after the manual call Fsync () write data to disk, The everysec indicates synchronization once per second.

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.