Visually, the data in the database is stored on a table, and more microscopically, the tables are stored on a row. Each execution of a
Secondary select query, the database returns a result set that consists of several rows. So, a natural
The idea is to find a data structure in Redis that corresponds to the database L row. Five basic data structures are available in Redis
, which is a string, a list, a hash (hash), a collection (set), and an ordered set (sorted
Set). After investigation, it is found that there are two kinds of data structures suitable for storing rows, namely, string and hash.
To put the row data of a database into a string, you first need to format the row data. In fact, the result set
Each row can be seen as a collection of key-value pairs consisting of field names and their corresponding values. This key-value pair structure makes it easy for us
Think of JSON format. Therefore, the JSON format is chosen as the format template for each row of the result set. According to this idea, I
You can format the result set as several JSON objects and convert the JSON object to a string into a redis.
The process of storing data in a database is much more intuitive than storing it in string. This is the structural nature of the hash
The--hash itself is a set of key-value pairs: A "Parent key" contains a number of "subkeys", each "sub-key"
Key "corresponds to a value. According to the previous analysis, each row in the result set is actually a set of key-value pairs. Use
The Redis key-value pair collection represents the database key-value pair collection should be more appropriate: for a row in the result set, the field corresponds
In the hash "sub-key", the value of the field is the hash "sub-key" corresponding to the value, that is, the result set of one row corresponds to a
Hash
Redis Hash is a string-type field and value mapping table. A key can correspond to more than one field, and one field corresponds to a value. Storing an object as a hash type can save memory more than storing each field as a string type.
Redis Cache Data Table