Przeglądaj źródła

增加 getImageStyle 缓存

tsurumure 11 miesięcy temu
rodzic
commit
3c0ba4d066

+ 61 - 23
src/main/java/com/backendsys/service/Ai/Aizn/AiznModelServiceImpl.java

@@ -1,8 +1,10 @@
 package com.backendsys.service.Ai.Aizn;
 
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.backendsys.aspect.HttpRequestAspect;
+import com.backendsys.config.Redis.RedisCache;
 import com.backendsys.entity.Ai.Aizn.AiznImageStyleDTO;
 import com.backendsys.entity.Ai.Aizn.AiznIntelligentClipTaskDTO;
 import com.backendsys.entity.Ai.Aizn.AiznImageTaskVO;
@@ -18,6 +20,7 @@ import com.backendsys.service.SDKService.SDKTencent.SDKTencentCOSService;
 import com.backendsys.service.System.SysResourceService;
 import com.backendsys.utils.ListUtil;
 import com.backendsys.utils.MapUtil;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fzzixun.openapi.sdk.client.OpenClient;
 import com.fzzixun.openapi.sdk.common.RequestMethod;
 import com.fzzixun.openapi.sdk.request.CommonRequest;
@@ -56,43 +59,78 @@ public class AiznModelServiceImpl implements AiznModelService {
     @Autowired
     private SDKTencentCOSService sdkTencentCOSService;
 
+    @Autowired
+    private RedisCache redisCache;
+
     /**
      * [紫鸟] 获取风格/场景配置数据 (https://open.ziniao.com/manage/apiTest/809)
      */
     @Override
     public Map<String, Object> getImageStyle(AiznImageStyleDTO aiznImageStyleDTO) {
 
+        Integer type = aiznImageStyleDTO.getType();
+
         // -- 业务参数 -------------------------------------------------------------------
         JSONObject object = new JSONObject();
         object.set("showType", aiznImageStyleDTO.getShow_type());       // 展示类型 0.默认
-        object.set("type", aiznImageStyleDTO.getType());                // 类型 1.AI绘图
-
-        // -- 实现方法 -------------------------------------------------------------------
-        OpenClient client = aiznService.getOpenClient();
-        CommonRequest request = new CommonRequest("/linkfox-ai/image/v1/make/style", RequestMethod.POST_JSON);
-        request.setBizContent(object.toString());
+        object.set("type", type);                                       // 类型 1.AI绘图
 
-        // -- 返回结果 -------------------------------------------------------------------
-        CommonResponse response = client.executeAppToken(request, aiznService.getAppToken());
-        JSONObject resp = aiznService.checkResponse(response);
-        if ((Integer) resp.get("code") == 200) {
-
-            // -- 成功 ------------------------------------------------------------------
-            List<Map<String, Object>> respData = (List<Map<String, Object>>) resp.get("data");
+        String redisKey = "getImageStyle-" + type;
+        String redisCacheObject = redisCache.getCacheObject(redisKey);
+        if (StrUtil.isNotEmpty(redisCacheObject)) {
+            // [缓存存在] 读取缓存
             Map<String, Object> result = new LinkedHashMap<>();
-            result.put("type", aiznImageStyleDTO.getType());
-            result.put("total", respData.size());
-
-            // 将返回结果:首字母大写命名 转换为 下划线命名
-            result.put("list", ListUtil.convertToUnderscoreCase(respData));
-            // System.out.println(result);
+            try {
+                System.out.println("[缓存存在] 读取缓存");
+                ObjectMapper mapper = new ObjectMapper();
+                List<Map<String, Object>> respData = mapper.readValue(redisCacheObject, List.class);
+                result.put("type", type);
+                result.put("total", respData.size());
+                result.put("list", ListUtil.convertToUnderscoreCase(respData)); // 将返回结果:首字母大写命名 转换为 下划线命名
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
             return result;
-            // --------------------------------------------------------------------------
 
         } else {
-            System.out.println(resp);
-            Integer code = resp.get("code") != null ? (Integer) resp.get("code") : 6001;
-            throw new CustomException("功能暂不可用,请联系管理员", code, resp);
+
+            // -- 实现方法 -------------------------------------------------------------------
+            OpenClient client = aiznService.getOpenClient();
+            CommonRequest request = new CommonRequest("/linkfox-ai/image/v1/make/style", RequestMethod.POST_JSON);
+            request.setBizContent(object.toString());
+
+            // -- 返回结果 -------------------------------------------------------------------
+            CommonResponse response = client.executeAppToken(request, aiznService.getAppToken());
+            JSONObject resp = aiznService.checkResponse(response);
+            if ((Integer) resp.get("code") == 200) {
+
+                // -- 成功 ------------------------------------------------------------------
+                List<Map<String, Object>> respData = (List<Map<String, Object>>) resp.get("data");
+                Map<String, Object> result = new LinkedHashMap<>();
+                result.put("type", type);
+                result.put("total", respData.size());
+                result.put("list", ListUtil.convertToUnderscoreCase(respData)); // 将返回结果:首字母大写命名 转换为 下划线命名
+
+                // [缓存不存在] 加入缓存
+                try {
+                    System.out.println("[缓存不存在] 加入缓存");
+                    ObjectMapper mapper = new ObjectMapper();
+                    String respDataStr = mapper.writeValueAsString(respData);
+                    System.out.println(respDataStr);
+                    redisCache.setCacheObject(redisKey, respDataStr);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+
+                return result;
+                // --------------------------------------------------------------------------
+
+            } else {
+                System.out.println(resp);
+                Integer code = resp.get("code") != null ? (Integer) resp.get("code") : 6001;
+                throw new CustomException("功能暂不可用,请联系管理员", code, resp);
+            }
+
         }
     }