|
@@ -37,6 +37,7 @@ import org.springframework.web.bind.annotation.*;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.text.ParseException;
|
|
import java.text.ParseException;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -124,7 +125,7 @@ public class CommunityArticleController extends BaseController {
|
|
private CommunityReportController communityReportController;
|
|
private CommunityReportController communityReportController;
|
|
|
|
|
|
private static final String CACHE_PREFIX = "article:list:"; // 缓存前缀
|
|
private static final String CACHE_PREFIX = "article:list:"; // 缓存前缀
|
|
- private static final long CACHE_EXPIRE_TIME = 30; // 缓存过期时间(分钟)
|
|
|
|
|
|
+ private static final Integer CACHE_EXPIRE_TIME = 30; // 缓存过期时间(分钟)
|
|
@Autowired
|
|
@Autowired
|
|
private CommunityUserLikeMapper communityUserLikeMapper;
|
|
private CommunityUserLikeMapper communityUserLikeMapper;
|
|
@Autowired
|
|
@Autowired
|
|
@@ -153,9 +154,58 @@ public class CommunityArticleController extends BaseController {
|
|
int pageNum,
|
|
int pageNum,
|
|
int pageSize,
|
|
int pageSize,
|
|
int searchType) {
|
|
int searchType) {
|
|
- // 将参数拼接为唯一键(例如:article:list:userId=1:page=1:searchType=1)
|
|
|
|
- return String.format("%suserId=%d:page=%d:size=%d:searchType=%d",
|
|
|
|
- CACHE_PREFIX, userId, pageNum, pageSize, searchType);
|
|
|
|
|
|
+ StringBuilder keyBuilder = new StringBuilder(CACHE_PREFIX);
|
|
|
|
+
|
|
|
|
+ // 添加用户ID
|
|
|
|
+ keyBuilder.append("userId=").append(userId != null ? userId : 0).append(":");
|
|
|
|
+
|
|
|
|
+ // 添加文章查询条件(如果有)
|
|
|
|
+ if (article != null) {
|
|
|
|
+ if (article.getClassIds() != null && !article.getClassIds().isEmpty()) {
|
|
|
|
+ // 直接使用逗号连接数字,避免使用String.valueOf()可能产生的"[1]"格式
|
|
|
|
+ StringBuilder classIdsBuilder = new StringBuilder();
|
|
|
|
+ boolean first = true;
|
|
|
|
+ for (Long classId : article.getClassIds()) {
|
|
|
|
+ if (!first) {
|
|
|
|
+ classIdsBuilder.append(",");
|
|
|
|
+ }
|
|
|
|
+ classIdsBuilder.append(classId);
|
|
|
|
+ first = false;
|
|
|
|
+ }
|
|
|
|
+ keyBuilder.append("classIds=").append(classIdsBuilder.toString()).append(":");
|
|
|
|
+ }
|
|
|
|
+ if (article.getCircleId() != null) {
|
|
|
|
+ keyBuilder.append("circleId=").append(article.getCircleId()).append(":");
|
|
|
|
+ }
|
|
|
|
+ if (article.getTitle() != null) {
|
|
|
|
+ keyBuilder.append("title=").append(article.getTitle()).append(":");
|
|
|
|
+ }
|
|
|
|
+ // 可以根据需要添加更多条件
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 添加分页和搜索类型信息
|
|
|
|
+ keyBuilder.append("page=").append(pageNum).append(":")
|
|
|
|
+ .append("size=").append(pageSize).append(":")
|
|
|
|
+ .append("searchType=").append(searchType);
|
|
|
|
+
|
|
|
|
+ return keyBuilder.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 清除文章列表相关的所有缓存
|
|
|
|
+ */
|
|
|
|
+ private void clearArticleListCache() {
|
|
|
|
+ try {
|
|
|
|
+ // 获取所有以CACHE_PREFIX开头的缓存键
|
|
|
|
+ Collection<String> keys = redisCache.keys(CACHE_PREFIX + "*");
|
|
|
|
+ if (keys != null && !keys.isEmpty()) {
|
|
|
|
+ // 删除所有匹配的缓存
|
|
|
|
+ redisCache.deleteObject(keys);
|
|
|
|
+ log.info("已清除{}个文章列表缓存", keys.size());
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("清除文章列表缓存失败", e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -172,14 +222,48 @@ public class CommunityArticleController extends BaseController {
|
|
if (userId == null) {
|
|
if (userId == null) {
|
|
userId = SecurityUtils.getUserId();
|
|
userId = SecurityUtils.getUserId();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 生成缓存键
|
|
|
|
+ String cacheKey = generateCacheKey(userId, communityArticle, pageNum, pageSize, searchType);
|
|
|
|
+
|
|
try {
|
|
try {
|
|
|
|
+ // 尝试从缓存中获取数据
|
|
|
|
+ TableDataInfo cachedData = null;
|
|
|
|
+ try {
|
|
|
|
+ cachedData = redisCache.getCacheObject(cacheKey);
|
|
|
|
+ if (cachedData != null) {
|
|
|
|
+ log.info("从缓存中获取文章列表数据, key: {}", cacheKey);
|
|
|
|
+ return cachedData;
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ // 如果从缓存获取数据时出错,记录错误并继续从数据库获取
|
|
|
|
+ log.error("从缓存获取数据失败: {}", e.getMessage(), e);
|
|
|
|
+ // 清除可能损坏的缓存
|
|
|
|
+ try {
|
|
|
|
+ redisCache.deleteObject(cacheKey);
|
|
|
|
+ log.info("已清除可能损坏的缓存: {}", cacheKey);
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ log.error("清除缓存失败: {}", ex.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 缓存中没有数据或获取缓存失败,从数据库查询
|
|
|
|
+ log.info("从数据库中获取文章列表数据");
|
|
rspData = new TableDataInfo();
|
|
rspData = new TableDataInfo();
|
|
List<CommunityArticleVo> list = communityArticleService.selectCommunityArticleList(userId, rspData, communityArticle, pageNum, pageSize, searchType);
|
|
List<CommunityArticleVo> list = communityArticleService.selectCommunityArticleList(userId, rspData, communityArticle, pageNum, pageSize, searchType);
|
|
rspData.setCode(HttpStatus.SUCCESS);
|
|
rspData.setCode(HttpStatus.SUCCESS);
|
|
rspData.setMsg("查询成功");
|
|
rspData.setMsg("查询成功");
|
|
rspData.setRows(list);
|
|
rspData.setRows(list);
|
|
|
|
+
|
|
|
|
+ // 将查询结果存入缓存
|
|
|
|
+ try {
|
|
|
|
+ redisCache.setCacheObject(cacheKey, rspData, CACHE_EXPIRE_TIME, TimeUnit.MINUTES);
|
|
|
|
+ log.info("文章列表数据已缓存, key: {}, 过期时间: {}分钟", cacheKey, CACHE_EXPIRE_TIME);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("缓存数据失败: {}", e.getMessage(), e);
|
|
|
|
+ }
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- log.info(e.getMessage());
|
|
|
|
|
|
+ log.error("获取文章列表失败: {}", e.getMessage(), e);
|
|
throw new ProjectException();
|
|
throw new ProjectException();
|
|
}
|
|
}
|
|
return rspData;
|
|
return rspData;
|
|
@@ -283,6 +367,10 @@ public class CommunityArticleController extends BaseController {
|
|
}
|
|
}
|
|
|
|
|
|
communityArticleService.insertCommunityArticle(communityArticle);
|
|
communityArticleService.insertCommunityArticle(communityArticle);
|
|
|
|
+
|
|
|
|
+ // 清除文章列表缓存,确保新发布的文章能够立即显示
|
|
|
|
+ clearArticleListCache();
|
|
|
|
+ log.info("文章发布成功,已清除文章列表缓存");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.info(e.getMessage());
|
|
log.info(e.getMessage());
|
|
throw new ProjectException();
|
|
throw new ProjectException();
|
|
@@ -319,12 +407,15 @@ public class CommunityArticleController extends BaseController {
|
|
|
|
|
|
communityArticleService.editCommunityArticle(communityArticle);
|
|
communityArticleService.editCommunityArticle(communityArticle);
|
|
|
|
|
|
|
|
+ // 清除文章列表缓存,确保编辑后的文章能够立即显示
|
|
|
|
+ clearArticleListCache();
|
|
|
|
+ log.info("文章编辑成功,已清除文章列表缓存");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// e.printStackTrace();
|
|
// e.printStackTrace();
|
|
log.info(e.getMessage());
|
|
log.info(e.getMessage());
|
|
throw new ProjectException();
|
|
throw new ProjectException();
|
|
}
|
|
}
|
|
- return AjaxResult.success("文章发布成功!");
|
|
|
|
|
|
+ return AjaxResult.success("文章编辑成功!");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -354,6 +445,9 @@ public class CommunityArticleController extends BaseController {
|
|
.set("update_by", SecurityUtils.getUserId())
|
|
.set("update_by", SecurityUtils.getUserId())
|
|
.eq("article_id", id));
|
|
.eq("article_id", id));
|
|
if (result1 && result2 >= 0) {
|
|
if (result1 && result2 >= 0) {
|
|
|
|
+ // 清除文章列表缓存,确保删除的文章不再显示
|
|
|
|
+ clearArticleListCache();
|
|
|
|
+ log.info("文章删除成功,已清除文章列表缓存");
|
|
return success("删除成功!");
|
|
return success("删除成功!");
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|