Kaynağa Gözat

新增 deleteGzhWaiting 接口,实现删除待发布数据并清理对应缓存

wangyunpeng 8 saat önce
ebeveyn
işleme
25eecbf437

+ 26 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/common/constant/RedisConstants.java

@@ -0,0 +1,26 @@
+package com.tzld.longarticle.recommend.server.common.constant;
+
+/**
+ * Redis Key 常量
+ *
+ * @author dyp
+ */
+public class RedisConstants {
+
+    /**
+     * ContentPreFilterJob 预过滤缓存 key 前缀
+     * 完整 key 格式:ContentPreFilterJob:{yyyyMMdd}:{planId}-{accountId}
+     */
+    public static final String CONTENT_PRE_FILTER_JOB_KEY_PREFIX = "ContentPreFilterJob:";
+
+    /**
+     * 构造 ContentPreFilterJob 预过滤缓存 key
+     *
+     * @param dateStr   日期,格式 yyyyMMdd
+     * @param planId    发布计划ID
+     * @param accountId 发布账号ID
+     */
+    public static String buildContentPreFilterJobKey(String dateStr, String planId, String accountId) {
+        return CONTENT_PRE_FILTER_JOB_KEY_PREFIX + dateStr + ":" + planId + "-" + accountId;
+    }
+}

+ 6 - 5
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/XxlJobService.java

@@ -9,6 +9,7 @@ import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import com.tzld.longarticle.recommend.server.common.CommonThreadPoolExecutor;
 import com.tzld.longarticle.recommend.server.common.HttpPoolFactory;
+import com.tzld.longarticle.recommend.server.common.constant.RedisConstants;
 import com.tzld.longarticle.recommend.server.common.constant.SceneConstants;
 import com.tzld.longarticle.recommend.server.common.enums.FieshuTableColumnDataTypeEnum;
 import com.tzld.longarticle.recommend.server.common.enums.aigc.ChannelEnum;
@@ -1070,8 +1071,8 @@ public class XxlJobService {
                                 , cnt > 0 ? newAuditIds : null);
                     }
                     savePublishContentCache(contentList, account.getPlanId(), account.getAccountId(), findAll, now);
