|
@@ -6,11 +6,13 @@ import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
import com.ruoyi.common.annotation.Anonymous;
|
|
|
+import com.ruoyi.common.constant.CacheConstants;
|
|
|
import com.ruoyi.common.constant.HttpStatus;
|
|
|
import com.ruoyi.common.core.controller.BaseController;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
import com.ruoyi.common.core.page.TableDataInfo;
|
|
|
+import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.core.text.Convert;
|
|
|
import com.ruoyi.common.exception.user.ProjectException;
|
|
|
import com.ruoyi.common.utils.*;
|
|
@@ -33,6 +35,7 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -142,6 +145,9 @@ public class CommunityArticleController extends BaseController {
|
|
|
@Autowired
|
|
|
private CommunityImageProcessService communityImageProcessService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
/**
|
|
|
* 获取文章列表信息
|
|
|
*/
|
|
@@ -345,12 +351,33 @@ public class CommunityArticleController extends BaseController {
|
|
|
@ApiOperation("获取文章分类列表")
|
|
|
//@Anonymous
|
|
|
public AjaxResult getClassList() {
|
|
|
-
|
|
|
- List<CommunityClass> communityClasses = null;
|
|
|
- //获取用户信息
|
|
|
- CommunityUserInfoVo communityUserInfoVo = null;
|
|
|
try {
|
|
|
Long userId = SecurityUtils.getLoginUser().getUserId();
|
|
|
+
|
|
|
+ // 构建用户特定的缓存key
|
|
|
+ String userClassCacheKey = CacheConstants.COMMUNITY_ARTICLE_CLASS + ":" + userId;
|
|
|
+
|
|
|
+ // 尝试从缓存中获取用户特定的排序列表
|
|
|
+ List<CommunityClass> sortedCommunityClasses = redisCache.getCacheObject(userClassCacheKey);
|
|
|
+ if (sortedCommunityClasses != null) {
|
|
|
+ log.info("从缓存中获取用户特定的文章分类列表");
|
|
|
+ return AjaxResult.success(sortedCommunityClasses);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试从缓存中获取所有分类列表
|
|
|
+ List<CommunityClass> allCommunityClasses = redisCache.getCacheObject(CacheConstants.COMMUNITY_ARTICLE_CLASS);
|
|
|
+ if (allCommunityClasses == null) {
|
|
|
+ // 缓存中没有数据,从数据库查询
|
|
|
+ log.info("从数据库中获取文章分类列表");
|
|
|
+ allCommunityClasses = communityClassMapper.selectList(new QueryWrapper<CommunityClass>().orderByAsc("sort"));
|
|
|
+
|
|
|
+ // 将查询结果存入缓存,设置24小时过期
|
|
|
+ redisCache.setCacheObject(CacheConstants.COMMUNITY_ARTICLE_CLASS, allCommunityClasses, 7, TimeUnit.DAYS);
|
|
|
+ } else {
|
|
|
+ log.info("从缓存中获取所有文章分类列表");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取用户信息
|
|
|
String classId = communityArticleService.selectCommunityUserInfoById(userId, true).getClassId();
|
|
|
|
|
|
if (classId != null && !classId.isEmpty()) {
|
|
@@ -359,14 +386,11 @@ public class CommunityArticleController extends BaseController {
|
|
|
.map(Long::parseLong)
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
- // 假设 allCommunityClasses 是从数据库查询出来的结果
|
|
|
- List<CommunityClass> allCommunityClasses = communityClassMapper.selectList(new QueryWrapper<CommunityClass>().orderByAsc("sort"));
|
|
|
-
|
|
|
// 将 allCommunityClasses 按照 sortOrder 分组并排序
|
|
|
+ Map<Long, CommunityClass> classMap = allCommunityClasses.stream()
|
|
|
+ .collect(Collectors.toMap(CommunityClass::getId, c -> c));
|
|
|
|
|
|
- Map<Long, CommunityClass> classMap = allCommunityClasses.stream().collect(Collectors.toMap(CommunityClass::getId, c -> c));
|
|
|
-
|
|
|
- List<CommunityClass> sortedCommunityClasses = new ArrayList<>();
|
|
|
+ sortedCommunityClasses = new ArrayList<>();
|
|
|
// 按 sortOrder 先添加在列表中的元素
|
|
|
for (Long order : sortOrder) {
|
|
|
if (classMap.containsKey(order)) {
|
|
@@ -379,17 +403,20 @@ public class CommunityArticleController extends BaseController {
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
sortedCommunityClasses.addAll(remainingClasses);
|
|
|
+
|
|
|
+ // 将用户特定的排序列表存入缓存,设置1小时过期(用户排序可能会变化,所以过期时间短一些)
|
|
|
+ redisCache.setCacheObject(userClassCacheKey, sortedCommunityClasses, 1, TimeUnit.HOURS);
|
|
|
+
|
|
|
return AjaxResult.success(sortedCommunityClasses);
|
|
|
} else {
|
|
|
- communityClasses = communityClassMapper.selectList(new QueryWrapper<CommunityClass>().orderByAsc("sort"));
|
|
|
-
|
|
|
+ // 将所有分类列表作为用户特定的排序列表存入缓存
|
|
|
+ redisCache.setCacheObject(userClassCacheKey, allCommunityClasses, 1, TimeUnit.HOURS);
|
|
|
+ return AjaxResult.success(allCommunityClasses);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- //e.printStackTrace();
|
|
|
log.info(e.getMessage());
|
|
|
throw new ProjectException();
|
|
|
}
|
|
|
- return AjaxResult.success(communityClasses);
|
|
|
}
|
|
|
|
|
|
|