From b0a09142aefab671818f83341d8b294a3b92020d Mon Sep 17 00:00:00 2001
From: Xubx <1827135378@qq.com>
Date: Mon, 2 Dec 2024 19:11:00 +0800
Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E9=98=85=E5=88=86=E7=B1=BB=E3=80=81?=
=?UTF-8?q?=E5=85=B4=E8=B6=A3=E6=8E=A8=E9=80=81=E3=80=81=E7=83=AD=E5=BA=A6?=
=?UTF-8?q?=E6=8E=92=E8=A1=8C=E6=A6=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Springboot_01Demo.iml => XubxBlog.iml | 2 +-
.../Springboot01DemoApplication.java | 2 +
.../xubx/springboot_01demo/Task/DataSync.java | 116 +++++++++--
.../com/xubx/springboot_01demo/Task/Test.java | 17 --
.../WebSocket/WebSocketServer.java | 5 +-
.../configuration/AsyncConfig.java | 29 +++
.../configuration/constantConfiguration.java | 6 +
.../controller/BlogsController.java | 60 +++++-
.../controller/CommentController.java | 4 +-
.../controller/MessagesController.java | 5 +-
.../controller/RelationshipController.java | 6 +-
.../controller/UserController.java | 96 ++++++++-
.../dto/FriendsRequestDto.java | 8 -
.../{ => entity}/dto/FriendsDto.java | 2 +-
.../{ => entity}/dto/RelationshipDto.java | 2 +-
.../{ => entity}/dto/SendMesDto.java | 2 +-
.../{ => entity}/dto/blog/AddBlogDto.java | 14 +-
.../{ => entity}/dto/blog/CollectBlogDto.java | 2 +-
.../dto/blog/GetUserBlogsDto.java | 2 +-
.../dto/user/EditMaterialDto.java | 2 +-
.../entity/dto/user/FollowUserDto.java | 25 +++
.../dto/user/NewFavoritesDto.java | 2 +-
.../entity/dto/user/UserSubscription.java | 15 ++
.../{ => entity}/pojo/BlogLike.java | 10 +-
.../{ => entity}/pojo/Blogs.java | 15 +-
.../{ => entity}/pojo/Comment.java | 5 +-
.../{ => entity}/pojo/Messages.java | 4 +-
.../{ => entity}/pojo/Relationship.java | 2 +-
.../springboot_01demo/entity/pojo/TopK.java | 45 +++++
.../{ => entity}/pojo/User.java | 2 +-
.../{ => entity}/pojo/UserFavorite.java | 4 +-
.../{ => entity}/pojo/UserFavoriteBlog.java | 4 +-
.../entity/pojo/UserFollow.java | 26 +++
.../{ => entity}/vo/CommentVo.java | 2 +-
.../{ => entity}/vo/GetBlogDetailVo.java | 2 +-
.../springboot_01demo/entity/vo/HotBlog.java | 65 ++++++
.../{ => entity}/vo/UserListVo.java | 2 +-
.../entity/vo/UserModelVo.java | 15 ++
.../{ => entity}/vo/UserVo.java | 2 +-
.../{ => entity}/vo/getUserInfoVo.java | 2 +-
.../{ => entity}/vo/historyMessagesVo.java | 2 +-
.../mapper/BlogLikeMapper.java | 3 +-
.../springboot_01demo/mapper/BlogsMapper.java | 14 +-
.../mapper/CommentMapper.java | 4 +-
.../mapper/MessagesMapper.java | 4 +-
.../mapper/RelationshipMapper.java | 6 +-
.../mapper/UserFavoriteBlogMapper.java | 2 +-
.../mapper/UserFavoriteMapper.java | 2 +-
.../mapper/UserFollowMapper.java | 7 +
.../springboot_01demo/mapper/UserMapper.java | 9 +-
.../mapper/UserSubscriptionMapper.java | 10 +
.../service/BlogService.java | 41 +++-
.../service/CommentService.java | 4 +-
.../service/InterestPushService.java | 45 +++++
.../service/MessagesService.java | 4 +-
.../service/RelationshipService.java | 5 +-
.../service/UserService.java | 40 +++-
.../service/UserSubscriptionService.java | 15 ++
.../service/impl/BlogsServiceImpl.java | 94 ++++++++-
.../service/impl/CommentServiceImpl.java | 7 +-
.../service/impl/InterestPushServiceImpl.java | 189 ++++++++++++++++++
.../service/impl/MessagesServiceImpl.java | 6 +-
.../service/impl/RelationshipServiceImpl.java | 9 +-
.../service/impl/UserServiceImpl.java | 159 +++++++++++++--
.../impl/UserSubscriptionServiceImpl.java | 27 +++
.../xubx/springboot_01demo/sms/SMSsend.java | 60 ------
.../xubx/springboot_01demo/sms/Sample.java | 81 --------
.../springboot_01demo/utils/api/Result.java | 3 -
.../utils/token/TokenGenerate.java | 2 +-
src/main/resources/mapper/BlogLikeMapper.xml | 3 +
src/main/resources/mapper/BlogsMapper.xml | 17 +-
src/main/resources/mapper/CommentMapper.xml | 4 +-
src/main/resources/mapper/MessagesMapper.xml | 6 +-
.../resources/mapper/RelationshipMapper.xml | 12 +-
.../resources/mapper/UserFollowMapper.xml | 6 +
src/main/resources/mapper/UserMapper.xml | 17 +-
.../mapper/UserSubscriptionMappser.xml | 9 +
src/test/java/test.java | 34 ----
78 files changed, 1198 insertions(+), 395 deletions(-)
rename Springboot_01Demo.iml => XubxBlog.iml (61%)
delete mode 100644 src/main/java/com/xubx/springboot_01demo/Task/Test.java
create mode 100644 src/main/java/com/xubx/springboot_01demo/configuration/AsyncConfig.java
delete mode 100644 src/main/java/com/xubx/springboot_01demo/dto/FriendsRequestDto.java
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/dto/FriendsDto.java (95%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/dto/RelationshipDto.java (93%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/dto/SendMesDto.java (95%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/dto/blog/AddBlogDto.java (69%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/dto/blog/CollectBlogDto.java (83%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/dto/blog/GetUserBlogsDto.java (85%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/dto/user/EditMaterialDto.java (82%)
create mode 100644 src/main/java/com/xubx/springboot_01demo/entity/dto/user/FollowUserDto.java
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/dto/user/NewFavoritesDto.java (91%)
create mode 100644 src/main/java/com/xubx/springboot_01demo/entity/dto/user/UserSubscription.java
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/pojo/BlogLike.java (65%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/pojo/Blogs.java (84%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/pojo/Comment.java (94%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/pojo/Messages.java (95%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/pojo/Relationship.java (97%)
create mode 100644 src/main/java/com/xubx/springboot_01demo/entity/pojo/TopK.java
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/pojo/User.java (96%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/pojo/UserFavorite.java (93%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/pojo/UserFavoriteBlog.java (86%)
create mode 100644 src/main/java/com/xubx/springboot_01demo/entity/pojo/UserFollow.java
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/vo/CommentVo.java (98%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/vo/GetBlogDetailVo.java (96%)
create mode 100644 src/main/java/com/xubx/springboot_01demo/entity/vo/HotBlog.java
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/vo/UserListVo.java (96%)
create mode 100644 src/main/java/com/xubx/springboot_01demo/entity/vo/UserModelVo.java
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/vo/UserVo.java (94%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/vo/getUserInfoVo.java (96%)
rename src/main/java/com/xubx/springboot_01demo/{ => entity}/vo/historyMessagesVo.java (96%)
create mode 100644 src/main/java/com/xubx/springboot_01demo/mapper/UserFollowMapper.java
create mode 100644 src/main/java/com/xubx/springboot_01demo/mapper/UserSubscriptionMapper.java
create mode 100644 src/main/java/com/xubx/springboot_01demo/service/InterestPushService.java
create mode 100644 src/main/java/com/xubx/springboot_01demo/service/UserSubscriptionService.java
create mode 100644 src/main/java/com/xubx/springboot_01demo/service/impl/InterestPushServiceImpl.java
create mode 100644 src/main/java/com/xubx/springboot_01demo/service/impl/UserSubscriptionServiceImpl.java
delete mode 100644 src/main/java/com/xubx/springboot_01demo/sms/SMSsend.java
delete mode 100644 src/main/java/com/xubx/springboot_01demo/sms/Sample.java
create mode 100644 src/main/resources/mapper/UserFollowMapper.xml
create mode 100644 src/main/resources/mapper/UserSubscriptionMappser.xml
delete mode 100644 src/test/java/test.java
diff --git a/Springboot_01Demo.iml b/XubxBlog.iml
similarity index 61%
rename from Springboot_01Demo.iml
rename to XubxBlog.iml
index 93f251c..88093bd 100644
--- a/Springboot_01Demo.iml
+++ b/XubxBlog.iml
@@ -2,7 +2,7 @@
-
+
\ No newline at end of file
diff --git a/src/main/java/com/xubx/springboot_01demo/Springboot01DemoApplication.java b/src/main/java/com/xubx/springboot_01demo/Springboot01DemoApplication.java
index 762c782..9efd06a 100644
--- a/src/main/java/com/xubx/springboot_01demo/Springboot01DemoApplication.java
+++ b/src/main/java/com/xubx/springboot_01demo/Springboot01DemoApplication.java
@@ -4,6 +4,7 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@@ -12,6 +13,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@MapperScan("com.xubx.springboot_01demo.mapper")
@EnableScheduling //开启定时任务
@EnableSwagger2 //开启Swagger2
+@EnableAsync // 开启异步任务
public class Springboot01DemoApplication {
public static void main(String[] args) {
diff --git a/src/main/java/com/xubx/springboot_01demo/Task/DataSync.java b/src/main/java/com/xubx/springboot_01demo/Task/DataSync.java
index add3617..c2913ac 100644
--- a/src/main/java/com/xubx/springboot_01demo/Task/DataSync.java
+++ b/src/main/java/com/xubx/springboot_01demo/Task/DataSync.java
@@ -6,40 +6,47 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xubx.springboot_01demo.configuration.constantConfiguration;
+import com.xubx.springboot_01demo.entity.pojo.TopK;
+import com.xubx.springboot_01demo.entity.vo.HotBlog;
import com.xubx.springboot_01demo.mapper.BlogLikeMapper;
import com.xubx.springboot_01demo.mapper.BlogsMapper;
import com.xubx.springboot_01demo.mapper.UserFavoriteBlogMapper;
-import com.xubx.springboot_01demo.pojo.BlogLike;
-import com.xubx.springboot_01demo.pojo.Blogs;
-import com.xubx.springboot_01demo.pojo.UserFavoriteBlog;
+import com.xubx.springboot_01demo.entity.pojo.BlogLike;
+import com.xubx.springboot_01demo.entity.pojo.Blogs;
+import com.xubx.springboot_01demo.entity.pojo.UserFavoriteBlog;
+import com.xubx.springboot_01demo.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
+import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
@Component
@Slf4j
public class DataSync {
@Resource
- RedisTemplate redisTemplate;
+ private RedisTemplate redisTemplate;
@Resource
- BlogsMapper blogsMapper;
+ private BlogsMapper blogsMapper;
@Resource
- BlogLikeMapper blogLikeMapper;
+ private BlogLikeMapper blogLikeMapper;
@Resource
- UserFavoriteBlogMapper userFavoriteBlogMapper;
+ private UserFavoriteBlogMapper userFavoriteBlogMapper;
@Autowired
- ObjectMapper objectMapper;
+ private BlogService blogService;
+ @Autowired
+ private ObjectMapper objectMapper;
+
/**
* 博客浏览数的数据同步 ,每小时执行一次
@@ -97,6 +104,7 @@ public class DataSync {
try {
Set likeCountKeys = redisTemplate.keys(constantConfiguration.BLOG_LIKE_COUNT + "*");
if (likeCountKeys != null) {
+ likeCountKeys = likeCountKeys.stream().filter(key -> !key.endsWith(":expired")).collect(Collectors.toSet());
for (String key : likeCountKeys) {
String blogIdStr = key.split(":")[2];
String likeCountExpireKey = constantConfiguration.BLOG_LIKE_COUNT + blogIdStr + ":expired";
@@ -139,6 +147,7 @@ public class DataSync {
try {
Set likeUserKeys = redisTemplate.keys(constantConfiguration.BLOG_LIKE_HASH + "*");
if (likeUserKeys != null) {
+ likeUserKeys = likeUserKeys.stream().filter(key -> !key.endsWith(":expired")).collect(Collectors.toSet());
for (String key : likeUserKeys) {
String blogIdStr = key.split(":")[2];
String likeUserExpireKey = constantConfiguration.BLOG_LIKE_HASH + blogIdStr + ":expired";
@@ -160,10 +169,10 @@ public class DataSync {
if (blogLike == null) {
BlogLike newBlogLike = new BlogLike();
- newBlogLike.setBlogId(Integer.valueOf(blogIdStr));
- newBlogLike.setUserId(Integer.valueOf(userId));
+ newBlogLike.setBlogId(Long.valueOf(blogIdStr));
+ newBlogLike.setUserId(Long.valueOf(userId));
newBlogLike.setCreatedTime(Timestamp.valueOf(LocalDateTime.now()));
- blogLikeMapper.insert(newBlogLike);
+ blogLikeMapper.addData(newBlogLike);
}
} else if ("false".equals(likeStatus)) {
// 用户取消点赞 则删除记录
@@ -190,7 +199,7 @@ public class DataSync {
/**
* 博客收藏数的数据同步定时任务
*/
- @Scheduled(cron = "0 * * * * ?") //TODO 每小时执行一次
+ @Scheduled(cron = "0 * * * * ?") //TODO 每小时执行一次,需要隔开,不能同一时间全部执行
public void collectCountSync() {
log.info("博客收藏数的数据同步定时任务执行");
String lockKey = constantConfiguration.BLOG_COLLECT_COUNT_SYNC_LOCK;
@@ -200,6 +209,7 @@ public class DataSync {
try {
Set collectCountKeys = redisTemplate.keys(constantConfiguration.BLOG_COLLECT_COUNT + "*");
if (collectCountKeys != null) {
+ collectCountKeys = collectCountKeys.stream().filter(key -> !key.endsWith(":expired")).collect(Collectors.toSet());
for (String key : collectCountKeys) {
String blogIdStr = key.split(":")[2];
String collectCountExpireKey = constantConfiguration.BLOG_COLLECT_COUNT + blogIdStr + ":expired";
@@ -245,6 +255,7 @@ public class DataSync {
// TODO 应该是同时获取到了逻辑过期标志的Key,需要进行去除
Set collectUserKeys = redisTemplate.keys(constantConfiguration.BLOG_COLLECT_HASH + "*");
if (collectUserKeys != null) {
+ collectUserKeys = collectUserKeys.stream().filter(key -> !key.contains(":expired")).collect(Collectors.toSet());
for (String key : collectUserKeys) {
String favoriteIdStr = key.split(":")[2];
String collectUserExpireKey = constantConfiguration.BLOG_COLLECT_HASH + favoriteIdStr + ":expired";
@@ -291,4 +302,77 @@ public class DataSync {
log.warn("无法获取分布式锁,跳过此次收藏用户Hash集合的数据同步任务");
}
}
+
+ /**
+ * 热度排行榜,全表分片扫描
+ */
+ @Scheduled(cron = "0 * * * * ?") //TODO 每小时执行一次
+ public void hotRank() {
+ log.info("热度排行榜刷新定时任务执行");
+
+ final TopK topK = new TopK(10);
+ long limit = 1000;
+ long id = 0;
+
+ List blogs = blogService.list(new LambdaQueryWrapper()
+ .select(Blogs::getId, Blogs::getTitle, Blogs::getLikeCount, Blogs::getViewCount, Blogs::getCollectCount, Blogs::getCommentCount,
+ Blogs::getShareCount, Blogs::getCreatedTime).gt(Blogs::getId, id)
+ .eq(Blogs::getStatus, "公开")
+ .last("limit " + limit));
+
+ while (!ObjectUtils.isEmpty(blogs)) {
+ for (Blogs blog : blogs) {
+ Long likeCount = blog.getLikeCount();
+ Double viewCount = blog.getViewCount() * 0.8;
+ Double collectCount = blog.getCollectCount() * 1.5;
+ Long commentCount = blog.getCommentCount();
+ Long shareCount = blog.getShareCount();
+ final Date date = new Date();
+ long decayTime = date.getTime() - blog.getCreatedTime().getTime();
+ // 防止出现热度完全相同的情况,加入0.1-1.0之间的随机小数
+ final double v = Math.random() * 0.9 + 0.1;
+ final double hot = hot(likeCount + viewCount + collectCount + commentCount + shareCount + v, TimeUnit.MILLISECONDS.toDays(decayTime));
+ final HotBlog hotBlog = new HotBlog(blog.getId(), hot, blog.getTitle());
+
+ // add过程中,已存入热度最大的十个博客
+ topK.add(hotBlog);
+ }
+ id = blogs.get(blogs.size() - 1).getId();
+ blogs = blogService.list(new LambdaQueryWrapper().gt(Blogs::getId, id).eq(Blogs::getStatus, "公开").last("limit " + limit));
+ }
+ // 通过redis管道进行数据更新
+ final byte[] key = constantConfiguration.HOT_RANK.getBytes();
+ final List hotBlogs = topK.get();// 已从大到小排序
+ final Double minHot = hotBlogs.get(hotBlogs.size() - 1).getHot();
+ redisTemplate.executePipelined((RedisCallback