本周热议功能实现实例
环境准备
- SpringBoot
- Redis
- MySQL
思路
**将前天内发表的文章的评论数量存入Redis,利用Redis中ZSet数据结构的特性,将七天内的博客的评论数存入Redis,然后求并集,通过score的大小顺序来确定热议**。
测试:
## 每天的评论数量存入数据库 格式: zdd day:时间 数量 post:文章ID
127.0.0.1:6379> zadd day:18 10 post:1
(integer) 1
127.0.0.1:6379> zadd day:19 10 post:1
(integer) 1
127.0.0.1:6379> zadd day:20 10 post:1
(integer) 1
127.0.0.1:6379> zadd day:18 6 post:2
(integer) 1
127.0.0.1:6379> zadd day:19 6 post:2
(integer) 1
127.0.0.1:6379> zadd day:20 6 post:2
(integer) 1
127.0.0.1:6379> keys *
1) "day:19"
2) "cart:uid:32"
3) "day:20"
4) "day:18"
127.0.0.1:6379> zrevrange day:18 0 -1 withscores
1) "post:1"
2) "10"
3) "post:2"
4) "6"
## 求并集
127.0.0.1:6379> zunionstore week:rank 3 day:18 day:19 day:20
(integer) 2
127.0.0.1:6379> keys *
1) "day:19"
2) "day:18"
3) "cart:uid:32"
4) "week:rank"
5) "day:20"
127.0.0.1:6379> zrevrange week:rank 0 -1 withscores
1) "post:1"
2) "30"
3) "post:2"
4) "18"
127.0.0.1:6379>
SpringBoot 整合Redis实现本周热议步骤
存入Redis并生成并集核心代码:
/**
* 本周热议
*/
@Override
public void initWeekRank() {
// 获取7天内发布的文章
List<MPost> posts = this.list(new QueryWrapper<MPost>()
.ge("created", DateUtil.offsetDay(new Date(), -7))
.select("id, title, user_id, comment_count, view_count, created")
);
// 初始化文章的总评论数
for (MPost post : posts) {
// 定义ZSet的集合名称: zdd day:rank:20200923 yyyyMMdd
String key = "day:rank:" + DateUtil.format(post.getCreated(), DatePattern.PURE_DATE_FORMAT);
redisUtil.zSet(key, post.getId(), post.getCommentCount());
// 7天后自动过期
long between = DateUtil.between(new Date(), post.getCreated(), DateUnit.DAY);
long expireTime = (7 - between) * 24 * 60 * 60;
redisUtil.expire(key, expireTime);
// 缓存文章的基本信息
this.hashCachPost(post, expireTime);
}
// 做并集
this.zunionWeekRank();
}
// 做并集,合并每日评论的数量
private void zunionWeekRank() {
// 当前时间的key
String currenkey = "day:rank:" + DateUtil.format(new Date(), DatePattern.PURE_DATE_FORMAT);
// 并集后保存的集合
String destkey = "week:rank";
List<String> ortherKeys = new ArrayList<>();
// 循环添加前七天的key
for (int i = -7; i < 0; i++) {
String temp = "day:rank:" +
DateUtil.format(DateUtil.offsetDay(new Date(), i), DatePattern.PURE_DATE_FORMAT);
ortherKeys.add(temp);
}
redisUtil.zUnionAndStore(currenkey, ortherKeys, destkey);
}
// 缓存博客的基本信息
private void hashCachPost(MPost post, long expireTime) {
String key = "rank:post:" + post.getId();
if(! redisUtil.hasKey(key)) {
redisUtil.hset(key, "post:id", post.getId(), expireTime);
redisUtil.hset(key, "post:title", post.getTitle(), expireTime);
redisUtil.hset(key, "post:commentCount", post.getCommentCount(), expireTime);
}
}
获取前7条作为热议:
@Override
public List<Map> getHotPost() throws Exception {
String weekRankKey = "week:rank";
Set<ZSetOperations.TypedTuple> typedTuples = redisUtil.getZSetRank(weekRankKey, 0, 6);
List<Map> hotPosts = new ArrayList<>();
for (ZSetOperations.TypedTuple typedTuple : typedTuples) {
Map<String, Object> map = new HashMap<>();
Object value = typedTuple.getValue(); // post的id
String postKey = "rank:post:" + value;
map.put("id", value);
map.put("title", redisUtil.hget(postKey, "post:title"));
map.put("commentCount", typedTuple.getScore());
hotPosts.add(map);
}
return hotPots;
}