-                    String redisKey = "ContentPreFilterJob:" + DateUtils.getCurrentDateStr("yyyyMMdd") + ":"
-                            + account.getPlanId() + "-" + account.getAccountId();
+                    String redisKey = RedisConstants.buildContentPreFilterJobKey(
+                            DateUtils.getCurrentDateStr("yyyyMMdd"), account.getPlanId(), account.getAccountId());
                     doPreFilter(account, redisKey, 0, null);
                 } finally {
                     cdl.countDown();
@@ -1107,8 +1108,8 @@ public class XxlJobService {
                     List<Content> contentList;
                     contentList = aigcWaitingPublishContentService.getAllContent(account.getPlanId(), account.getAccountId(), null);
                     savePublishContentCache(contentList, account.getPlanId(), account.getAccountId(), true, now);
-                    String redisKey = "ContentPreFilterJob:" + DateUtils.getCurrentDateStr("yyyyMMdd") + ":"
-                            + account.getPlanId() + "-" + account.getAccountId();
+                    String redisKey = RedisConstants.buildContentPreFilterJobKey(
+                            DateUtils.getCurrentDateStr("yyyyMMdd"), account.getPlanId(), account.getAccountId());
                     doPreFilter(account, redisKey, 0, null);
                 } finally {
                     cdl.countDown();
@@ -1213,7 +1214,7 @@ public class XxlJobService {
         CountDownLatch cdl = new CountDownLatch(planAccountList.size());
         String dateStr = DateUtils.getCurrentDateStr("yyyyMMdd");
         for (PublishPlanAccountDTO item : planAccountList) {
-            String redisKey = "ContentPreFilterJob:" + dateStr + ":" + item.getPlanId() + "-" + item.getAccountId();
+            String redisKey = RedisConstants.buildContentPreFilterJobKey(dateStr, item.getPlanId(), item.getAccountId());
             if (!(calculateRemainder(item.getAccountId(), serverSize) == index)) {
                 continue;
             }

+ 17 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/RecommendService.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.google.common.collect.Lists;
 import com.tzld.longarticle.recommend.server.common.CostMonitor;
+import com.tzld.longarticle.recommend.server.common.constant.RedisConstants;
 import com.tzld.longarticle.recommend.server.common.constant.SceneConstants;
 import com.tzld.longarticle.recommend.server.common.enums.aigc.PushTypeEnum;
 import com.tzld.longarticle.recommend.server.common.enums.recommend.ArticleTypeEnum;
@@ -39,6 +40,7 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
@@ -74,6 +76,8 @@ public class RecommendService {
     private PublishPlanRepository publishPlanRepository;
     @Autowired
     private LongArticleBaseMapper longArticleBaseMapper;
+    @Autowired
+    private RedisTemplate<String, String> redisTemplate;
 
     @ApolloJsonValue("${accountStrategyConfig:{}}")
     private Map<String, String> accountStrategyConfigMap;
@@ -375,4 +379,17 @@ public class RecommendService {
         }
     }
 
+    /**
+     * 删除指定计划与发布账号下的 publish_content_gzh_waiting 数据,
+     * 并清理对应的 ContentPreFilterJob redis 缓存。
+     */
+    public void deleteGzhWaitingByPlanAndAccount(String planId, String publishAccountId) {
+        longArticleBaseMapper.deleteGzhWaitingByPlanIdAccountId(planId, publishAccountId);
+        String dateStr = DateUtils.getCurrentDateStr("yyyyMMdd");
+        String redisKey = RedisConstants.buildContentPreFilterJobKey(dateStr, planId, publishAccountId);
+        redisTemplate.delete(redisKey);
+        log.info("deleteGzhWaitingByPlanAndAccount planId:{} publishAccountId:{} redisKey:{}",
+                planId, publishAccountId, redisKey);
+    }
+
 }

+ 2 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recommend/filter/FilterService.java

@@ -3,6 +3,7 @@ package com.tzld.longarticle.recommend.server.service.recommend.filter;
 import com.tzld.longarticle.recommend.server.common.ContentCountMonitor;
 import com.tzld.longarticle.recommend.server.common.CostMonitor;
 import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
+import com.tzld.longarticle.recommend.server.common.constant.RedisConstants;
 import com.tzld.longarticle.recommend.server.common.enums.recommend.RankStrategyEnum;
 import com.tzld.longarticle.recommend.server.mapper.longArticle.LongArticleBaseMapper;
 import com.tzld.longarticle.recommend.server.model.dto.Content;
@@ -48,7 +49,7 @@ public class FilterService {
 
         List<FilterStrategy> strategies = getAllStrategies(param);
         String dateStr = DateUtils.getCurrentDateStr("yyyyMMdd");
-        String redisKey = "ContentPreFilterJob:" + dateStr + ":" + param.getPlanId() + "-" + param.getAccountId();
+        String redisKey = RedisConstants.buildContentPreFilterJobKey(dateStr, param.getPlanId(), param.getAccountId());
 
         // 已同步待发布内容 预过滤
         if (!preFilter) {

+ 13 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/web/recommend/RecommendController.java

@@ -1,13 +1,16 @@
 package com.tzld.longarticle.recommend.server.web.recommend;
 
+import com.tzld.longarticle.recommend.server.common.response.CommonResponse;
 import com.tzld.longarticle.recommend.server.model.param.RecommendRequest;
 import com.tzld.longarticle.recommend.server.model.vo.RecommendResponse;
 import com.tzld.longarticle.recommend.server.model.vo.RecommendWithUserGroupResponse;
 import com.tzld.longarticle.recommend.server.service.recommend.RecommendService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 /**
@@ -31,4 +34,14 @@ public class RecommendController {
         return recommendService.recommend4FwhColdStart(httpRequest);
     }
 
+    @RequestMapping("/deleteGzhWaiting")
+    public CommonResponse<Void> deleteGzhWaiting(@RequestParam String planId,
+                                                 @RequestParam String publishAccountId) {
+        if (!StringUtils.hasText(planId) || !StringUtils.hasText(publishAccountId)) {
+            return CommonResponse.create(-1, "planId、publishAccountId不能为空");
+        }
+        recommendService.deleteGzhWaitingByPlanAndAccount(planId, publishAccountId);
+        return CommonResponse.success();
+    }
+
 }

+ 2 - 1
long-article-recommend-service/src/test/java/com/tzld/longarticle/recommend/server/XxlJobTest.java

@@ -2,6 +2,7 @@ package com.tzld.longarticle.recommend.server;
 
 import com.alibaba.fastjson.JSONObject;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.tzld.longarticle.recommend.server.common.constant.RedisConstants;
 import com.tzld.longarticle.recommend.server.mapper.aigc.AigcBaseMapper;
 import com.tzld.longarticle.recommend.server.mapper.aigc.PublishContentMapper;
 import com.tzld.longarticle.recommend.server.mapper.longArticle.ArticleCategoryMapper;
@@ -128,7 +129,7 @@ public class XxlJobTest {
         item.setPlanId("20240809061450286142711");
         item.setAccountId("20240708082126256410152");
         String dateStr = DateUtils.getCurrentDateStr("yyyyMMdd");
-        String redisKey = "ContentPreFilterJob:" + dateStr + ":" + item.getPlanId() + "-" + item.getAccountId();
+        String redisKey = RedisConstants.buildContentPreFilterJobKey(dateStr, item.getPlanId(), item.getAccountId());
         xxlJobService.preFilter(item, redisKey, 1);
     }
 }