From 1cdb060a8aa59b0979f7609db1781805528e76e7 Mon Sep 17 00:00:00 2001 From: xiangpei <xiangpei@timesnew.cn> Date: 星期三, 25 六月 2025 18:25:42 +0800 Subject: [PATCH] 视频评论数、收藏数通过mq、redis实现 --- framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java | 93 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 93 insertions(+), 0 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java b/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java index ca75179..57909e0 100644 --- a/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/lmk/service/impl/VideoServiceImpl.java @@ -1,6 +1,9 @@ package cn.lili.modules.lmk.service.impl; +import cn.lili.cache.Cache; +import cn.lili.cache.CachePrefix; import cn.lili.common.security.context.UserContext; +import cn.lili.modules.lmk.constant.RedisKeyExpireConstant; import cn.lili.modules.lmk.domain.entity.*; import cn.lili.modules.lmk.domain.form.*; import cn.lili.modules.lmk.domain.query.*; @@ -57,6 +60,7 @@ private final KitchenVideoTypeRefService kitchenVideoTypeRefService; private final VideoGoodsService videoGoodsService; private final KitchenTypeService kitchenTypeService; + private final Cache cache; /** * 娣诲姞 @@ -389,6 +393,8 @@ page.getRecords().forEach(v -> { v.setTagList(tagMap.get(v.getId())); v.setCollected(CollectionUtils.isNotEmpty(collectMap.get(v.getId()))); + v.setCommentNum(this.getCommentNum(v.getId(), v.getCommentNum())); + v.setCollectNum(this.getCollectNum(v.getId(), v.getCollectNum())); if (VideoContentTypeEnum.VIDEO.getValue().equals(v.getVideoContentType())) { v.setVideoUrl(cosUtil.getPreviewUrl(v.getVideoFileKey())); v.setCoverUrl(cosUtil.getPreviewUrl(v.getCoverFileKey())); @@ -400,6 +406,40 @@ }); } return Result.ok().data(page.getRecords()); + } + + /** + * 浠巖edis涓幏鍙栬瘎璁烘暟閲忥紝濡傛灉redis涓病鏈夊垯灏唌ysql涓殑鏁伴噺鍐欏叆鍒皉edis + * + * @param videoId + * @param mysqlNum + * @return + */ + private Integer getCommentNum(String videoId, Integer mysqlNum) { + Object redisNum = cache.get(CachePrefix.VIDEO_COMMENT_NUM.getPrefixWithId(videoId)); + if (Objects.isNull(redisNum)) { + // redis涓病鏈夊氨鎶婃暟鎹簱鐨勫啓鍒皉edis涓� + cache.put(CachePrefix.VIDEO_COMMENT_NUM.getPrefixWithId(videoId), mysqlNum, RedisKeyExpireConstant.COMMENT_NUM_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY); + return mysqlNum; + } + return (Integer) redisNum; + } + + /** + * 浠巖edis涓幏鍙栨敹钘忔暟閲忥紝濡傛灉redis涓病鏈夊垯灏唌ysql涓殑鏁伴噺鍐欏叆鍒皉edis + * + * @param videoId + * @param mysqlNum + * @return + */ + private Integer getCollectNum(String videoId, Integer mysqlNum) { + Object redisNum = cache.get(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(videoId)); + if (Objects.isNull(redisNum)) { + // redis涓病鏈夊氨鎶婃暟鎹簱鐨勫啓鍒皉edis涓� + cache.put(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(videoId), mysqlNum, RedisKeyExpireConstant.COLLECT_NUM_EXPIRE, RedisKeyExpireConstant.EXPIRE_DAY); + return mysqlNum; + } + return (Integer) redisNum; } @Override @@ -446,6 +486,10 @@ List<List<CollectTypeNumVO>> chunks = ListUtils.partition(numList, 500); for (List<CollectTypeNumVO> chunk : chunks) { baseMapper.updateCollectNumBatch(chunk); + new LambdaUpdateChainWrapper<>(baseMapper) + .in(Video::getId, chunk.stream().map(CollectTypeNumVO::getId).collect(Collectors.toList())) + .set(Video::getCollectNumJob, Boolean.FALSE) + .update(); } } @@ -456,6 +500,10 @@ List<List<CollectTypeNumVO>> chunks = ListUtils.partition(numList, 500); for (List<CollectTypeNumVO> chunk : chunks) { baseMapper.updateCommentNumBatch(chunk); + new LambdaUpdateChainWrapper<>(baseMapper) + .in(Video::getId, chunk.stream().map(CollectTypeNumVO::getId).collect(Collectors.toList())) + .set(Video::getCommentNumJob, Boolean.FALSE) + .update(); } } @@ -709,4 +757,49 @@ .eq(KitchenVideoTypeRef::getVideoId, id)); return Result.ok("鍒犻櫎鎴愬姛"); } + + /** + * mq鎵ц瑙嗛鐨勬敹钘�/鍙栨秷鏀惰棌 + * + * @param collect + */ + @Override + @Transactional(rollbackFor = Exception.class) + public void mqCollectChange(MyCollect collect) { + MyCollect myCollect = new LambdaQueryChainWrapper<>(myCollectService.getBaseMapper()) + .eq(MyCollect::getCollectType, collect.getCollectType()) + .eq(MyCollect::getRefId, collect.getRefId()) + .eq(MyCollect::getUserId, collect.getUserId()) + .one(); + boolean add = false; + if (Objects.nonNull(myCollect)) { + myCollectService.removeById(myCollect.getId()); + } else { + myCollectService.save(collect); + add = true; + } + // 澶勭悊缂撳瓨 + Video video = baseMapper.selectById(collect.getRefId()); + if (cache.exist(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(collect.getRefId()))) { + if (add) { + cache.incr(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(collect.getRefId())); + } else { + cache.decr(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(collect.getRefId())); + } + } else { + if (Objects.nonNull(video)) { + cache.put(CachePrefix.VIDEO_COLLECT_NUM.getPrefixWithId(video.getId()), + video.getCollectNum() + (add ? 1 : -1), + RedisKeyExpireConstant.COLLECT_NUM_EXPIRE, + RedisKeyExpireConstant.EXPIRE_DAY); + } + } + // 鏍囪瘑璇ヨ棰戦渶瑕侀�氳繃瀹氭椂浠诲姟缁熻鏀惰棌鏁� + if (Objects.nonNull(video) && ! video.getCollectNumJob()) { + new LambdaUpdateChainWrapper<>(baseMapper) + .eq(Video::getId, video.getId()) + .set(Video::getCollectNumJob, Boolean.TRUE) + .update(); + } + } } -- Gitblit v1.8.0