Esta página aún no está disponible en su idioma local. Estamos trabajando arduamente para agregar más versiones de idiomas. Gracias por tu apoyo.
Ranking with Redis
The best practice for DCS guides you through ranking using DCS.
Scenario
Ranking is a function commonly used on web pages and apps. It is implemented by listing key-values in descending order. However, a huge number of concurrent operation and query requests can result in a performance bottleneck, significantly increasing latency.
Ranking using DCS for Redis provides the following advantages:
- Data is stored in the cache, so read/write is fast.
- Multiple types of data structures, such as strings, lists, sets, and hashes are supported.
Operation Guidance
- Prepare an ECS that runs the Windows OS.
- Install JDK1.8 (or later) and a development tool (Eclipse is used as an example) on the ECS, and download the Jedis client.
- Create a DCS instance on the DCS console. Ensure that you configure the same VPC and subnet for the DCS instance and the ECS.
- Run Eclipse on the ECS and create a Java project. Then, create a productSalesRankDemo.java file for the example code, and reference the Jedis client as a library to the project.
- Configure the connection address, port number, and password for the DCS instance in the example code file.
- Compile and run the code.
Sample Code
package dcsDemo02; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.UUID; import redis.clients.jedis.Jedis; import redis.clients.jedis.Tuple; public class productSalesRankDemo { static final int PRODUCT_KINDS = 30; public static void main(String[] args) { //Instance connection address, which is obtained from the DCS console. String host = "192.168.0.246"; //Redis port number int port = 6379; Jedis jedisClient = new Jedis(host, port); try { //Instance password String authMsg = jedisClient.auth("******"); if (!authMsg.equals("OK")) { System.out.println("AUTH FAILED: " + authMsg); } //Key String key = "Best-seller Rankings"; jedisClient.del(key); //Generate product data at random List<String> productList = new ArrayList<>(); for(int i = 0; i < PRODUCT_KINDS; i ++) { productList.add("product-" + UUID.randomUUID().toString()); } //Generate sales volume at random for(int i = 0; i < productList.size(); i ++) { int sales = (int)(Math.random() * 20000); String product = productList.get(i); //Insert sales volume into Redis SortedSet jedisClient.zadd(key, sales, product); } System.out.println(); System.out.println(" "+key); //Obtain all lists and display the lists by sales volume Set<Tuple> sortedProductList = jedisClient.zrevrangeWithScores(key, 0, -1); for(Tuple product : sortedProductList) { System.out.println("Product ID: " + product.getElement() + ", Sales volume: " + Double.valueOf(product.getScore()).intValue()); } System.out.println(); System.out.println(" "+key); System.out.println(" Top 5 Best-sellers"); //Obtain the top 5 best-selling products and display the products by sales volume Set<Tuple> sortedTopList = jedisClient.zrevrangeWithScores(key, 0, 4); for(Tuple product : sortedTopList) { System.out.println("Product ID: " + product.getElement() + ", Sales volume: " + Double.valueOf(product.getScore()).intValue()); } } catch (Exception e) { e.printStackTrace(); } finally { jedisClient.quit(); jedisClient.close(); } } }
Operation Result
Compile and run the preceding Demo code. The operation result is as follows:
Best-seller Rankings Product ID: product-b290c0d4-e919-4266-8eb5-7ab84b19862d, Sales volume: 18433 Product ID: product-e61a0642-d34f-46f4-a720-ee35940a5e7f, Sales volume: 18334 Product ID: product-ceeab7c3-69a7-4994-afc6-41b7bc463d44, Sales volume: 18196 Product ID: product-f2bdc549-8b3e-4db1-8cd4-a2ddef4f5d97, Sales volume: 17870 Product ID: product-f50ca2de-7fa4-45a3-bf32-23d34ac15a41, Sales volume: 17842 Product ID: product-d0c364e0-66ec-48a8-9ac9-4fb58adfd033, Sales volume: 17782 Product ID: product-5e406bbf-47c7-44a9-965e-e1e9b62ed1cc, Sales volume: 17093 Product ID: product-0c4d31ee-bb15-4c88-b319-a69f74e3c493, Sales volume: 16432 Product ID: product-a986e3a4-4023-4e00-8104-db97e459f958, Sales volume: 16380 Product ID: product-a3ac9738-bed2-4a9c-b96a-d8511ae7f03a, Sales volume: 15305 Product ID: product-6b8ad4b7-e134-480f-b3ae-3d35d242cb53, Sales volume: 14534 Product ID: product-26a9b41b-96b1-4de0-932b-f78d95d55b2d, Sales volume: 11417 Product ID: product-1f043255-a1f9-40a0-b48b-f40a81d07e0e, Sales volume: 10875 Product ID: product-c8fee24c-d601-4e0e-9d18-046a65e59835, Sales volume: 10521 Product ID: product-5869622b-1894-4702-b750-d76ff4b29163, Sales volume: 10271 Product ID: product-ff0317d2-d7be-4021-9d25-1f997d622768, Sales volume: 9909 Product ID: product-da254e81-6dec-4c76-928d-9a879a11ed8d, Sales volume: 9504 Product ID: product-fa976c02-b175-4e82-b53a-8c0df96fe877, Sales volume: 8630 Product ID: product-0624a180-4914-46b9-84d0-9dfbbdaa0da2, Sales volume: 8405 Product ID: product-d0079955-eaea-47b2-845f-5ff05a110a70, Sales volume: 7930 Product ID: product-a53145ef-1db9-4c4d-a029-9324e7f728fe, Sales volume: 7429 Product ID: product-9b1a1fd1-7c3b-4ae8-9fd3-ab6a0bf71cae, Sales volume: 5944 Product ID: product-cf894aee-c1cb-425e-a644-87ff06485eb7, Sales volume: 5252 Product ID: product-8bd78ba8-f2c4-4e5e-b393-60aa738eceae, Sales volume: 4903 Product ID: product-89b64402-c624-4cf1-8532-ae1b4ec4cabc, Sales volume: 4527 Product ID: product-98b85168-9226-43d9-b3cf-ef84e1c3d75f, Sales volume: 3095 Product ID: product-0dda314f-22a7-464b-ab8c-2f8f00823a39, Sales volume: 2425 Product ID: product-de7eb085-9435-4924-b6fa-9e9fe552d5a7, Sales volume: 1694 Product ID: product-9beadc07-aab0-438c-ac5e-bcc72b9d9c36, Sales volume: 1135 Product ID: product-43834316-4aca-4fb2-8d2d-c768513015c5, Sales volume: 256 Best-seller Rankings Top 5 Best-sellers Product ID: product-b290c0d4-e919-4266-8eb5-7ab84b19862d, Sales volume: 18433 Product ID: product-e61a0642-d34f-46f4-a720-ee35940a5e7f, Sales volume: 18334 Product ID: product-ceeab7c3-69a7-4994-afc6-41b7bc463d44, Sales volume: 18196 Product ID: product-f2bdc549-8b3e-4db1-8cd4-a2ddef4f5d97, Sales volume: 17870 Product ID: product-f50ca2de-7fa4-45a3-bf32-23d34ac15a41, Sales volume: 17842
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.