代码优化

This commit is contained in:
卢春梦 2024-09-12 11:16:22 +08:00
parent b5efed073b
commit 0bb1832964
2 changed files with 12 additions and 26 deletions

View File

@ -67,7 +67,11 @@ public class CacheUtil {
if (Func.hasEmpty(cacheName, keyPrefix, key)) { if (Func.hasEmpty(cacheName, keyPrefix, key)) {
return null; 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)) { if (Func.hasEmpty(cacheName, keyPrefix, key)) {
return null; return null;
} }
try { String cacheKey = keyPrefix.concat(String.valueOf(key));
Cache.ValueWrapper valueWrapper = getCache(cacheName).get(keyPrefix.concat(String.valueOf(key))); return getCache(cacheName).get(cacheKey, valueLoader);
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;
}
} }
/** /**

View File

@ -60,7 +60,7 @@ public class BladeScopeModelHandler implements ScopeModelHandler {
// 后续若有新增配置则会清空缓存重新加载 // 后续若有新增配置则会清空缓存重新加载
DataScopeModel dataScope = CacheUtil.get(SYS_CACHE, SCOPE_CACHE_CLASS, mapperId + StringPool.COLON + roleId, DataScopeModel.class); DataScopeModel dataScope = CacheUtil.get(SYS_CACHE, SCOPE_CACHE_CLASS, mapperId + StringPool.COLON + roleId, DataScopeModel.class);
if (dataScope == null || !dataScope.getSearched()) { if (dataScope == null || !dataScope.getSearched()) {
List<DataScopeModel> list = jdbcTemplate.query(DataScopeConstant.dataByMapper(roleIds.size()), args.toArray(), new BeanPropertyRowMapper<>(DataScopeModel.class)); List<DataScopeModel> list = jdbcTemplate.query(DataScopeConstant.dataByMapper(roleIds.size()), new BeanPropertyRowMapper<>(DataScopeModel.class), args.toArray());
if (CollectionUtil.isNotEmpty(list)) { if (CollectionUtil.isNotEmpty(list)) {
dataScope = list.iterator().next(); dataScope = list.iterator().next();
dataScope.setSearched(Boolean.TRUE); dataScope.setSearched(Boolean.TRUE);
@ -84,7 +84,7 @@ public class BladeScopeModelHandler implements ScopeModelHandler {
// 增加searched字段防止未配置的参数重复读库导致缓存击穿 // 增加searched字段防止未配置的参数重复读库导致缓存击穿
// 后续若有新增配置则会清空缓存重新加载 // 后续若有新增配置则会清空缓存重新加载
if (dataScope == null || !dataScope.getSearched()) { if (dataScope == null || !dataScope.getSearched()) {
List<DataScopeModel> list = jdbcTemplate.query(DataScopeConstant.DATA_BY_CODE, new Object[]{code}, new BeanPropertyRowMapper<>(DataScopeModel.class)); List<DataScopeModel> list = jdbcTemplate.query(DataScopeConstant.DATA_BY_CODE, new BeanPropertyRowMapper<>(DataScopeModel.class), code);
if (CollectionUtil.isNotEmpty(list)) { if (CollectionUtil.isNotEmpty(list)) {
dataScope = list.iterator().next(); dataScope = list.iterator().next();
dataScope.setSearched(Boolean.TRUE); dataScope.setSearched(Boolean.TRUE);
@ -104,11 +104,8 @@ public class BladeScopeModelHandler implements ScopeModelHandler {
*/ */
@Override @Override
public List<Long> getDeptAncestors(Long deptId) { public List<Long> getDeptAncestors(Long deptId) {
List ancestors = CacheUtil.get(SYS_CACHE, DEPT_CACHE_ANCESTORS, deptId, List.class); return CacheUtil.get(SYS_CACHE, DEPT_CACHE_ANCESTORS, deptId, () ->
if (CollectionUtil.isEmpty(ancestors)) { jdbcTemplate.queryForList(DataScopeConstant.DATA_BY_DEPT, Long.class, deptId)
ancestors = jdbcTemplate.queryForList(DataScopeConstant.DATA_BY_DEPT, new Object[]{deptId}, Long.class); );
CacheUtil.put(SYS_CACHE, DEPT_CACHE_ANCESTORS, deptId, ancestors);
}
return ancestors;
} }
} }