diff --git a/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java b/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java index 9e1f7715e1c498bf2cee24b914b4e13efd71a5bb..761374c78b8d85f0786a6fbf97acfd866aa5b94d 100644 --- a/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java +++ b/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java @@ -31,9 +31,12 @@ import java.sql.Connection; */ @Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})}) public class DataSchemaInterceptor implements Interceptor { + // 判断是否查询了数据库 + public static final ThreadLocal QUERY_FROM_DATABASE_INSTANCE = new ThreadLocal<>(); @Override public Object intercept(Invocation invocation) throws Throwable { + QUERY_FROM_DATABASE_INSTANCE.set(true); StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); BoundSql boundSql = statementHandler.getBoundSql(); diff --git a/src/main/java/neatlogic/framework/dao/plugin/SqlCostInterceptor.java b/src/main/java/neatlogic/framework/dao/plugin/SqlCostInterceptor.java index c46638d003b3ce08a6e6d31e9b142c09a0e6e5d0..fd678ec7571e46f324b04a89a7e259d618760617 100644 --- a/src/main/java/neatlogic/framework/dao/plugin/SqlCostInterceptor.java +++ b/src/main/java/neatlogic/framework/dao/plugin/SqlCostInterceptor.java @@ -21,6 +21,7 @@ import neatlogic.framework.asynchronization.threadlocal.UserContext; import neatlogic.framework.dto.healthcheck.SqlAuditVo; import neatlogic.framework.healthcheck.SqlAuditManager; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; @@ -92,6 +93,7 @@ public class SqlCostInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { long starttime = 0; SqlAuditVo sqlAuditVo = null; + boolean hasCacheFirstLevel = false; try { if (!SqlIdMap.isEmpty()) { // Object target = invocation.getTarget(); @@ -131,10 +133,8 @@ public class SqlCostInterceptor implements Interceptor { RowBounds rowBounds = (RowBounds) args[2]; key = executor.createCacheKey(mappedStatement, parameterObject, rowBounds, mappedStatement.getBoundSql(parameterObject)); } - if (mappedStatement.getCache() != null && mappedStatement.getCache().getObject(key) != null) { - sqlAuditVo.setUseCacheLevel("二级缓存"); - } else if (executor.isCached(mappedStatement, key)) { - sqlAuditVo.setUseCacheLevel("一级级缓存"); + if (executor.isCached(mappedStatement, key)) { + hasCacheFirstLevel = true; } } } @@ -142,9 +142,20 @@ public class SqlCostInterceptor implements Interceptor { } catch (Exception e) { logger.error(e.getMessage(), e); } + DataSchemaInterceptor.QUERY_FROM_DATABASE_INSTANCE.set(false); // 执行完上面的任务后,不改变原有的sql执行过程 Object val = invocation.proceed(); if (sqlAuditVo != null) { + if (DataSchemaInterceptor.QUERY_FROM_DATABASE_INSTANCE.get()) { + // sql语句被执行,没有使用到缓存 + sqlAuditVo.setUseCacheLevel(StringUtils.EMPTY); + } else { + if (hasCacheFirstLevel) { + sqlAuditVo.setUseCacheLevel("一级缓存"); + } else { + sqlAuditVo.setUseCacheLevel("二级缓存"); + } + } sqlAuditVo.setTimeCost(System.currentTimeMillis() - starttime); sqlAuditVo.setRunTime(new Date());