当前位置: 首页 > news >正文

XxlJob热点文章定时计算

需求分析

image-20250615144336639

image-20250615145019881

定时计算步骤

image-20250615144716824

image-20250615144828865

image-20250615145409396

image-20250615145528519

image-20250615222734160

热文章服务实现

计算热点文章

/*** 计算热点文章*/
@Override
public void computeHotArticle() {// 查询前5天的文章数据Date dateParam = DateTime.now().minusDays(5).toDate();List<ApArticle> apArticleList = apArticleMapper.findArticleListByLast5days(dateParam);// 计算文章的分值List<HotArticleVo> hotArticleVoList = computeHotArticle(apArticleList);// 为每个频道缓存30条分值较高的文章cacheTagToRedis(hotArticleVoList);
}

为每个频道缓存30条分值较高的文章

/*** 为每个频道缓存30条分值较高的文章** @param hotArticleVoList*/
private void cacheTagToRedis(List<HotArticleVo> hotArticleVoList) {// 每个频道缓存30条分值较高的文章ResponseResult responseResult = wemediaClient.getChannels();if (responseResult.getCode().equals("200")) {String channelJson = JSON.toJSONString(responseResult.getData());List<WmChannel> wmChannels = JSON.parseArray(channelJson, WmChannel.class);// 检索出每个频道的文章if (wmChannels != null && !wmChannels.isEmpty()) {for (WmChannel wmChannel : wmChannels) {List<HotArticleVo> hotArticleVos = hotArticleVoList.stream().filter(x ->x.getChannelId().equals(wmChannel.getId())).collect(Collectors.toList());// 给文章进行排序,取30条分值较高的文章存入redis key:频道idsortAndCache(hotArticleVos, ArticleConstants.HOT_ARTICLE_FIRST_PAGE + wmChannel.getId());}}}// 设置推荐数据// 给文章进行排序,取30条分值较高的文章存入redis key:频道id value:30条分值较高的文章sortAndCache(hotArticleVoList, ArticleConstants.HOT_ARTICLE_FIRST_PAGE + ArticleConstants.DEFAULT_TAG);
}

计算文章的具体分数

/*** 计算文章的具体分数** @param apArticle* @return*/
private Integer computeScore(ApArticle apArticle) {Integer score = 0;if (apArticle.getLikes() != null) {score += apArticle.getLikes() * ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;}if (apArticle.getViews() != null) {score += apArticle.getViews();}if (apArticle.getComment() != null) {score += apArticle.getComment() * ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;}if (apArticle.getCollection() != null) {score += apArticle.getCollection() * ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;}return score;
}

计算文章分值

/*** 计算文章分值** @param apArticleList* @return*/
private List<HotArticleVo> computeHotArticle(List<ApArticle> apArticleList) {List<HotArticleVo> hotArticleVoList = new ArrayList<>();if (apArticleList != null && !apArticleList.isEmpty()) {for (ApArticle apArticle : apArticleList) {HotArticleVo hot = new HotArticleVo();BeanUtils.copyProperties(apArticle, hot);Integer score = computeScore(apArticle);hot.setScore(score);hotArticleVoList.add(hot);}}return hotArticleVoList;
}

给文章进行排序,取30条分值较高的文章存入redis

/*** 给文章进行排序,取30条分值较高的文章存入redis* @param hotArticleVos* @param key*/
private void sortAndCache(List<HotArticleVo> hotArticleVos, String key) {hotArticleVos = hotArticleVos.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed()).collect(Collectors.toList());if (hotArticleVos.size() > 30) {hotArticleVos = hotArticleVos.subList(0, 30);}cacheService.set(key, JSON.toJSONString(hotArticleVos));
}

任务

@XxlJob("computeHotArticleJob")
public void handle() {log.info("热文章分值计算调度任务开始执行...");hotArticleService.computeHotArticle();log.info("热文章分值计算调度任务结束...");
}
http://www.dtcms.com/a/251081.html

相关文章:

  • 组合模式Composite Pattern
  • 系统辨识的研究生水平读书报告期末作业参考
  • LangChain面试内容整理-知识点14:工具包(Toolkits)与用法
  • 嵌入式学习笔记 - SH79F6441 堆栈栈顶可以是片上内部RAM(00H-FFH)的任意地址怎么理解
  • Jmeter录制APP脚本
  • Kafka多副本机制
  • React 实现卡牌翻牌游戏
  • 小记:把react项目从web迁移到electron
  • 蒸馏微调DeepSeek-R1-Distill-Qwen-7B
  • Leetcode 刷题记录 16 —— 栈
  • [windows工具]OCR识文找图工具1.2版本使用教程及注意事项
  • [windows工具]OCR多区域识别导出excel工具1.2版本使用教程及注意事项
  • Unity3D仿星露谷物语开发63之NPC移动
  • XR-RokidAR-ADB环境搭建
  • OpenSpeedy:让游戏体验“飞”起来的秘密武器
  • 【Shader学习】完整光照效果
  • Unity基础-范围检测
  • 【Quest开发】初始项目环境配置
  • 用c语言实现简易c语言扫雷游戏
  • 嵌入式硬件篇---常见电平标准
  • NJet Portal 应用门户管理介绍
  • 实时操作系统(FreeRTOS、RT-Thread)RISC-V
  • 基于SpringBoot和Leaflet的电影票房时序展示-以《哪吒2》为例
  • Django图片管理系统
  • 在idea上打包DolphinScheduler
  • VTK 显示大量点云数据及交互(点云拾取、着色、测量等)功能
  • SAP实施服务专家——哲讯科技,赋能企业智慧升级
  • RTDETRv2 pytorch 官方版自己数据集训练遇到的问题解决
  • SQL Server 2025 预览版发布:AI深度集成、开发者体验飞跃与混合云新篇章
  • Composer 的 PHP 依赖库提交教程