The usual Redis sort we can do this:
Sort by commodity price: sort Goods_id_set by P_*_price
This is not a problem in non-clustered mode, but if you are in cluster mode, you will get an error:
It is not possible to use the sort by clause in cluster mode, because in cluster mode, the above p_*_price matching key may be distributed in the slots of different nodes, so it cannot be sorted.
What about that? Don't worry, there's still some way, we'll use SortedSet .
How to use it? Very simple, look at the code:
Let's add a few items and assign the price
Zadd goods_prices 1 //Product ID 1, price is 10 yuan
Zadd goods_prices 2 //Product ID 2, price is 20 yuan
Zadd goods_prices 3 //Product ID 3, price is 15 yuan
Zadd goods_prices 5 4 //commodity ID 4 Price is $5
Then sort:
1. Zrange goods_prices 0,9999999 //Sort by price from low to high
2. Zrevrange goods_prices 0,9999999 //Sort by price from highest to lowest
Printing results:
1.4,1,3,2
2.2,3,1,4
And then go through these IDs to get the product information.
Troubleshoot sorting problems in Redis cluster mode