Redis data types, and applications

Source: Internet
Author: User
Tags set set value store

The data types commonly used by Redis are string,hash,list,set, and so on, as follows:

String
1, String Common command: In addition to get, set, INCR, DECR Mget and other operations, Redis also provides the following actions: Gets the string length toward the string append content settings and gets a section of the string content settings and get a string of one (bit) batch set a series of string content scenario: string is the most common type of data, ordinary key/ The value store can be classified as such, and value is not only a string, but also a number: for example, you want to know when to block an IP address (accessed more than a few times). The Incrby command makes it easy to keep count by atomic increment. Implementation mode: M,DECR and other operations will be converted to a numerical type for calculation, at this time redisobject the encoding field is an int.
Hash
Common commands: Hget,hset,hgetall and so on. Application scenario: We simply cite an example to describe the application of the hash, for example, we want to store a user information object data, including the following information: User ID, key to find, stored value user object contains name name, age, Birthday BIRTHD Ay and other information, if the ordinary key/value structure to store, mainly has the following 2 storage methods: The first way to find the user ID key, the other information encapsulated as an object to be stored in a serialized way, such as: Set u001 "Lie triple, 18,200       10101 "The disadvantage of this approach is that it increases the cost of serialization/deserialization, and when one of the information needs to be modified, the entire object needs to be retrieved, and the modification operation needs to protect concurrency and introduce complex problems such as CAs. The second method is how many members of this user information object will be saved into the number of key-value, with the user id+ the name of the corresponding property as a unique identifier to obtain the value of the corresponding property, such as: Mset User:001:name "Lie triple" User:001:age18 user    : 001:birthday "20010101" Although there is no serialization overhead and concurrency problems, but the user ID is a duplicate storage, if there is a large number of such data, memory waste is very considerable. So the hash provided by Redis is a good solution to this problem, the Redis hash is actually stored internally as a value of HashMap, and provides direct access to the map member's interface, such as: Hmset user:001 name "Lie Triple" age 1 8 Birthday "20010101" that is, the key is still user Id,value is a map, the map key is a member of the property name, value is a property value, so that the data can be modified and accessed directly through its Internal Map key (Redis is called the internal map key is field), that is, through the key (user ID) + field (attribute tag) operation of the corresponding attribute data, do not need to store data repeatedly, and does not bring serialization and concurrency modification control problems.          A good solution to the problem. It is also important to note that Redis provides an interface (Hgetall) that can be taken directly to the entire genusData, but if the internal map has a large number of members, then it involves traversing the entire internal map, which, due to the Redis single-threaded model, can be time-consuming and other client requests are completely unresponsive, which requires extra attention. Implementation: The above has been said that the Redis hash corresponds to value inside the actual is a hashmap, actually there will be 2 different implementations, this hash of the members of the relatively small redis in order to save memory will be similar to a one-dimensional array to compact storage, Instead of a true hashmap structure, the corresponding value Redisobject encoding is Zipmap, and when the number of members increases, it automatically turns into a real hashmap, at which time encoding is HT.
List
Common commands: Lpush,rpush,lpop,rpop,lrange,blpop (blocked version) and so on.    Application Scenario: Redis list has many applications and is one of the most important data structures for Redis.    We can easily achieve the latest news ranking and other functions. Another application of the lists is Message Queuing, which can take advantage of the lists push operation to present the task in lists, and then the worker thread then takes the task out of execution with a pop operation. Implementation: the implementation of the Redis list is a doubly linked list, which can support reverse lookup and traversal, more convenient operation, but with some additional memory overhead, many implementations within Redis, including sending buffer queues, are also used in this data structure.    The Rpoplpush source Destination command Rpoplpush performs the following two actions in an atomic time: POPs the last element in the list source (the trailing element) and returns it to the client.    Inserts a source popup element into the list destination, as the head element of the destination list.    If source and destination are the same, the footer elements in the list are moved to the table header and returned, which can be considered as a rotation (rotation) operation of the list.    A typical example is the server's monitoring program: they need to check a set of Web sites in parallel, in the shortest possible time, to ensure their accessibility. Redis.lpush "Downstream_ips", "192.168.0.10" Redis.lpush "Downstream_ips", "192.168.0.11" Redis.lpush "downstream_ip  S "," 192.168.0.12 "Redis.lpush" Downstream_ips "," 192.168.0.13 "then:next_ip = Redis.rpoplpush" Downstream_ips ", The "Downstream_ips" Blpop assumes that there are now three lists of job, command, and request, where job does not exist, and command and request both hold a non-empty list. Consider the following command: Blpop job command Request #阻塞30秒, 0 is blocked indefinitely, the job list isEmpty, skipped, and immediately after the first element of the command list is ejected. 1) "Command" # popup element belongs to List 2) "Update system ..." # The value that the popup element belongs to why block the version of Pop? The main purpose is to avoid polling. As a simple example, if we use list to implement a Task Force column. The thread that performs the task can invoke the blocked version of the pop to get the task so that the polling can be avoided to check for the presence of a task. The worker thread can return immediately when the task comes, or it can avoid the delay caused by polling.
Set
4, set common commands:    Sadd,srem,spop,sdiff, Smembers,sunion and so on. Scenario:    Redis Set provides functionality that is similar to a list, except that set is automatic, and when you need to store a list of data and you don't want duplicate data, set is a good choice. and set provides an important interface to determine whether a member is within a set set, which is not available in list.    For example, in Weibo applications, everyone's friends have a collection (set), so that two people's common friend's operation, you may only need to use the intersection command.    Redis also provides for the collection of intersection, set, difference sets and other operations, can be very convenient implementation of the real way:    the internal implementation of the set is a value is always null HashMap, in fact, by calculating the way of hash to quickly weight, This is also why set can provide a reason for judging whether a member is within a set.

Reproduced in: http://blog.csdn.net/gaogaoshan/article/details/41039581#comments

Redis data types, and applications

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.