From 0bb1832964a885c9256d4502b034eca66317211c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Thu, 12 Sep 2024 11:16:22 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tool/utils/CacheUtil.java | 25 ++++++------------- .../handler/BladeScopeModelHandler.java | 13 ++++------ 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/CacheUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/CacheUtil.java index 0dfcb7d..74cedcb 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/CacheUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/CacheUtil.java @@ -67,7 +67,11 @@ public class CacheUtil { if (Func.hasEmpty(cacheName, keyPrefix, key)) { return null; } - return getCache(cacheName).get(keyPrefix.concat(String.valueOf(key))).get(); + Cache.ValueWrapper wrapper = getCache(cacheName).get(keyPrefix.concat(String.valueOf(key))); + if (wrapper == null) { + return null; + } + return wrapper.get(); } /** @@ -103,23 +107,8 @@ public class CacheUtil { if (Func.hasEmpty(cacheName, keyPrefix, key)) { return null; } - try { - Cache.ValueWrapper valueWrapper = getCache(cacheName).get(keyPrefix.concat(String.valueOf(key))); - Object value = null; - if (valueWrapper == null) { - T call = valueLoader.call(); - if (Func.isNotEmpty(call)) { - getCache(cacheName).put(keyPrefix.concat(String.valueOf(key)), call); - value = call; - } - } else { - value = valueWrapper.get(); - } - return (T) value; - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } + String cacheKey = keyPrefix.concat(String.valueOf(key)); + return getCache(cacheName).get(cacheKey, valueLoader); } /** diff --git a/blade-starter-datascope/src/main/java/org/springblade/core/datascope/handler/BladeScopeModelHandler.java b/blade-starter-datascope/src/main/java/org/springblade/core/datascope/handler/BladeScopeModelHandler.java index 8666432..96e3ca4 100644 --- a/blade-starter-datascope/src/main/java/org/springblade/core/datascope/handler/BladeScopeModelHandler.java +++ b/blade-starter-datascope/src/main/java/org/springblade/core/datascope/handler/BladeScopeModelHandler.java @@ -60,7 +60,7 @@ public class BladeScopeModelHandler implements ScopeModelHandler { // 后续若有新增配置则会清空缓存重新加载 DataScopeModel dataScope = CacheUtil.get(SYS_CACHE, SCOPE_CACHE_CLASS, mapperId + StringPool.COLON + roleId, DataScopeModel.class); if (dataScope == null || !dataScope.getSearched()) { - List list = jdbcTemplate.query(DataScopeConstant.dataByMapper(roleIds.size()), args.toArray(), new BeanPropertyRowMapper<>(DataScopeModel.class)); + List list = jdbcTemplate.query(DataScopeConstant.dataByMapper(roleIds.size()), new BeanPropertyRowMapper<>(DataScopeModel.class), args.toArray()); if (CollectionUtil.isNotEmpty(list)) { dataScope = list.iterator().next(); dataScope.setSearched(Boolean.TRUE); @@ -84,7 +84,7 @@ public class BladeScopeModelHandler implements ScopeModelHandler { // 增加searched字段防止未配置的参数重复读库导致缓存击穿 // 后续若有新增配置则会清空缓存重新加载 if (dataScope == null || !dataScope.getSearched()) { - List list = jdbcTemplate.query(DataScopeConstant.DATA_BY_CODE, new Object[]{code}, new BeanPropertyRowMapper<>(DataScopeModel.class)); + List list = jdbcTemplate.query(DataScopeConstant.DATA_BY_CODE, new BeanPropertyRowMapper<>(DataScopeModel.class), code); if (CollectionUtil.isNotEmpty(list)) { dataScope = list.iterator().next(); dataScope.setSearched(Boolean.TRUE); @@ -104,11 +104,8 @@ public class BladeScopeModelHandler implements ScopeModelHandler { */ @Override public List getDeptAncestors(Long deptId) { - List ancestors = CacheUtil.get(SYS_CACHE, DEPT_CACHE_ANCESTORS, deptId, List.class); - if (CollectionUtil.isEmpty(ancestors)) { - ancestors = jdbcTemplate.queryForList(DataScopeConstant.DATA_BY_DEPT, new Object[]{deptId}, Long.class); - CacheUtil.put(SYS_CACHE, DEPT_CACHE_ANCESTORS, deptId, ancestors); - } - return ancestors; + return CacheUtil.get(SYS_CACHE, DEPT_CACHE_ANCESTORS, deptId, () -> + jdbcTemplate.queryForList(DataScopeConstant.DATA_BY_DEPT, Long.class, deptId) + ); } }