Sfoglia il codice sorgente

听书入口全缓存化

volta 2 anni fa
parent
commit
b77ef53612

+ 17 - 18
launch-admin/src/main/java/com/webflux/launchadmin/mysql/controller/planNew/PlanNewOutController.java

@@ -183,24 +183,23 @@ public class PlanNewOutController {
                 throw new BaseException("未获取到计划信息");
             }
             ListeningPlanNew planInfo = com.alibaba.fastjson.JSONObject.parseObject(planCache.toString(),ListeningPlanNew.class);
-//            Mono<ListeningPlanNew> code1 = template.selectOne(Query.query(Criteria.where("code").is(code)), ListeningPlanNew.class)
-//                    .switchIfEmpty(Mono.error(new BaseException("计划code异常 返回结果为空"))) //Mono.error(new BaseException("查询海报模版 id:"+f.posterTemplateId()+"=>空"))
-//                    .onErrorResume(throwable -> Mono.error(new Exception(  throwable.getMessage())));
-
-            Mono<DomainSelectRes> domainSelectResMono1 = template.select(Query.query(Criteria.where("group_type_id").is(planInfo.groupId())
-                    .and("status").is(1).and("type").is(type).and("deleted_at").isNull()), DomainSelect.class)
-                    .switchIfEmpty(Mono.error(new BaseException("查询domain 返回结果为空")))
-                    .onErrorResume(throwable -> Mono.error(new Exception(throwable.getMessage()))).collectList().flatMap(fm -> {
-                        DomainSelectRes domainSelectRes = new DomainSelectRes();
-                        int i = RandomUtil.randomInt(0, fm.size());
-                        String domain = fm.get(i).domain();
-                        String string = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 6);
-                        domainSelectRes.setDomain(string+"."+domain);
-                        domainSelectRes.setPath("");
-                        domainSelectRes.setUnique(IdUtil.fastSimpleUUID());
-                        return Mono.just(domainSelectRes);
-                    });
-            return RStatus.success(domainSelectResMono1);
+            String idKey = RedisKey.DOMAIN_RAND_ID + ":" + planInfo.groupId().toString() + ":" + type.toString();
+            String domainId = stringRedisTemplate.opsForSet().randomMember(idKey);
+            if (domainId == null || domainId.isEmpty()){
+                throw new BaseException("缓存"+ idKey + "不存在");
+            }
+            Object domainInfoCache = stringRedisTemplate.opsForHash().get(RedisKey.DOMAIN_INFO,domainId);
+            if (domainInfoCache == null){
+                throw new BaseException("缓存" + RedisKey.DOMAIN_INFO + "指定域名信息不存在");
+            }
+            DomainSelect domainInfo = com.alibaba.fastjson.JSONObject.parseObject(domainInfoCache.toString(),DomainSelect.class);
+            DomainSelectRes domainSelectRes = new DomainSelectRes();
+            String domain = domainInfo.domain();
+            String string = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 6);
+            domainSelectRes.setDomain(string+"."+domain);
+            domainSelectRes.setPath("");
+            domainSelectRes.setUnique(IdUtil.fastSimpleUUID());
+            return RStatus.success(Mono.just(domainSelectRes));
         }
         throw new BaseException("typePlan 类型错误");
 

+ 50 - 0
launch-admin/src/main/java/com/webflux/launchadmin/mysql/task/SyncDomainToRedis.java

@@ -0,0 +1,50 @@
+package com.webflux.launchadmin.mysql.task;
+
+import com.alibaba.fastjson.JSONObject;
+import com.webflux.launchadmin.mysql.entity.listening.ListeningPlanNew;
+import com.webflux.launchadmin.mysql.entity.planNew.DomainSelect;
+import com.weblux.launchredis.utils.RedisKey;
+import jakarta.annotation.Resource;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.data.relational.core.query.Criteria;
+import org.springframework.data.relational.core.query.Query;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+import java.util.List;
+
+@Component
+public class SyncDomainToRedis {
+    @Resource
+    private R2dbcEntityTemplate template;
+
+    @Autowired
+    private StringRedisTemplate stringRedisTemplate;
+
+    @Scheduled(cron = "0 */1 * * * ?")
+    public void syncDomain() {
+        //.更新相信信息
+        //.新增ID
+        template.select(Query.query(Criteria.where("status").is(1).and("deleted_at").isNull()), DomainSelect.class).toIterable().forEach(one -> {
+                    if (one.groupTypeId() == null || one.type() == null){
+                        return;
+                    }
+                    String idKey = RedisKey.DOMAIN_RAND_ID + one.groupTypeId() + ":" + one.type();
+                    stringRedisTemplate.opsForSet().add(idKey,String.valueOf(one.id()));
+                    stringRedisTemplate.opsForHash().put(RedisKey.DOMAIN_INFO,one.id().toString(),JSONObject.toJSONString(one));
+        });
+
+        //.移除
+        template.select(Query.query(Criteria.
+                        where("status").not(1)
+                        .or("deleted_at").isNotNull()),
+                DomainSelect.class).toIterable().forEach(one -> {
+            String idKey = RedisKey.DOMAIN_RAND_ID + ":" + one.groupTypeId() + ":" + one.type();
+            stringRedisTemplate.opsForSet().remove(idKey,String.valueOf(one.id()));
+        });
+    }
+}

+ 6 - 0
launch-redis/src/main/java/com/weblux/launchredis/utils/RedisKey.java

@@ -23,4 +23,10 @@ public class RedisKey {
 
     //.计划缓存
     public final static String PLAN_LISTEN_INFO = "sync_plan_listen_info";
+
+    //.域名随机ID
+    public final static String DOMAIN_RAND_ID = "sync_domain_rand_id:";
+
+    //.域名相信信息
+    public final static String DOMAIN_INFO = "sync_domain_info";
 }