From 03fa174c70f07e8f5c0795dfbadfc9f066f5d838 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Tue, 20 May 2025 17:05:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[=E4=BF=AE=E5=A4=8D]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=AF=E4=BB=A5=E5=9C=A8=E9=AB=98=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E5=9C=BA=E6=99=AF=E4=B8=8B=E9=98=B2=E6=AD=A2=E8=A2=AB?= =?UTF-8?q?=E5=87=BB=E7=A9=BF=E7=9A=84Mybaties=E4=BA=8C=E7=BA=A7=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1421727635046400]增加一个可以在高并发场景下防止被击穿的Mybaties二级缓存 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1421727635046400 --- .../dao/plugin/DataSchemaInterceptor.java | 4 ++++ .../dao/plugin/SqlCostInterceptor.java | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java b/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java index 9e1f7715e..6ec88b721 100644 --- a/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java +++ b/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java @@ -31,9 +31,13 @@ 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 { + System.out.println("DataSchemaInterceptor = "); + 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 c46638d00..fd678ec75 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()); -- Gitee From 8f1404e4c3430ea5b66b405086e99f2ea7e42eee Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Tue, 20 May 2025 17:08:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[=E4=BF=AE=E5=A4=8D]=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=AF=E4=BB=A5=E5=9C=A8=E9=AB=98=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E5=9C=BA=E6=99=AF=E4=B8=8B=E9=98=B2=E6=AD=A2=E8=A2=AB?= =?UTF-8?q?=E5=87=BB=E7=A9=BF=E7=9A=84Mybaties=E4=BA=8C=E7=BA=A7=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1421727635046400]增加一个可以在高并发场景下防止被击穿的Mybaties二级缓存 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1421727635046400 --- .../neatlogic/framework/dao/plugin/DataSchemaInterceptor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java b/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java index 6ec88b721..761374c78 100644 --- a/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java +++ b/src/main/java/neatlogic/framework/dao/plugin/DataSchemaInterceptor.java @@ -36,7 +36,6 @@ public class DataSchemaInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { - System.out.println("DataSchemaInterceptor = "); QUERY_FROM_DATABASE_INSTANCE.set(true); StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); -- Gitee