Просмотр исходного кода

新增rootSourceId存在性检查接口

wangyunpeng 6 часов назад
Родитель
Сommit
7210bf3bad

+ 4 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/api/ApiService.java

@@ -1,5 +1,9 @@
 package com.tzld.longarticle.recommend.server.service.api;
 package com.tzld.longarticle.recommend.server.service.api;
 
 
 public interface ApiService {
 public interface ApiService {
+
     String getGhIdByRootSourceId(String rootSourceId);
     String getGhIdByRootSourceId(String rootSourceId);
+
+    Boolean checkExistRootSourceId(String rootSourceId);
+
 }
 }

+ 19 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/api/impl/ApiServiceImpl.java

@@ -4,7 +4,9 @@ import com.tzld.longarticle.recommend.server.model.entity.longArticle.LongArticl
 import com.tzld.longarticle.recommend.server.repository.longArticle.LongArticlesRootSourceIdRepository;
 import com.tzld.longarticle.recommend.server.repository.longArticle.LongArticlesRootSourceIdRepository;
 import com.tzld.longarticle.recommend.server.service.api.ApiService;
 import com.tzld.longarticle.recommend.server.service.api.ApiService;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import java.util.Objects;
 import java.util.Objects;
@@ -16,6 +18,9 @@ public class ApiServiceImpl implements ApiService {
     @Autowired
     @Autowired
     LongArticlesRootSourceIdRepository longArticlesRootSourceIdRepository;
     LongArticlesRootSourceIdRepository longArticlesRootSourceIdRepository;
 
 
+    @Autowired
+    private RedisTemplate<String, String> redisTemplate;
+
     @Override
     @Override
     public String getGhIdByRootSourceId(String rootSourceId) {
     public String getGhIdByRootSourceId(String rootSourceId) {
         LongArticlesRootSourceId longArticlesRootSourceId = longArticlesRootSourceIdRepository.getByRootSourceId(rootSourceId);
         LongArticlesRootSourceId longArticlesRootSourceId = longArticlesRootSourceIdRepository.getByRootSourceId(rootSourceId);
@@ -25,4 +30,18 @@ public class ApiServiceImpl implements ApiService {
         return longArticlesRootSourceId.getGhId();
         return longArticlesRootSourceId.getGhId();
     }
     }
 
 
+    @Override
+    public Boolean checkExistRootSourceId(String rootSourceId) {
+        String redisKey = "check_exist_root_source_id_" + rootSourceId;
+        String value = redisTemplate.opsForValue().get(redisKey);
+        if (StringUtils.isNotEmpty(value)) {
+            return Boolean.parseBoolean(value);
+        }
+        LongArticlesRootSourceId longArticlesRootSourceId = longArticlesRootSourceIdRepository.getByRootSourceId(rootSourceId);
+        boolean exists = Objects.nonNull(longArticlesRootSourceId);
+        // 缓存1小时
+        redisTemplate.opsForValue().set(redisKey, String.valueOf(exists), 60 * 60);
+        return exists;
+    }
+
 }
 }

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

@@ -22,4 +22,9 @@ public class ApiController {
         return CommonResponse.success(service.getGhIdByRootSourceId(rootSourceId));
         return CommonResponse.success(service.getGhIdByRootSourceId(rootSourceId));
     }
     }
 
 
+    @GetMapping("/checkExistRootSourceId")
+    public CommonResponse<Boolean> checkExistRootSourceId(@RequestParam String rootSourceId) {
+        return CommonResponse.success(service.checkExistRootSourceId(rootSourceId));
+    }
+
 }
 }