Redis (4) for beginners-Sort Redis cache and redis

Source: Internet
Author: User

Redis (4) for beginners-Sort Redis cache and redis

Before implementing the cache sorting function, you must first clarify the rationality of this function. Now that you can sort data in the database, why do you need to put the sorting function in the cache? The following two reasons are briefly summarized: first, sorting increases the load on the database and is difficult to support highly concurrent applications. Second, sorting in the cache will not encounter the problem of table locking. Redis just provides the sorting function, so that we can conveniently sort the cache.

In Redis, the SORT command is used to implement the sorting function. This command provides multiple parameters to sort lists, sets, and sorted sets. The SORT command format is as follows:

SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]

The BY parameter is used to specify the sorting field. The function is similar to the order by parameter in SQL. For lists and sets, sorting by their values alone is often meaningless. Take the set returned by the Cache2Hash function as an example (the set key is actually returned). The set stores a series of complete hash keys, which are sorted only by these keys, the result is simply arranged in numbers or dictionary order, and its usage is obviously not large. This is because the hash structure rather than the hash key is used to store row data. Assume that the set key is "resultset. hash: 123456 ", each hash key in the set corresponds to a hash structure with a field named" timestamp ". Now we want to sort all the hash keys in the set according to the timestamp field, run the following command:

SORT resultset.hash:123456 BY *->timestamp

From the above example, we can see that the real power of BY is that it allows the SORT command to SORT according to the external field of a specified external key. SORT uses the set resultset. hash: Replace the first "*" after the BY parameter for each value (that is, each hash key) in 123456, and obtain the value based on the field given after "->, finally, sort the hash key based on these field values.

The LIMIT parameter is used to limit the number of elements returned after sorting. It is similar to the LIMIT parameter in SQL. This parameter accepts two other parameters, namely offset and count. LIMIT offset count indicates that the first offset element is skipped and the count element is returned continuously. It can be seen that the LIMIT parameter can be used to implement the paging function.

The GET parameter is used to return the specified field value. Take the resultset. hash: 123456 as an example. After the BY parameter is used to SORT all hash keys in the set according to the timestamp field in the hash structure, the SORT command returns all sorted hash keys. If a request requires some field values instead of keys, you must use the GET parameter so that the SORT command returns the specified field value. In addition to the timestamp field, the hash structure corresponding to each hash key in the Set also contains a field named "id, run the following command to enable SORT to return the values of timestamp and id in the hash structure corresponding to each hash key sorted by timestamp:

SORT resultset.hash:123456 BY *->timestamp GET *->timestamp GET *->id

SORT replaces the first "*" after the GET parameter with each value (that is, each hash key) in the resultset. hash: 123456 set and uses it as the return value. It is worth noting that you can use GET # To GET the hash key itself in the set.

The ASC and DESC parameters are used to specify the sorting order (ASC by default, from low to high), and the ALPHA parameter is used to sort non-numeric elements alphabetically.

The STORE parameter is used to save the return value of the SORT command, that is, the sorting result to a specified list. After the STORE parameter is added, the return value of the SORT command changes to the number of sorting results.

The following code sorts the hash keys in the set by a hash field and saves the result to the list:

