From 0eb7aa4516d4346a64d745bd8de63dcf1cc8288b Mon Sep 17 00:00:00 2001 From: haha Date: Mon, 16 Jun 2025 23:22:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yanzhu/manage/hasor/HasorModule.java | 90 +++++++++++++++---- 1 file changed, 72 insertions(+), 18 deletions(-) diff --git a/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/hasor/HasorModule.java b/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/hasor/HasorModule.java index 1459ff91..f8686f32 100644 --- a/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/hasor/HasorModule.java +++ b/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/hasor/HasorModule.java @@ -1,19 +1,19 @@ package com.yanzhu.manage.hasor; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.yanzhu.common.core.utils.StringUtils; +import com.yanzhu.common.redis.service.RedisService; import com.yanzhu.common.security.utils.SecurityUtils; import com.yanzhu.system.api.domain.SysUser; import net.hasor.core.ApiBinder; import net.hasor.core.DimModule; import net.hasor.dataql.DataQL; import net.hasor.dataql.compiler.qil.QIL; -import net.hasor.dataway.DatawayApi; -import net.hasor.dataway.authorization.PermissionType; import net.hasor.dataway.dal.FieldDef; import net.hasor.dataway.service.InterfaceApiFilter; -import net.hasor.dataway.spi.ApiInfo; -import net.hasor.dataway.spi.AuthorizationChainSpi; -import net.hasor.dataway.spi.CompilerSpiListener; -import net.hasor.dataway.spi.PreExecuteChainSpi; +import net.hasor.dataway.spi.*; import net.hasor.db.JdbcModule; import net.hasor.db.Level; import net.hasor.spring.SpringModule; @@ -22,9 +22,7 @@ import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.io.IOException; -import java.lang.reflect.Method; import java.util.Map; -import java.util.Set; @DimModule @Component @@ -32,20 +30,60 @@ public class HasorModule implements SpringModule { @Autowired private DataSource dataSource = null; + @Autowired + private RedisService redisService; + + private boolean isCache(ApiInfo apiInfo) { + Map map = apiInfo.getObj(); + if (map == null) { + return false; + } + String header = map.get(FieldDef.REQ_HEADER_SAMPLE); + if (StringUtils.isBlank(header)) { + return false; + } + try { + JSONArray jsonObject = JSON.parseArray(header); + for (int i = 0; i < jsonObject.size(); i++) { + JSONObject obj = jsonObject.getJSONObject(i); + if ("cache".equals(obj.getString("name"))) { + String value = obj.getString("value"); + return "1".equals(value) || "true".equals(value); + } + } + + + } catch (Exception ex) { + + } + return false; + } + + public String getCacheKey(ApiInfo apiInfo) { + return "hasor-" + apiInfo.getApiPath() + JSON.toJSONString(apiInfo.getParameterMap()); + } + public void loadModule(ApiBinder apiBinder) throws Throwable { // .DataSource form Spring boot into Hasor apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource)); InterfaceApiFilter f; apiBinder.bindSpiListener(PreExecuteChainSpi.class, (apiInfo, future) -> { - try{ + try { SysUser user = SecurityUtils.getLoginUser().getSysUser(); - if(user!=null) { + if (user != null) { apiInfo.getParameterMap().put("currentUser", user.getUserName() + " " + user.getPhonenumber()); - }else{ + } else { apiInfo.getParameterMap().put("currentUser", "system"); } - }catch (Exception ex){ + if (isCache(apiInfo)) { + String key = getCacheKey(apiInfo); + Object obj = redisService.getCacheObject(key); + if (obj != null) { + future.completed(obj); + } + } + } catch (Exception ex) { } //String apiPath = apiInfo.getApiPath(); @@ -58,12 +96,28 @@ public class HasorModule implements SpringModule { // } }); apiBinder.bindSpiListener(CompilerSpiListener.class, new CompilerSpiListener() { - @Override - public QIL compiler(ApiInfo apiInfo, String query, DataQL dataQL) throws IOException { - query = "hint FRAGMENT_SQL_COLUMN_CASE=\"hump\"\n hint FRAGMENT_SQL_QUERY_BY_PAGE_NUMBER_OFFSET = 1 \n hint FRAGMENT_SQL_OPEN_PACKAGE = 'off' \n" + query; - return CompilerSpiListener.super.compiler(apiInfo, query, dataQL); - } - }); + @Override + public QIL compiler(ApiInfo apiInfo, String query, DataQL dataQL) throws IOException { + query = "hint FRAGMENT_SQL_COLUMN_CASE=\"hump\"\n hint FRAGMENT_SQL_QUERY_BY_PAGE_NUMBER_OFFSET = 1 \n hint FRAGMENT_SQL_OPEN_PACKAGE = 'off' \n" + query; + return CompilerSpiListener.super.compiler(apiInfo, query, dataQL); + } + }); + apiBinder.bindSpiListener(ResultProcessChainSpi.class, new ResultProcessChainSpi() { + @Override + public Object callAfter(boolean formPre, ApiInfo apiInfo, Object result) { + if (formPre || !isCache(apiInfo) || apiInfo.getCallSource() == CallSource.InterfaceUI) { + return ResultProcessChainSpi.super.callAfter(formPre, apiInfo, result); + } + try { + String key = getCacheKey(apiInfo); + redisService.setCacheObject(key, result, 60L, java.util.concurrent.TimeUnit.MINUTES); + } catch (Exception ex) { + } + return ResultProcessChainSpi.super.callAfter(formPre, apiInfo, result); + } + }); } + + }