Redis is an open source key-value store or cache that provides a rich data type. The main include string, hash, list, set and ordered set, different data types have different applicable scenarios. This article makes a brief introduction to Redis's various data types so that new contact with Redis can be familiar and used as soon as possible.
1. String type
Defining this type of data can store any form of string, even images, JSON objects, and so on. However, there is a limit to the size of the storage, such as a maximum of 512M per key value, but this is sufficient to satisfy most applications. The string type is the basic type of redis storage and, in a sense, other centralized data types are also combinations of string types.
Simple client use commands such as:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/54/1F/wKioL1R4ivzw4d5zAAIrKnAjmlY768.jpg "title=" String.png "alt=" Wkiol1r4ivzw4d5zaairknajmly768.jpg "/>
2.HASH type
A hash type is similar to the structure of a dictionary and is suitable for storing specific properties of an entity. such as book, the author, publishing house, publication date and other information. In fact, it stores a mapping of fields and corresponding values.
Simple to use such as:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/1F/wKioL1R4iyjw0SM5AAKtViLtTRM907.jpg "title=" Hash.png "alt=" Wkiol1r4iyjw0sm5aaktvilttrm907.jpg "/>
3. List Type
The list type actually stores an ordered list of strings, which corresponds to the following collection type. A common list operation is to add elements at both ends, or to get data from a segment of the list. Lists are actually implemented using a doubly linked list, so adding elements at both ends of the table is fast. But when you look at some of the elements in the middle of the list, the efficiency is not too high.
The simple usage of the list is as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/20/wKiom1R4itiwd5bpAAFGcMYVGTo387.jpg "title=" List.jpg "alt=" Wkiom1r4itiwd5bpaafgcmyvgto387.jpg "/>
In fact, the use of the list of scenes is relatively broad, such as the implementation of the queue, log storage, etc., can be used lpush and lpop implementation of the stack, using Lpush and Rpop to achieve the use of the queue (Lrange command display list of data, 0 and 1 respectively represents the left and right border).
4. Collection type
The collection here is actually analogous to a collection of mathematics, often used to determine whether an element exists, such as a cache of friends in a microblog. The collection is implemented inside Redis using a hash table with a null value. So the time complexity of these operations is O (1). The collection can be easily used to do set operations, such as orthogonal, such as micro-blog can easily display two accounts of common friends.
The simple collection commands are used as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/20/wKiom1R4i46xowcFAAG9tV6c9uk308.jpg "style=" float: none; "title=" set1.jpg "alt=" Wkiom1r4i46xowcfaag9tv6c9uk308.jpg "/>
You can also save the result of an operation of 2 sets to another collection type:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/20/wKiom1R4i5SjpseiAACcKb_N98U112.jpg "style=" float: none; "title=" set2.jpg "alt=" Wkiom1r4i5sjpseiaacckb_n98u112.jpg "/>
5. Ordered collection types
on the collection type , an ordered collection is associated with a fraction for each element in the collection. You can get, sort, and manipulate the ordered set values based on the scores in the range. The specific usage is as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/54/1F/wKioL1R4jMDD7Q_-AADVRHt8nQE775.jpg "title=" Zset.jpg "alt=" Wkiol1r4jmdd7q_-aadvrht8nqe775.jpg "/>
6. Application Scenarios
I'm working for a game company, and for the current application, Redis is widely used in data caching. Basically configuring MySQL or MongoDB as a DB tier for data storage use. Simple analysis of its various types of application scenarios.
String: Typically used for account mapping.
Hash: The primary user account role information and the storage of its accessory props.
List: Application of queues, slow write of DB data, etc.
Collection: User single attribute records such as friends and other information
An ordered set: such as a global ranking
Expiry time: Often used for regular event package issuance
The data types for Redis are described here, and the advanced features of Redis are described in more detail later.
This article from "Haris" blog, declined reprint!
Introduction to Redis data types