Skip to content

Commit

Permalink
feat: 将管理页首屏数据缓存Redis中,加快访问速度
Browse files Browse the repository at this point in the history
  • Loading branch information
moxi624 committed Sep 23, 2020
1 parent 8df24cb commit e33ab80
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public String init() {
public String getVisitByWeek() {

Map<String, Object> visitByWeek = webVisitService.getVisitByWeek();

return ResultUtil.result(SysConf.SUCCESS, visitByWeek);
}

Expand All @@ -75,7 +74,6 @@ public String getVisitByWeek() {
public String getBlogCountByTag() {

List<Map<String, Object>> blogCountByTag = blogService.getBlogCountByTag();

return ResultUtil.result(SysConf.SUCCESS, blogCountByTag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,30 @@ public class BaseRedisConf {
*/
public final static String WEB_CONFIG = "WEB_CONFIG";

/**
* 首页展示
*/
public final static String DASHBOARD = "DASHBOARD";

/**
* 一周访问量
*/
public final static String WEEK_VISIT = "WEEK_VISIT";

/**
* 博客标签下包含的博客数量
*/
public final static String BLOG_COUNT_BY_TAG = "BLOG_COUNT_BY_TAG";

/**
* 博客分类下包含的博客数量
*/
public final static String BLOG_COUNT_BY_SORT = "BLOG_COUNT_BY_SORT";

/**
* 全年博客贡献数
*/
public final static String BLOG_CONTRIBUTE_COUNT = "BLOG_CONTRIBUTE_COUNT";


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* 博客监听器(用于更新Redis和索引)
*
* @author [email protected]
* @author 陌溪
* @date 2018年11月3日下午12:53:23
*/
@Component
Expand All @@ -35,23 +35,23 @@ public class BlogListener {
private SearchFeignClient searchFeignClient;

// TODO 在这里同时需要对Redis和Solr进行操作,同时利用MQ来保证数据一致性

@RabbitListener(queues = "mogu.blog")
public void updateRedis(Map<String, String> map) {

if (map != null) {

String comment = map.get(SysConf.COMMAND);

String uid = map.get(SysConf.BLOG_UID);

//从Redis清空对应的数据
redisUtil.set(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + Constants.NUM_ONE, "");
redisUtil.set(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + Constants.NUM_TWO, "");
redisUtil.set(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + Constants.NUM_THREE, "");
redisUtil.set(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + Constants.NUM_FOUR, "");
redisUtil.set(RedisConf.HOT_BLOG, "");
redisUtil.set(RedisConf.NEW_BLOG, "");
redisUtil.delete(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + Constants.NUM_ONE);
redisUtil.delete(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + Constants.NUM_TWO);
redisUtil.delete(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + Constants.NUM_THREE);
redisUtil.delete(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + Constants.NUM_FOUR);
redisUtil.delete(RedisConf.HOT_BLOG);
redisUtil.delete(RedisConf.NEW_BLOG);
redisUtil.delete(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_CONTRIBUTE_COUNT);
redisUtil.delete(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_COUNT_BY_SORT);
redisUtil.delete(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_COUNT_BY_TAG);

switch (comment) {
case SysConf.DELETE_BATCH: {
Expand Down Expand Up @@ -128,7 +128,7 @@ private void updateSearch(Map<String, String> map) {
String year = list[0];
String month = list[1];
String key = year + "年" + month + "月";
redisUtil.set(RedisConf.BLOG_SORT_BY_MONTH + Constants.SYMBOL_COLON + key, "");
redisUtil.delete(RedisConf.BLOG_SORT_BY_MONTH + Constants.SYMBOL_COLON + key);
String jsonResult = redisUtil.get(RedisConf.MONTH_SET);
ArrayList<String> monthSet = (ArrayList<String>) JsonUtils.jsonArrayToArrayList(jsonResult);
Boolean haveMonth = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ public interface BlogService extends SuperService<Blog> {

/**
* 通过标签获取博客数目
*
* @author Administrator
* @date 2019年11月27日13:14:34
*/
public List<Map<String, Object>> getBlogCountByBlogSort();

Expand Down Expand Up @@ -183,9 +180,21 @@ public interface BlogService extends SuperService<Blog> {
public String uploadLocalBlog(List<MultipartFile> filedatas) throws IOException;

/**
* mogu-web端使用的接口
* 删除和博客分类有关的Redis缓存
*/
public void deleteRedisByBlogSort();

/**
* 删除和博客标签有关的Redis缓存
*/
public void deleteRedisByBlogTag();

/**
* 删除和博客有关的Redis缓存
*/
public void deleteRedisByBlog();

//========================mogu-web使用==========================
/**
* 通过推荐等级获取博客Page
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,15 @@ public Integer getBlogCount(Integer status) {

@Override
public List<Map<String, Object>> getBlogCountByTag() {
// 从Redis中获取标签下包含的博客数量
String jsonArrayList = redisUtil.get(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_COUNT_BY_TAG);
if (StringUtils.isNotEmpty(jsonArrayList)) {
ArrayList jsonList = JsonUtils.jsonArrayToArrayList(jsonArrayList);
return jsonList;
}

List<Map<String, Object>> blogCoutByTagMap = blogMapper.getBlogCountByTag();

Map<String, Integer> tagMap = new HashMap<>();

for (Map<String, Object> item : blogCoutByTagMap) {
String tagUid = String.valueOf(item.get(SQLConf.TAG_UID));
// java.lang.Number是Integer,Long的父类
Expand Down Expand Up @@ -353,7 +357,7 @@ public List<Map<String, Object>> getBlogCountByTag() {
}
}

List<Map<String, Object>> resultMap = new ArrayList<>();
List<Map<String, Object>> resultList = new ArrayList<>();
for (Map.Entry<String, Integer> entry : tagMap.entrySet()) {
String tagUid = entry.getKey();
if (tagEntityMap.get(tagUid) != null) {
Expand All @@ -363,17 +367,24 @@ public List<Map<String, Object>> getBlogCountByTag() {
itemResultMap.put(SysConf.TAG_UID, tagUid);
itemResultMap.put(SysConf.NAME, tagName);
itemResultMap.put(SysConf.VALUE, count);
resultMap.add(itemResultMap);
resultList.add(itemResultMap);
}
}

return resultMap;

// 将 每个标签下文章数目 存入到Redis【过期时间2小时】
if(resultList.size() > 0) {
redisUtil.setEx(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_COUNT_BY_TAG, JsonUtils.objectToJson(resultList), 2, TimeUnit.HOURS);
}
return resultList;
}

@Override
public List<Map<String, Object>> getBlogCountByBlogSort() {

// 从Redis中获取博客分类下包含的博客数量
String jsonArrayList = redisUtil.get(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_COUNT_BY_SORT);
if (StringUtils.isNotEmpty(jsonArrayList)) {
ArrayList jsonList = JsonUtils.jsonArrayToArrayList(jsonArrayList);
return jsonList;
}
List<Map<String, Object>> blogCoutByBlogSortMap = blogMapper.getBlogCountByBlogSort();
Map<String, Integer> blogSortMap = new HashMap<>();
for (Map<String, Object> item : blogCoutByBlogSortMap) {
Expand Down Expand Up @@ -403,44 +414,46 @@ public List<Map<String, Object>> getBlogCountByBlogSort() {
}
}

List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
for (Map.Entry<String, Integer> entry : blogSortMap.entrySet()) {

String blogSortUid = entry.getKey();

if (blogSortEntityMap.get(blogSortUid) != null) {
String blogSortName = blogSortEntityMap.get(blogSortUid);
Integer count = entry.getValue();

Map<String, Object> itemResultMap = new HashMap<>();
itemResultMap.put(SysConf.BLOG_SORT_UID, blogSortUid);
itemResultMap.put(SysConf.NAME, blogSortName);
itemResultMap.put(SysConf.VALUE, count);
resultMap.add(itemResultMap);
resultList.add(itemResultMap);
}
}
return resultMap;
// 将 每个分类下文章数目 存入到Redis【过期时间2小时】
if(resultList.size() > 0) {
redisUtil.setEx(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_COUNT_BY_SORT, JsonUtils.objectToJson(resultList), 2, TimeUnit.HOURS);
}
return resultList;
}

@Override
public Map<String, Object> getBlogContributeCount() {
// 从Redis中获取博客分类下包含的博客数量
String jsonMap = redisUtil.get(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_CONTRIBUTE_COUNT);
if (StringUtils.isNotEmpty(jsonMap)) {
Map<String, Object> resultMap = JsonUtils.jsonToMap(jsonMap);
return resultMap;
}

// 获取今天结束时间
String endTime = DateUtils.getNowTime();

// 获取365天前的日期
Date temp = DateUtils.getDate(endTime, -365);

String startTime = DateUtils.dateTimeToStr(temp);

List<Map<String, Object>> blogContributeMap = blogMapper.getBlogContributeCount(startTime, endTime);

List<String> dateList = DateUtils.getDayBetweenDates(startTime, endTime);

Map<String, Object> dateMap = new HashMap<>();

for (Map<String, Object> itemMap : blogContributeMap) {

dateMap.put(itemMap.get("DATE").toString(), itemMap.get("COUNT"));
}

Expand All @@ -456,12 +469,14 @@ public Map<String, Object> getBlogContributeCount() {
resultList.add(objectList);
}

Map<String, Object> resultMap = new HashMap<>();
Map<String, Object> resultMap = new HashMap<>(Constants.NUM_TWO);
List<String> contributeDateList = new ArrayList<>();
contributeDateList.add(startTime);
contributeDateList.add(endTime);
resultMap.put(SysConf.CONTRIBUTE_DATE, contributeDateList);
resultMap.put(SysConf.BLOG_CONTRIBUTE_COUNT, resultList);
// 将 全年博客贡献度 存入到Redis【过期时间2小时】
redisUtil.setEx(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_CONTRIBUTE_COUNT, JsonUtils.objectToJson(resultMap), 2, TimeUnit.HOURS);
return resultMap;
}

Expand Down Expand Up @@ -993,6 +1008,32 @@ public String uploadLocalBlog(List<MultipartFile> filedatas) throws IOException
return ResultUtil.result(SysConf.SUCCESS, MessageConf.INSERT_SUCCESS);
}

@Override
public void deleteRedisByBlogSort() {
// 删除Redis中博客分类下的博客数量
redisUtil.delete(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_COUNT_BY_SORT);
// 删除博客相关缓存
deleteRedisByBlog();
}

@Override
public void deleteRedisByBlogTag() {
// 删除Redis中博客分类下的博客数量
redisUtil.delete(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.BLOG_COUNT_BY_TAG);
// 删除博客相关缓存
deleteRedisByBlog();
}

@Override
public void deleteRedisByBlog() {
// 删除博客相关缓存
redisUtil.delete(RedisConf.NEW_BLOG);
redisUtil.delete(RedisConf.HOT_BLOG);
redisUtil.delete(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + ELevel.FIRST);
redisUtil.delete(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + ELevel.SECOND);
redisUtil.delete(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + ELevel.THIRD);
redisUtil.delete(RedisConf.BLOG_LEVEL + Constants.SYMBOL_COLON + ELevel.FOURTH);
}

@Override
public IPage<Blog> getBlogPageByLevel(Integer level, Long currentPage, Integer useSort) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@
import java.util.*;

/**
* <p>
* 博客分类表 服务实现类
* </p>
*
* @author xuzhixiang
* @author 陌溪
* @since 2018-09-08
*/
@Service
public class BlogSortServiceImpl extends SuperServiceImpl<BlogSortMapper, BlogSort> implements BlogSortService {

@Autowired
BlogSortService blogSortService;
private BlogSortService blogSortService;

@Autowired
BlogService blogService;
private BlogService blogService;

@Override
public IPage<BlogSort> getPageList(BlogSortVO blogSortVO) {
Expand Down Expand Up @@ -86,7 +84,6 @@ public String addBlogSort(BlogSortVO blogSortVO) {
@Override
public String editBlogSort(BlogSortVO blogSortVO) {
BlogSort blogSort = blogSortService.getById(blogSortVO.getUid());

/**
* 判断需要编辑的博客分类是否存在
*/
Expand All @@ -99,13 +96,14 @@ public String editBlogSort(BlogSortVO blogSortVO) {
return ResultUtil.result(SysConf.ERROR, MessageConf.ENTITY_EXIST);
}
}

blogSort.setContent(blogSortVO.getContent());
blogSort.setSortName(blogSortVO.getSortName());
blogSort.setSort(blogSortVO.getSort());
blogSort.setStatus(EStatus.ENABLE);
blogSort.setUpdateTime(new Date());
blogSort.updateById();
// 删除和博客相关的Redis缓存
blogService.deleteRedisByBlogSort();
return ResultUtil.result(SysConf.SUCCESS, MessageConf.UPDATE_SUCCESS);
}

Expand All @@ -128,17 +126,15 @@ public String deleteBatchBlogSort(List<BlogSortVO> blogSortVoList) {
if (blogCount > 0) {
return ResultUtil.result(SysConf.ERROR, MessageConf.BLOG_UNDER_THIS_SORT);
}

Collection<BlogSort> blogSortList = blogSortService.listByIds(uids);

blogSortList.forEach(item -> {
item.setUpdateTime(new Date());
item.setStatus(EStatus.DISABLED);
});

Boolean save = blogSortService.updateBatchById(blogSortList);

if (save) {
// 删除和博客相关的Redis缓存
blogService.deleteRedisByBlogSort();
return ResultUtil.result(SysConf.SUCCESS, MessageConf.DELETE_SUCCESS);
} else {
return ResultUtil.result(SysConf.ERROR, MessageConf.DELETE_FAIL);
Expand All @@ -165,15 +161,10 @@ public String stickBlogSort(BlogSortVO blogSortVO) {
if (maxSort.getUid().equals(blogSort.getUid())) {
return ResultUtil.result(SysConf.ERROR, MessageConf.THIS_SORT_IS_TOP);
}

Integer sortCount = maxSort.getSort() + 1;

blogSort.setSort(sortCount);

blogSort.setUpdateTime(new Date());

blogSort.updateById();

return ResultUtil.result(SysConf.SUCCESS, MessageConf.OPERATION_SUCCESS);
}

Expand Down
Loading

0 comments on commit e33ab80

Please sign in to comment.