Redis Five types of data usage scenarios

Source: Internet
Author: User
Tags redis serialization

Copyright NOTICE: This article is the original article of the blogger, without the permission of the blogger may not be reproduced.

Directory (?) [+]String [HTML]View plain copy 1, String common commands: In addition to operations such as GET, set, INCR, Decr mget, Redis also provides the following actions: getting string length to set the string append content and get a string of Segment content setting and getting a string of one (bit) batch setting the contents of a series of strings the scenario: string is the most commonly used type of data, and ordinary key/value storage can be classified as such, and value is not just a string, but it can also be a number: Want to know when to block an IP address (access more than a few times).      The Incrby command makes it easy to keep the count by increasing atomic increments. Implementation: M,DECR and other operations will be converted to a numeric calculation, at this time Redisobject encoding field of Int.
Hash [HTML]  View plain  copy   Common commands:   hget,hset,hgetall  etc.    Application Scenario:   We simply give an example to describe the application scenario for the hash, for example, we want to store a user information object data containing the following information:                User ID, for lookup key,                stored value user objects include name, age, birthday birthday  and other information,       If the common key/value structure to store, there are mainly the following 2 storage methods:           The first way to use the user ID as the lookup key, Encapsulate other information into an object to store,   in a serialized way             such as: Set  u001  "Lie triple Systems, 18,20010101"                The disadvantage of this approach is that the cost of serialization/deserialization is increased, and when one of the information needs to be modified, the entire object needs to be retrieved, and the modification operation needs to be secured concurrently, introducing complex issues such as CAs.            The second method is how many members of this user information object will be stored as key-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,             &nbsp, such as:mset user:001:name  "lie triple systems  " user:001:age18 user:001:birthday  "20010101"                 Although it eliminates serialization overhead and concurrency problems, the user ID is duplicate storage, If there is a large amount of such data, memory waste is still very considerable.         So Redis provides a good solution to this problem, Redis hash is actually the internal storage value of a hashmap,        provides direct access to this map member's interface,            such as: Hmset  user:001 name  "Lie triple Systems"  age 18 birthday  "20010101"                     In other words, key is still user Id,value is a map, The key of this map is the property name of the member, value is the property,                 Such data modification and access can be directly through its internal map key (Redis called internal map key field),  that is through                  key (User ID)  + field (property label)   operation corresponding attribute data, There is no need to store data repeatedly or to bring serialization and concurrencyModify the control problem. Solved the problem very well.                  Here you should also note that Redis provides an interface ( Hgetall) can directly fetch all of the attribute data, but if there are many members of the internal map, it involves the operation of traversing the entire internal map, which, due to the Redis single-threaded model, may be time-consuming and the requests of other clients are completely unresponsive, which requires extra attention.       Implementation:        It has already been said that Redis hash corresponding to the value inside is actually a hashmap, In fact there will be 2 different implementations, this hash member is relatively young redis in order to save memory will be similar to a one-dimensional array of storage, rather than using a real hashmap structure, the corresponding value  The encoding of Redisobject is Zipmap, and when the number of members increases, it automatically turns into a real hashmap, at which point encoding is HT.   
List [HTML]  View plain  copy   Common commands:       lpush,rpush,lpop,rpop,lrange,blpop (blocked version Wait       Application Scenario:       redis list has many applications and is also one of the most important data structures in Redis.         We can easily implement features such as the latest news ranking. Another application of        lists is Message Queuing, which can take advantage of lists push operations, where tasks exist in lists, and then worker threads use pop operations to pull tasks out.       Implementation Mode:       redis list is a two-way linked list, which can support reverse lookup and traverse, more convenient operation, But with some extra memory overhead, many implementations within the Redis, including sending buffer queues, are also used in this data structure.       rpoplpush source destination           Command  RPOPLPUSH  within an atomic time, perform the following two actions:        the last element in the list  source  (tail element ) pops up and returns to the client.         inserts  source  popup elements into the list  destination , as  destination The header element of the   list.         if  source  and  destination  are the same, columnsThe footer element in the table is moved to the header and returns the element, which can be regarded as the rotation (rotation) Action of the list. &n

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.