增加缓存配置

dev_xd
haha 2025-06-16 23:22:25 +08:00
parent 602ec947e4
commit 0eb7aa4516
1 changed files with 72 additions and 18 deletions

View File

@ -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<FieldDef, String> 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);
}
});
}
}