In this paper, Redis How to save relational data, and how to match, scope, fuzzy query For example, fuzzy query function is based on the latest 2.8.9 version. 1 Storage of relational data
Take the staff object as an example, in a relational database or a similar Gridgain memory grid product (the underlying use of the H2 database in memory mode storage), we save the object's data in a table Form. Because the memory grid is cached based on objects, an extra column (staff column) is used to save the entire object's encoding, such as a serialized binary or JSON format, which facilitates direct return to the application for deserialization. In Redis, we can use ID as the unique identification, using key-value, hash, zset three kinds of data structure to save. Key-value is to save the ID and the entire object, determine the ID range can be based on it to return the object to the client, and choose the other two data structure of the specific reasons behind.
2 Matching query
The hget or hmget of a hash table can be used to achieve a single or multivalued exact matching query dept= ' it ' or dept in (' It ', ' QA '). Get the ID list, then go to the query Key-value get to the object. 3 Range Query
Because we save the age as a Zset score,value is an ID, we can use the Zset Zrangebyscore method to get the value of score in a range of intervals. 4 Fuzzy Query
Redis 2.8.9 after Zset added a very useful method Zrangebylex, we will save score is the name: ID format, using 0,value can get letters in a range of value values. For example, Zrangebylex name [A, (F), you can query Allen, Aaron, Carter. 5 pagination Query
At the same time,Zrangebylex also supports paging queries, syntax similar to limit start, offset. 6 Limitations
The above examples illustrate several common queries in the Redis implementation, but redis after all just key-value storage, so there are many limitations. For example, 1 cannot implement a multiple-conditional combination of queries, such as AGE>25 and name like ' a% ', which requires more than one command to implement and compute the set or intersection. 2 Fuzzy query Chinese more laborious: