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

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.xdnf.cn/news/14335.html

相关文章:

  • 001微信小程序入门
  • 向量外积与秩1矩阵的关系
  • Path.mkdir vs os.makedirs:为什么Ruff建议替换?
  • node中Token刷新机制:给你的数字钥匙续期的奇妙之旅
  • RADIUS服务器的核心应用场景与ASP认证服务器的快速对接指南
  • Linux--存储系统探秘:从块设备到inode
  • 基于STM32单片机RLC检测仪
  • TabSyncer:浏览器标签页管理工具
  • 【freertos互斥量补充】递归锁
  • 1.18 进程管理PM2
  • 山东大学项目实训-创新实训-法律文书专家系统-项目报告(六)
  • 【数据结构中的堆】
  • ASR-PRO语音识别可能出现的问题
  • langchain从入门到精通(九)——ChatGPT/Playground手动模拟记忆功能
  • MFE微前端:如何捕捉远程应用的remote.js加载失败的错误?
  • 【人工智能数学基础】测度论
  • 11.OpenCV—联合QT环境配置
  • RTDETRv2 pytorch 官方版自己数据集训练遇到的问题解决
  • 正整数的正向分解
  • 股指期货的多空策略是什么?
  • 编译链接实战(30)strip移除了哪些内容
  • java设计模式[3]之结构性型模式
  • Druid 连接池详解
  • 基于CSO算法的任务卸载在IoT移动边缘计算
  • 绝对收敛 趋于 0 的速度足够快 | 条件收敛 --> 项趋于 0 正负项相互抵消
  • 语言模型的泛化能力和训练数据依赖性
  • Docker -- 快速入门
  • JavaScript 数据结构详解
  • Java垃圾回收机制
  • [NLP]课程期末知识点总结