Redis data types

Source: Internet
Author: User
Tags set set value store

The previous article mainly wrote about Redis installation under Linux, here is the basic data type of Redis, Linux is rich in data types, there are five types of data
1. String string type common commands: except GET,Set, INCR, DECR Mget and other operations, Redis also provides the following actions: Gets the string length to the string append content settings and gets a section of the string content settings and gets a string of one (bit) batch set a series of strings of the content scenario: String is the most common type of data, and a common 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. 2. Hash Key-value pair type common commands: Hget,hset,hgetall, etc. 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: The user ID, the key to find, the stored value user object contains the name name, age, birthday birthday and other information, If you use a normal key/value structure to store, mainly has the following 2 kinds of storage: 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:Setu001"lie triple, 18,20010101"The disadvantage of this approach is that it adds serialization/The cost of deserialization , and when one of the information needs to be modified, the whole object needs to be retrieved, and the modification operation needs to protect the concurrency, introduce complex problems such as CAs. The second method is how many members of this user information object will be saved to the number of keys-value, with the user id+The name of the corresponding property is used as a unique identifier to obtain the value of the corresponding property, such as: Mset User:001: Name"lie triple"User001: Age18 User:001: Birthday"20010101"Although the serialization overhead and concurrency issues are omitted, the user ID is a duplicate storage, and if there is a large amount of such data, the memory waste is very considerable. The hash provided by Redis is a good solution to this problem, and the Redis hash is actually the internal stored value as a hashmap, and provides a direct access to the map member's interface, such as: Hmset User:001Name"lie triple"Age -Birthday"20010101"that is, the key is still the user id,value is a map, the map key is a member of the property name, value is the property value, so that the data can be modified and accessed directly through its internal map key (Redis called internal map key field ), i.e. via key (user ID)+The field (property label) operation corresponds to the attribute data, which does not require the data to be stored repeatedly, nor does it introduce serialization and concurrency modification control issues. A good solution to the problem. It is also important to note that Redis provides an interface (Hgetall) that can fetch all of the property data directly, but if the internal map has a large number of members, it involves traversing the entire internal map, which can be time-consuming due to the Redis single-threaded model. The other client requests are not responding at all, 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. 3. 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_ips","192.168.0.12"Redis.lpush"downstream_ips","192.168.0.13"then:next_ip= Redis.rpoplpush"downstream_ips","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 is empty, skipped, followed by the first element of the command list is popped. 1)"Command"# The list that the popup element belongs to2)"Update System ..."# The value that the popup element belongs to is why block the version of Pop, mainly 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. 4.SetCommon 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 apps, everyone's friends have a collection (Setin this case, it may only be necessary to use the intersection command in order to find the operation of a common friend of two people. Redis also provides a set of operations such as intersection, set, and difference set, which can be implemented in a very convenient way:Setthe internal implementation is a value that is always null hashmap, is actually calculated by calculating the hash of the way to fast weight, which is also set to provide a judge whether a member is within the set. 5. sortSetcommonly used commands: Zadd,zrange,zrem,zcard, such as the use of the scene: a condition as a weight, such as the number of top-ranked. The Zrevrange command can be used to get the top 100 users according to the score, Zrank can be used to get the user rank, very straightforward and easy to operate. The usage scenario for Redis sorted set is similar to set, except that the set is not automatically ordered, and the sorted set can be ordered by the user with an additional priority (score) parameter, and is inserted in an orderly, automatic sort. For example, the public timeline of Twitter can be stored as score in the publication time, which is automatically sorted by time. For example: The class students sortedsets,value can be the student's school number, and score can be its test scores, so that the data into the collection, 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. Applications that require precise expiration time For example, if you can set the score value of the sorted set above as the timestamp of the expiration time, then you can simply sort through the expiration time and periodically purge out-of-date data, not just the stale data in Redis, You can think of this expiration time in Redis as an index to the data in the database, use Redis to find out what data needs to be deleted, and then delete the corresponding records from the database exactly. Implementation: The internal use of Redis sorted set HashMap and Jump Table (skiplist) to ensure data storage and order, HashMap in the member to score mapping, and the jumping table is stored in all the members, Sorting is based on the score of HashMap, using the structure of the jumping table can obtain a relatively high efficiency, and the implementation is relatively simple. 

Redis data 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.