瀏覽代碼

增加缓存

xueyiming 8 月之前
父節點
當前提交
880e439291

+ 43 - 0
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/config/RedisConfig.java

@@ -0,0 +1,43 @@
+package com.tzld.piaoquan.longarticle.config;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+@Configuration
+public class RedisConfig {
+
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setConnectionFactory(redisConnectionFactory);
+        //设置value的序列化方式json
+        redisTemplate.setValueSerializer(redisSerializer());
+        //设置key序列化方式String
+        redisTemplate.setKeySerializer(new StringRedisSerializer());
+        //设置hash key序列化方式String
+        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
+        //设置hash value序列化json
+        redisTemplate.setHashValueSerializer(redisSerializer());
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
+    }
+
+    public RedisSerializer<Object> redisSerializer() {
+        //创建JSON序列化器
+        ObjectMapper objectMapper = new ObjectMapper();
+        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        //必须设置,否则无法序列化实体类对象
+        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
+        return new GenericJackson2JsonRedisSerializer(objectMapper);
+    }
+
+}

+ 8 - 0
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/local/PlanAccountService.java

@@ -1,7 +1,15 @@
 package com.tzld.piaoquan.longarticle.service.local;
 
+import com.tzld.piaoquan.longarticle.model.po.PlanAccount;
+
+import java.util.List;
+
 public interface PlanAccountService {
 
+    void saveOrUpdatePlanAccount(PlanAccount planAccount);
+
+    List<PlanAccount> getMatchPlanAccount();
+
     void updateMatchStatus(Integer status, Long id);
 
     void updateStatus(Integer status, Long id);

+ 17 - 0
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/local/impl/ContentServiceImpl.java

@@ -10,7 +10,9 @@ import com.tzld.piaoquan.longarticle.model.po.MatchVideo;
 import com.tzld.piaoquan.longarticle.model.po.MatchVideoExample;
 import com.tzld.piaoquan.longarticle.service.local.ContentService;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
@@ -18,6 +20,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 @Slf4j
 @Service
@@ -31,7 +34,21 @@ public class ContentServiceImpl implements ContentService {
     @Autowired
     private CrawlerVideoMapper crawlerVideoMapper;
 
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
+
     public MatchVideo getContent(String contentId, String ghId) {
+        String key = contentId + "_" + ghId;
+        String traceId = (String) redisTemplate.opsForValue().get(key);
+        if (StringUtils.isNotEmpty(traceId)) {
+            MatchVideoExample matchVideoExample = new MatchVideoExample();
+            matchVideoExample.createCriteria().andTraceIdEqualTo(traceId);
+            List<MatchVideo> matchVideos = matchVideoMapper.selectByExample(matchVideoExample);
+            if (!CollectionUtils.isEmpty(matchVideos)) {
+                redisTemplate.delete(key);
+                return matchVideos.get(0);
+            }
+        }
         MatchVideoExample matchVideoExample = new MatchVideoExample();
         matchVideoExample.createCriteria().andGhIdEqualTo(ghId).andContentIdEqualTo(contentId);
         List<MatchVideo> matchVideos = matchVideoMapper.selectByExample(matchVideoExample);

+ 18 - 24
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/local/impl/CoreServiceImpl.java

@@ -11,6 +11,7 @@ import com.tzld.piaoquan.longarticle.model.dto.*;
 import com.tzld.piaoquan.longarticle.model.po.*;
 import com.tzld.piaoquan.longarticle.model.vo.*;
 import com.tzld.piaoquan.longarticle.service.local.CoreService;
+import com.tzld.piaoquan.longarticle.service.local.PlanAccountService;
 import com.tzld.piaoquan.longarticle.service.remote.AigcService;
 import com.tzld.piaoquan.longarticle.service.remote.MatchService;
 import com.tzld.piaoquan.longarticle.service.remote.VideoService;
@@ -21,12 +22,14 @@ import com.tzld.piaoquan.longarticle.utils.TimeZoneUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
 import java.time.LocalTime;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -58,7 +61,7 @@ public class CoreServiceImpl implements CoreService {
     private PlanAccountMapper planAccountMapper;
 
     @Autowired
-    private PlanAccountServiceImpl planAccountService;
+    private PlanAccountService planAccountService;
 
     @Autowired
     private PublishMiniprogramMapper publishMiniprogramMapper;
@@ -72,16 +75,13 @@ public class CoreServiceImpl implements CoreService {
     @Autowired
     private PublicContentServiceImpl publicContentService;
 
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
+
     @Override
     public void initPlanAccount() {
         List<LongArticleSystemPlan> allLongArticleSystemPlans = aigcService.getAllLongArticleSystemPlan();
         for (LongArticleSystemPlan longArticleSystemPlan : allLongArticleSystemPlans) {
-            if (longArticleSystemPlan.getPublishRate() == 1) {
-                List<Integer> weeks = JSON.parseArray(longArticleSystemPlan.getPublishDate(), Integer.class);
-                if (!weeks.contains(TimeZoneUtil.getTodayDayOfWeek(TimeZoneUtil.Timezone.china))) {
-                    continue;
-                }
-            }
             //获取排序策略
             Map<String, String> sortStgMap = new HashMap<>();
             List<GzhArticleSortTaskParam> gzhArticleSortTask = longArticleSystemPlan.getGzhArticleSortTask();
@@ -130,16 +130,7 @@ public class CoreServiceImpl implements CoreService {
                     planAccount.setSortStrategy(sortStgMap.get(accountId));
                 }
                 planAccount.setPushType(longArticleSystemPlan.getPushType());
-                PlanAccountExample example = new PlanAccountExample();
-                example.createCriteria().andAccountIdEqualTo(account.getId()).andPlanIdEqualTo(longArticleSystemPlan.getId())
-                        .andCreateTimeGreaterThan(DateUtil.getThatDayDate());
-                List<PlanAccount> planAccounts = planAccountMapper.selectByExample(example);
-                if (CollectionUtils.isEmpty(planAccounts)) {
-                    planAccountMapper.insertSelective(planAccount);
-                } else {
-                    planAccount.setId(planAccounts.get(0).getId());
-                    planAccountMapper.updateByPrimaryKeySelective(planAccount);
-                }
+                planAccountService.saveOrUpdatePlanAccount(planAccount);
             }
         }
     }
@@ -147,10 +138,11 @@ public class CoreServiceImpl implements CoreService {
     @Override
     public void matchContent() {
         //查询状态为0的请求匹配
-        PlanAccountExample example = new PlanAccountExample();
-        example.createCriteria().andCreateTimeGreaterThan(DateUtil.getThatDayDate()).andMatchStatusEqualTo(0);
-        List<PlanAccount> planAccounts = planAccountMapper.selectByExample(example);
-        for (PlanAccount planAccount : planAccounts) {
+        List<PlanAccount> matchPlanAccounts = planAccountService.getMatchPlanAccount();
+        if (CollectionUtils.isEmpty(matchPlanAccounts)) {
+            return;
+        }
+        for (PlanAccount planAccount : matchPlanAccounts) {
             LongArticleSystemGetContentsParam param = new LongArticleSystemGetContentsParam();
             param.setAccountId(planAccount.getAccountId());
             param.setPlanId(planAccount.getPlanId());
@@ -196,7 +188,11 @@ public class CoreServiceImpl implements CoreService {
                     request.setStrategy("strategy_v2");
                     request.setArticleId(contentItemVO.getSourceId());
                     request.setFlowPoolLevelTag(contentItemVO.getFlowPoolLevelTag());
-                    matchService.matchMiniprogramVideo(request);
+                    String traceId = matchService.matchMiniprogramVideo(request);
+                    if (StringUtils.isNotEmpty(traceId)) {
+                        String key = contentItemVO.getSourceId() + "_" + planAccount.getGhId();
+                        redisTemplate.opsForValue().set(key, traceId, 24, TimeUnit.HOURS);
+                    }
                 }
             }
         }
@@ -208,11 +204,9 @@ public class CoreServiceImpl implements CoreService {
         }
         LocalTime currentTime = TimeZoneUtil
                 .currentTime(Optional.ofNullable(timezone).orElse(TimeZoneUtil.Timezone.china));
-
         if (currentTime.isAfter(LocalTime.parse(startWindow)) && currentTime.isBefore(LocalTime.parse(endWindow))) {
             return true;
         }
-
         return false;
     }
 

+ 26 - 0
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/local/impl/PlanAccountServiceImpl.java

@@ -2,9 +2,14 @@ package com.tzld.piaoquan.longarticle.service.local.impl;
 
 import com.tzld.piaoquan.longarticle.dao.mapper.PlanAccountMapper;
 import com.tzld.piaoquan.longarticle.model.po.PlanAccount;
+import com.tzld.piaoquan.longarticle.model.po.PlanAccountExample;
 import com.tzld.piaoquan.longarticle.service.local.PlanAccountService;
+import com.tzld.piaoquan.longarticle.utils.DateUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.util.List;
 
 @Service
 public class PlanAccountServiceImpl implements PlanAccountService {
@@ -12,6 +17,27 @@ public class PlanAccountServiceImpl implements PlanAccountService {
     @Autowired
     private PlanAccountMapper planAccountMapper;
 
+    public void saveOrUpdatePlanAccount(PlanAccount planAccount) {
+        PlanAccountExample example = new PlanAccountExample();
+        example.createCriteria().andAccountIdEqualTo(planAccount.getAccountId())
+                .andPlanIdEqualTo(planAccount.getPlanId())
+                .andCreateTimeGreaterThan(DateUtil.getThatDayDate());
+        List<PlanAccount> planAccounts = planAccountMapper.selectByExample(example);
+        if (CollectionUtils.isEmpty(planAccounts)) {
+            planAccountMapper.insertSelective(planAccount);
+        } else {
+            planAccount.setId(planAccounts.get(0).getId());
+            planAccountMapper.updateByPrimaryKeySelective(planAccount);
+        }
+    }
+
+    public List<PlanAccount> getMatchPlanAccount() {
+        PlanAccountExample example = new PlanAccountExample();
+        example.createCriteria().andCreateTimeGreaterThan(DateUtil.getThatDayDate())
+                .andMatchStatusEqualTo(0);
+        return planAccountMapper.selectByExample(example);
+    }
+
 
     @Override
     public void updateMatchStatus(Integer status, Long id) {

+ 1 - 1
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/remote/MatchService.java

@@ -4,5 +4,5 @@ import com.tzld.piaoquan.longarticle.model.dto.MiniprogramCardRequest;
 
 public interface MatchService {
 
-    boolean matchMiniprogramVideo(MiniprogramCardRequest request);
+    String matchMiniprogramVideo(MiniprogramCardRequest request);
 }

+ 3 - 3
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/service/remote/impl/MatchServiceImpl.java

@@ -17,18 +17,18 @@ public class MatchServiceImpl implements MatchService {
 
 
     @Override
-    public boolean matchMiniprogramVideo(MiniprogramCardRequest request) {
+    public String matchMiniprogramVideo(MiniprogramCardRequest request) {
         String apiUrl = "http://47.99.132.47:8111/search_videos";
         try {
             String res = HTTP_POOL_CLIENT_UTIL_DEFAULT.post(apiUrl, JSON.toJSONString(request));
             JSONObject jsonObject = JSON.parseObject(res);
             Integer code = jsonObject.getInteger("code");
             if (code == 0) {
-                return true;
+                return jsonObject.getString("traceId");
             }
         } catch (Exception e) {
             log.error("matchMiniprogramVideo error", e);
         }
-        return false;
+        return "";
     }
 }

+ 4 - 0
long-article-server/src/main/resources/application-prod.properties

@@ -4,6 +4,10 @@ spring.datasource.username=changwen_admin
 spring.datasource.password=changwen@123456
 spring.datasource.url=jdbc:mysql://rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com:3306/long_articles?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
 
+spring.redis.database=2
+spring.redis.host=r-bp154bpw97gptefiqkpd.redis.rds.aliyuncs.com
+spring.redis.port=6379
+spring.redis.password=Qingqu2019
 
 apollo.meta: https://apolloconfig-internal.piaoquantv.com
 

+ 4 - 0
long-article-server/src/main/resources/application-test.properties

@@ -4,6 +4,10 @@ spring.datasource.username=changwen_admin
 spring.datasource.password=changwen@123456
 spring.datasource.url=jdbc:mysql://rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com:3306/long_articles?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
 
+spring.redis.database=2
+spring.redis.host=r-bp154bpw97gptefiqkpd.redis.rds.aliyuncs.com
+spring.redis.port=6379
+spring.redis.password=Qingqu2019
 
 apollo.meta: https://apolloconfig-internal.piaoquantv.com
 

+ 5 - 0
long-article-server/src/main/resources/application.properties

@@ -14,6 +14,11 @@ spring.datasource.testOnBorrow=false
 spring.datasource.testOnReturn=false
 spring.datasource.poolPreparedStatements=true
 
+spring.redis.lettuce.pool.max-active=8
+spring.redis.lettuce.pool.max-wait=-1
+spring.redis.lettuce.pool.max-idle=8
+spring.redis.lettuce.pool.min-idle=0
+
 
 app.id=LongArticlesMatchServer
 apollo.bootstrap.enabled=true