Redis sort implements list and hash connections isolate a row of the SQL table

Source: Internet
Author: User
Tags sorts

Sort using external key

You can sort by using the data from the external key as a weight instead of the default direct contrast key value.

Suppose you now have user data as follows:

UID User_name_{uid} User_level_{uid}
1 Admin 9999
2 Jack 10
3 Peter 25
4 Mary 70

The following code enters data into Redis:

# Adminredis 127.0.0.1:6379> lpush uid 1 (integer) 1redis 127.0.0.1:6379> SET user_name_1 Adminokredis 127.0.0.1:637  9> set User_level_1 9999ok# jackredis 127.0.0.1:6379> lpush uid 2 (integer) 2redis 127.0.0.1:6379> SET user_name_2 Jackokredis 127.0.0.1:6379> SET user_level_2 10ok# peterredis 127.0.0.1:6379> lpush uid 3 (integer) 3redis 127.0.0.1 :6379> set User_name_3 Peterokredis 127.0.0.1:6379> set user_level_3 25ok# maryredis 127.0.0.1:6379> LPUSH UID 4 ( Integer) 4redis 127.0.0.1:6379> set user_name_4 maryokredis 127.0.0.1:6379> set User_level_4 70OK
By option

By default, the sort uid is sorted directly by the value in the uid :

Redis 127.0.0.1:6379> SORT uid1) "1"      # admin2) "2"      # Jack3) "3"      # Peter4) "4"      # Mary

By using the by option, you can have the uid sorted by the elements of other keys.

For example, the following code allows the UID key to be sorted by the size of User_level_{uid} :

Redis 127.0.0.1:6379> SORT uid by user_level_*1) ' 2 '      # jack, level = 102) ' 3 '      # peter, level = 253) ' 4 '      # Mary, Level = 704) "1"      # Admin, level = 9999

user_level_* is a placeholder that takes out the value in the uid and then uses that value to find the corresponding key.

For example, when sorting the uid list, the program takes out the uid values 1 , 2 , 3 , 4 , and then uses the user_level the values of _1, user_level_2 , user_level_3 , and user_level_4 are used as weights for the sort uid .

GET Options

Using the GET option, you can remove the corresponding key value based on the result of the order.

For example, the following code first sorts the uid and then takes out the value of the key User_name_{uid} :

Redis 127.0.0.1:6379> SORT uid GET user_name_*1) "admin" 2) "Jack" 3) "Peter" 4) "Mary"
Combined use by and GET

By combining by and GET , you can make the sort results appear in a more intuitive way.

For example, the following code first sorts the UID list by pressing User_level_{uid} , and then extracts the value of the corresponding User_name_{uid} :

Redis 127.0.0.1:6379> SORT uid by user_level_* GET user_name_*1) "Jack" (Level       = 102) "Peter"      # level = 253) " Mary "       # level = 704)" admin "      # level = 9999

The sorting results are now much more intuitive than using only the sort uid by user_level_* .

Get multiple foreign keys

You can use multiple get options at the same time to obtain values for multiple foreign keys.

The following code obtains User_level_{uid} and User_name_{uid} by uid , respectively:

Redis 127.0.0.1:6379> SORT uid get user_level_* get user_name_*1) "9999"       # Level2) "admin"      # Name3) "4" Jac K "5)" (6) "Peter" 7) "8" "Mary"

get has an extra parameter rule that is--you can use # to get the value of the sorted key.

The following code returns the value of the uid , and its corresponding user_level_* and user_name_* , as the result:

Redis 127.0.0.1:6379> SORT uid Get # get user_level_* get User_name_*1) "1"          # Uid2) "9999"       # level3) "Admin" c12/># name4) "2" (5) "6") "Jack" 7) "3" 8) "9" "Peter" "4" and "Ten" "Mary"
get foreign keys, but not sort

By passing a non-existent key as an argument to the by option, you can let sort skip the sort operation and return the result directly:

Redis 127.0.0.1:6379> SORT uid by Not-exists-key1) "4" 2) "3" 3) "2" 4) "1"

When used alone, this usage is of little practical use.

However, by combining this usage with the get option, you can get multiple foreign keys without sorting, which is equivalent to performing a consolidated fetch operation (similar to the join keyword for SQL database).

The following code demonstrates how to get multiple foreign keys using sort , by, and get without causing a sort:

Redis 127.0.0.1:6379> SORT uid by Not-exists-key get # get user_level_* get User_name_*1) "4"      # Id2) "All"     # Lev EL3) "Mary"   # Name4) "3" 5) "6" "Peter" 7) "2" 8) "9" "Jack" (1) "9999") "admin"
Use a hash table as a GET or by parameter

In addition to string keys, a hash table can also be used as a parameter to a GET or by option.

For example, for the User information table given earlier:

UID User_name_{uid} User_level_{uid}
1 Admin 9999
2 Jack 10
3 Peter 25
4 Mary 70

Instead of saving the user's name and level in User_name_{uid} and User_level_{uid} Two string keys, we can use a name field with a The domain hash table User_info_{uid} to hold the user's name and level information:

Redis 127.0.0.1:6379> hmset user_info_1 name admin level 9999OKredis 127.0.0.1:6379> hmset user_info_2 name Jack Lev El 10OKredis 127.0.0.1:6379> hmset user_info_3 name Peter level 25OKredis 127.0.0.1:6379> hmset user_info_4 name Mar Y level 70OK

The by and get options can then be used in the format of the Key->field to get the values of the fields in the hash table, where key represents the Hashtable key, and field represents the HA Domain of the Greek table:

Redis 127.0.0.1:6379> sort uid by User_info_*->level1) "2" (2) "3" 3) "4" 4) "1" Redis 127.0.0.1:6379> SORT uid by use R_info_*->level GET user_info_*->name1) "Jack" 2) "Peter" 3) "Mary" 4) "admin"

Redis sort implements list and hash connections isolate a row of the SQL table

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.