Code word is not easy, click a concern to see, thank you ~
Background information
Video broadcast room is one of the core of the whole system. In addition to the live video window, the online users, gifts, comments, likes, ranking and other data information in the live broadcast room have high timeliness and interaction, and have very high requirements for system delay, which is very suitable for processing by Redis cache service.
This best practice will show you an example of building a video studio information system using Redis. You’ll learn how to build three types of information:
- Real-time ranking information
- Count class information
- Time line information
Real-time ranking information
Real-time ranking information includes the list of live broadcast online users, the list of all kinds of gifts, and the bullet-screen messages (similar to the list of messages sorted by message dimensions), which are suitable for storage by the sorted set structure in Redis.
Redis sets are implemented using hash tables with null values, so the time complexity of add, delete, change, and check operations on the set is O (1). Each member of an ordered set is associated with a score, which makes sorting and other operations easy. The following illustrates the practical application of ordered set in direct broadcast information system by taking adding and returning barrage messages as an example.
- Unix timestamp+ ms, record user55’s added 5 live barrage:
redis> ZADD user55:_danmu 1523959031601166 message111111111111
(integer) 1
11.16024.14.:3003> ZADD user55:_danmu 1523959031601266 message222222222222
(integer) 1
11.16024.14.:3003> ZADD user55:_danmu 1523959088894232 message33333
(integer) 1
11.16024.14.:3003> ZADD user55:_danmu 1523959090390160 message444444
(integer) 1
11.16024.14.:3003> ZADD user55:_danmu 1523959092951218 message5555
(integer) 1
Copy the code
- Returns the latest 3 barrage messages:
redis> ZREVRANGEBYSCORE user55:_danmu +inf -inf LIMIT 0 3
1) "message5555"
2) "message444444"
3) "message33333"
Copy the code
- Returns the information of three bullets in a specified period:
redis> ZREVRANGEBYSCORE user55:_danmu 1523959088894232 -inf LIMIT 0 3
1) "message33333"
2) "message222222222222"
3) "message111111111111"
Copy the code
Count class information
Count information takes user-related data as an example, including the number of unread messages, followers, fans, and experience values. Such messages are suitable for storing in the hash structure in Redis. For example, the number of concerns can be handled as follows:
redis> HSET user:55 follower 5
(integer) 1
redis> HINCRBY user:55 follower 1 // Focus +1
(integer) 6
redis> HGETALL user:55
1) "follow"
2) "6"
Copy the code
Time line information
Timeline information is a list of information in the dimension of time, typically including anchor news, new posts, etc. This information is arranged in a fixed chronological order and can be stored using lists or ordered lists, as shown in the following example:
redis> LPUSH user:55_recent_activitiy '{datetime: 201804112010, type: publish, title: air, content: refueling}'
(integer) 1
redis> LPUSH user:55_recent_activitiy '{datetime: 201804131910, type: publish, title: leave, content: I'm sorry, today have a pigeon day}'
(integer) 2
redis> LRANGE user:55_recent_activitiy 0 10
1) "{datetime:201804131910,type:publish,title:\xe8\xaf\xb7\xe5\x81\x87\",content:\xe6\x8a\xb1\xe6\xad\x89\xef\xbc\x8c\xe4\x bb\x8a\xe5\xa4\xa9\xe6\x9c\x89\xe4\xba\x8b\xe9\xb8\xbd\xe4\xb8\x80\xe5\xa4\xa9}"
2) "{datetime:201804112010,type:publish,title:\xe5\xbc\x80\xe6\x92\xad\xe5\x95\xa6,content:\xe5\x8a\xa0\xe6\xb2\xb9}"
Copy the code