// This function sorts all HASH keys in the Set Based on a field in the HASH key corresponding to the HASH key. // The sorting result is saved to a LIST structure, the LIST key should contain the result set identifier and sorting Field identifier, // such as "sorted: 123456: 1234" string SortHash (SQL: Connection * mysql_connection, redisContext * redis_connection, const string & resultset_id, const string & sort_field, int offset, int count, int order, int ttl) {// only SET string redis_row_set_key = "resultset that stores the HASH key. hash: "+ resultset_id; redisReply * reply; // check SET whether reply = static_cast <redisReply *> (redisCommand (redis_connection, "EXISTS % s", redis_row_set_key.c_str (); if (reply-> integer = 0) {freeReplyObject (reply); throw runtime_error ("FAILURE-no resultsets");} else {freeReplyObject (reply);} string field_md5 = md5 (sort_field ); // use MD5 to exclude the influence caused by spaces in the sorting field // Save the sorting result to the LIST string redis_sorted_list_key = "sorted:" + resultset_id + ":" + field_md5; st Ring by ("*->" + sort_field); // determine the sorting field string ord = (order = 1 )? "ASC": "DESC"; // order = 1 is sorted in ascending order; otherwise, it is stringstream ofsstream, cntstream; ofsstream <offset; cntstream <count; // execute the sorting command and save the sorting result to LIST reply = static_cast <redisReply *> (redisCommand (redis_connection, "SORT % s BY % s LIMIT % s GET % s alpha store % s", redis_row_set_key.c_str (),. c_str (), ofsstream. str (). c_str (), cntstream. str (). c_str (), "#", redis_sorted_list_key.c_str (); freeReplyObject (reply); stringstream ttlstream; ttlstream <ttl; // set the expiration time of the LIST reply = static_cast <redisReply *> (redisCommand (redis_connection, "EXPIRE % s", redis_sorted_list_key.c_str (), ttlstream. str (). c_str (); freeReplyObject (reply); return redis_sorted_list_key; // return the LIST key, so that other functions can obtain the content in the LIST.

Obviously, sorting hash keys in the result set is more intuitive and convenient than sorting string keys. The sorting function allows you to conveniently query the sorted result set in Redis. The Code is as follows:

// This function queries and sorts the corresponding result set in Redis based on SQL statements and sorting parameters, and finally returns the HASH key vector <string> GetSortedCache (SQL:: Connection * mysql_connection, redisContext * redis_connection, const string & SQL, const string & sort_field, int offset, int count, int order, int ttl) {vector <string> redis_row_key_vector; redisReply * reply; string resultset_id = md5 (SQL); // result set identifier string field_md5 = md5 (sort_field ); // sort Field identifier // try to get all HASH keys in the LIST string redis_sorted_list_key = "sorted:" + resultset_id + ":" + field_md5; // try to get all HASH keys in the LIST reply = static_cast <redisReply *> (redisCommand (redis_connection, "LRANGE % s", redis_sorted_list_key.c_str (), "0 ", "-1"); if (reply-> type = REDIS_REPLY_ARRAY) {// if the LIST does not exist, call the Cache2Hash function to pull data from Mysql to Redis, then, call the SortHash function // sort the result set and save the sorted HASH key to LIST if (reply-> elements = 0) {freeReplyObject (reply); SQL :: statement * stmt = mysql_connection-> createStatement (); SQL: ResultSet * resultset = stmt-> executeQuery (SQL); Cache2Hash (mysql_connection, redis_connection, resultset, resultset_id, ttl ); redis_sorted_list_key = SortHash (mysql_connection, redis_connection, resultset_id, sort_field, offset, count, order, ttl ); // try again to get all HASH keys in the LIST reply = static_cast <redisReply *> (redisCommand (redis_connection, "LRANGE % s", redis_sorted_list_key.c_str (), "0 ", "-1"); delete resultset; delete stmt;} // save all HASH keys in the LIST to string redis_row_key_key; for (int I = 0; I <reply-> elements; ++ I) {redis_row_key = reply-> element [I]-> str; redis_row_key_vector.push_back (redis_row_key);} freeReplyObject (reply );} else {freeReplyObject (reply); throw runtime_error ("FAILURE-LRANGE error");} return redis_row_key_vector ;}

In this way, you can simply sort the result set in Redis.


What should I do if I use redis to read cached data?

Is it C programming? You can use the hiredis database.

When Redis is used for caching, set is written to Chinese. The get result is \ xe6 \ xb5 \ x8b \ xe8 \ xaf \ x95. How can this problem be solved?

I have never encountered it. It is estimated that transcoding is incorrect,
Redis is serialized using js by default. If byte [] is written, it will not be serialized and saved directly,
We recommend that you serialize the data into byte [] before writing.
Deserialization

This is what I do now.

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.