Implementing Bullet-Screen and Social Comments with DCS
Overview
Application Scenario
Scenarios such as bullet-screen comments in videos or live streaming and commenting and replying on a social website require high live efficiency and interactivity. A platform must ensure a very low latency to support such services. Comments are sorted by time in reverse order. If a relational database is adopted, the sorting efficiency becomes lower and lower as comments pile up.
Solution
Using DCS for Redis, a key-value list can be displayed in descending order from multiple dimensions. Take live commenting as an example. Bullet-screen comments can be ordered according to their weighted score calculated using their timestamp and then displayed as sorted sets (zsets). The content can be directly stored as values. Zset can also be applied to social websites. Since the quantity of comments and replies is huge, they require ordered display and local persistence. The primary key ID of a comment can be stored as a value, and the content of the comment is stored in the database and queried with the ID.
Prerequisites
- A DCS instance has been created, and is in the Running state.
- The network between the client server and the DCS instance is connected:
- When the client and the DCS Redis instance are in the same VPC:
By default, networks in a VPC can communicate with each other.
- When the client and the DCS Redis instance are in different VPCs in the same region:
If the client and DCS Redis instance are not in the same VPC, connect them by establishing a VPC peering connection. For details, see Does DCS Support Cross-VPC Access?
- To access a Redis instance of another region on a client
If the client server and the Redis instance are not in the same region, connect the network using Direct Connect. For details, see What Is Direct Connect.
- For public access
For details about how to access a DCS Redis 4.0/5.0/6.0 instance on a client over a public network, see Using Nginx for Public Access to DCS or Using ELB for Public Access to DCS.
- When the client and the DCS Redis instance are in the same VPC:
- You have installed JDK1.8 (or later) and a development tool (Eclipse is used as an example) on the client server, and downloaded the Jedis client.
The development tools and clients mentioned in this document are for example only.
Procedure
- Run Eclipse on the server, choose File > New Project to create a Java project, and import the Jedis client as a library to the project.
- Choose New > Class to create a VideoBulletScreenDemo.java file.
- Copy the following demo code to the VideoBulletScreenDemo.java file.
- Sample code of bullet-screen comments in live streaming
package org.example.task; 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 VideoBulletScreenDemo { static final int MESSAGE_NUM = 30; public static void main(String[] args) { // Address and port for connecting to the Redis instance. Replace them with the actual values. String host = "127.0.0.1"; int port = 6379; Jedis jedisClient = new Jedis(host,port); try { // Password for connecting to the Redis instance. Replace it with the actual value. String authMsg = jedisClient.auth("******"); if (!authMsg.equals("OK")){ System.out.println("AUTH FAILED: " + authMsg); } String key = "Live comment list"; jedisClient.del(key); // Randomly spawn bullets. List<String> messageList = new ArrayList<>(); for (int i = 0; i < MESSAGE_NUM; i++){ messageList.add("message-" + UUID.randomUUID().toString()); } // Timestamp of random spawn. for (int i = 0; i < messageList.size(); i++){ String message = messageList.get(i); int sales = (int)(Math.random()*1000); long time = System.currentTimeMillis() + sales; // Insert as sorted set of Redis. jedisClient.zadd(key,time,message); } System.out.println(" " + key); // Obtain all lists and output in chronological order. Set<Tuple> sortedMessageList = jedisClient.zrangeWithScores(key, 0, -1); for (Tuple message : sortedMessageList){ System.out.println("bullets content: " + message.getElement() + ", sent time: " + Double.valueOf(message.getScore()).longValue()); } System.out.println(); System.out.println(" The latest 5 bullets"); Set<Tuple> sortedTopList = jedisClient.zrevrangeWithScores(key,0,4); for (Tuple product : sortedTopList){ System.out.println("bullets content: " + product.getElement() + ", sent time: " + Double.valueOf(product.getScore()).longValue()); } } catch (Exception e) { e.printStackTrace(); } finally { jedisClient.quit(); jedisClient.close(); } } }
- Sample code of replying to a comment on a social website
package org.example.task; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Set; import java.util.UUID; import redis.clients.jedis.Jedis; import redis.clients.jedis.Tuple; public class SiteCommentsDemo { // Total comments and replies. static final int COMMENT_NUM = 20; public static void main(String[] args) { // Address and port for connecting to the Redis instance. Replace them with the actual values. String host = "127.0.0.1"; int port = 6379; Jedis jedisClient = new Jedis(host,port); try { // Password for connecting to the Redis instance. Replace it with the actual value. String authMsg = jedisClient.auth("******"); if (!authMsg.equals("OK")){ System.out.println("AUTH FAILED: " + authMsg); } String key = "List of replies to comments on a social website"; jedisClient.del(key); HashMap<Integer, Comment> map = new HashMap<>(); // Randomly spawn objects for comment replies. List<Comment> commentList = new ArrayList<>(); for (int i = 0; i < COMMENT_NUM; i++){ Comment comment = new Comment(); comment.setId(i+1); comment.setContent(UUID.randomUUID().toString().substring(0,8)); long time = System.currentTimeMillis(); Thread.sleep(50); comment.setTime(time); // Randomly spawn replies. if (i > 0 && Math.random() < 0.5){ comment.setParentId((int)(Math.random()*i) + 1); } commentList.add(comment); map.put(comment.getId(),comment); // Insert as sorted set of Redis. jedisClient.zadd(key,time,String.valueOf(comment.getId())); } System.out.println(" " + key); // Obtain all lists and output in chronological order. Set<Tuple> sortedCommentList = jedisClient.zrangeWithScores(key, 0, -1); for (Tuple comment : sortedCommentList){ Integer commentId = Integer.valueOf(comment.getElement()); Comment tmpComment = map.get(commentId); System.out.println("comment ID: " + comment.getElement() + " comment parent ID: " + tmpComment.getParentId() + ", comment time: " + Double.valueOf(comment.getScore()).longValue()); } System.out.println(); System.out.println(" The latest 5 replies"); Set<Tuple> sortedTopList = jedisClient.zrevrangeWithScores(key,0,4); for (Tuple comment : sortedTopList){ Integer commentId = Integer.valueOf(comment.getElement()); Comment tmpComment = map.get(commentId); if (tmpComment.getParentId() != null){ System.out.println("comment ID: " + comment.getElement() + " reply:" + tmpComment.getParentId() + " comment content:" + tmpComment.getContent() + ", comment time: " + Double.valueOf(comment.getScore()).longValue()); }else { System.out.println("comment ID: " + comment.getElement() + ", comment time: " + Double.valueOf(comment.getScore()).longValue()); } } } catch (Exception e) { e.printStackTrace(); } finally { jedisClient.quit(); jedisClient.close(); } } /** * comment data object */ static class Comment{ // Comment ID private Integer id; // Comment content private String content; // Comment time private Long time; // Comment parent ID of a reply private Integer parentId; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Long getTime() { return time; } public void setTime(Long time) { this.time = time; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } } }
- Sample code of bullet-screen comments in live streaming
- Configure the connection address, port, and password of the DCS Redis instance in the sample code.
- Compile and run the code.
Operation Result
- Sample code of bullet-screen comments in live streaming:
Live comment list bullets content: message-07f1add5-2f85-4309-9f31-313c860b33dc, sent time: 1686902337377 bullets content: message-2062e817-3145-4d8b-af7f-46f334c8569c, sent time: 1686902337394 bullets content: message-ad36a0ca-e8bd-4883-a091-e12a25c00106, sent time: 1686902337396 bullets content: message-f02f9960-bb57-49ae-b7d8-6bd6d3ad3d14, sent time: 1686902337412 bullets content: message-5ca39948-866e-4e54-a469-f958cae843f6, sent time: 1686902337457 bullets content: message-5cc8b4ba-da61-4d01-9625-cf2e7337ef10, sent time: 1686902337489 bullets content: message-15378516-18ce-4da7-bd3c-35c57dd65602, sent time: 1686902337495 bullets content: message-1b280525-53e5-4fc6-a3e7-fb8e71eef85e, sent time: 1686902337540 bullets content: message-adf876d1-e747-414e-92a2-397fc329bd58, sent time: 1686902337541 bullets content: message-1d8d7901-164f-4dd4-abb4-6f2345164b0e, sent time: 1686902337582 bullets content: message-fb35b1b4-277a-48bf-b22b-80070aae8475, sent time: 1686902337667 bullets content: message-973b1b03-bf95-44d8-ab91-0c317b2d61b3, sent time: 1686902337755 bullets content: message-1481f883-757d-47f7-b8c0-df024d6e64a4, sent time: 1686902337770 bullets content: message-b79292ca-2409-43fb-aaf0-e33f3b9d9c8d, sent time: 1686902337820 bullets content: message-66b0e955-d509-4475-9ae5-12fb86cf9596, sent time: 1686902337844 bullets content: message-12b6d15a-037a-47ee-8294-8625d202c0a0, sent time: 1686902337907 bullets content: message-fbc06323-da2a-44b8-874b-d2cf1a737064, sent time: 1686902337927 bullets content: message-7a0f787c-aff1-422f-9e62-4beda0cd5914, sent time: 1686902337977 bullets content: message-8ba5e4e0-22af-4f80-90a6-35062967e0fd, sent time: 1686902337992 bullets content: message-fa9e1169-e918-4141-9805-87edcf84c379, sent time: 1686902338000 bullets content: message-5d17be15-ba2e-461f-aba5-65c20c21d313, sent time: 1686902338059 bullets content: message-dcedc840-1be7-496a-b781-5b79c2091fe5, sent time: 1686902338067 bullets content: message-9e39eb28-6629-4d4c-8970-2acdc0e81a5c, sent time: 1686902338102 bullets content: message-030b11fe-c258-4ca2-ac82-5e6ca1eb688f, sent time: 1686902338211 bullets content: message-93322018-a987-47ba-8093-3937dddda97d, sent time: 1686902338242 bullets content: message-bc04a9b0-ec83-4a24-83f6-0a4f25ee8896, sent time: 1686902338281 bullets content: message-c6dd96d0-c938-41e4-b5d8-6275fdf83050, sent time: 1686902338290 bullets content: message-12b70173-1b86-4370-a7ea-dc0ade135422, sent time: 1686902338312 bullets content: message-a39c2ef8-8167-4945-b60d-355db6c69005, sent time: 1686902338318 bullets content: message-2c3bf2fb-5298-472c-958c-c4b53d734e89, sent time: 1686902338326 The latest 5 bullets bullets content: message-2c3bf2fb-5298-472c-958c-c4b53d734e89, sent time: 1686902338326 bullets content: message-a39c2ef8-8167-4945-b60d-355db6c69005, sent time: 1686902338318 bullets content: message-12b70173-1b86-4370-a7ea-dc0ade135422, sent time: 1686902338312 bullets content: message-c6dd96d0-c938-41e4-b5d8-6275fdf83050, sent time: 1686902338290 bullets content: message-bc04a9b0-ec83-4a24-83f6-0a4f25ee8896, sent time: 1686902338281 Process finished with exit code 0
- Sample code of replying to a comment on a social website:
List of replies to comments on a social website comment id: 1 comment parentid: null, comment time: 1684745729506 comment id: 2 comment parentid: 1, comment time: 1684745729567 comment id: 3 comment parentid: null, comment time: 1684745729630 comment id: 4 comment parentid: 3, comment time: 1684745729692 comment id: 5 comment parentid: 3, comment time: 1684745729755 comment id: 6 comment parentid: 4, comment time: 1684745729819 comment id: 7 comment parentid: null, comment time: 1684745729879 comment id: 8 comment parentid: 6, comment time: 1684745729942 comment id: 9 comment parentid: null, comment time: 1684745730006 comment id: 10 comment parentid: 7, comment time: 1684745730069 comment id: 11 comment parentid: null, comment time: 1684745730132 comment id: 12 comment parentid: 9, comment time: 1684745730194 comment id: 13 comment parentid: null, comment time: 1684745730256 comment id: 14 comment parentid: 9, comment time: 1684745730320 comment id: 15 comment parentid: null, comment time: 1684745730382 comment id: 16 comment parentid: 1, comment time: 1684745730444 comment id: 17 comment parentid: null, comment time: 1684745730508 comment id: 18 comment parentid: 12, comment time: 1684745730570 comment id: 19 comment parentid: null, comment time: 1684745730631 comment id: 20 comment parentid: 12, comment time: 1684745730694 The latest 5 replies comment id: 20 reply:12 comment content:877ba7f1, comment time: 1684745730694 comment id: 19, comment time: 1684745730631 comment id: 18 reply:12 comment content:b29f2077, comment time: 1684745730570 comment id: 17, comment time: 1684745730508 comment id: 16 reply:1 comment content:9f31200e, comment time: 1684745730444
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot