Browse Source

🎉 优化数据权限缓存逻辑

test v2.7.2.release
smallchill 4 years ago
parent
commit
2740b774c7
  1. 9
      blade-service-api/blade-scope-api/src/main/java/org/springblade/system/cache/DataScopeCache.java
  2. 16
      blade-service/blade-system/src/main/java/org/springblade/system/feign/DataScopeClient.java

9
blade-service-api/blade-scope-api/src/main/java/org/springblade/system/cache/DataScopeCache.java vendored

@ -21,6 +21,7 @@ import org.springblade.core.datascope.model.DataScopeModel;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.system.feign.IDataScopeClient;
import java.util.List;
@ -56,11 +57,11 @@ public class DataScopeCache {
*/
public static DataScopeModel getDataScopeByMapper(String mapperId, String roleId) {
DataScopeModel dataScope = CacheUtil.get(SYS_CACHE, SCOPE_CACHE_CLASS, mapperId + StringPool.COLON + roleId, DataScopeModel.class);
if (dataScope == null) {
if (dataScope == null || !dataScope.getSearched()) {
dataScope = getDataScopeClient().getDataScopeByMapper(mapperId, roleId);
CacheUtil.put(SYS_CACHE, SCOPE_CACHE_CLASS, mapperId + StringPool.COLON + roleId, dataScope);
}
return dataScope;
return StringUtil.isNotBlank(dataScope.getResourceCode()) ? dataScope : null;
}
/**
@ -71,11 +72,11 @@ public class DataScopeCache {
*/
public static DataScopeModel getDataScopeByCode(String code) {
DataScopeModel dataScope = CacheUtil.get(SYS_CACHE, SCOPE_CACHE_CODE, code, DataScopeModel.class);
if (dataScope == null) {
if (dataScope == null || !dataScope.getSearched()) {
dataScope = getDataScopeClient().getDataScopeByCode(code);
CacheUtil.put(SYS_CACHE, SCOPE_CACHE_CODE, code, dataScope);
}
return dataScope;
return StringUtil.isNotBlank(dataScope.getResourceCode()) ? dataScope : null;
}
/**

16
blade-service/blade-system/src/main/java/org/springblade/system/feign/DataScopeClient.java

@ -43,6 +43,8 @@ import java.util.List;
@RequiredArgsConstructor
public class DataScopeClient implements IDataScopeClient {
private static final DataScopeModel SEARCHED_DATA_SCOPE_MODEL = new DataScopeModel(Boolean.TRUE);
private final JdbcTemplate jdbcTemplate;
/**
@ -58,10 +60,15 @@ public class DataScopeClient implements IDataScopeClient {
List<Object> args = new ArrayList<>(Collections.singletonList(mapperId));
List<Long> roleIds = Func.toLongList(roleId);
args.addAll(roleIds);
DataScopeModel dataScope = null;
// 增加searched字段防止未配置的参数重复读库导致缓存击穿
// 后续若有新增配置则会清空缓存重新加载
DataScopeModel dataScope;
List<DataScopeModel> list = jdbcTemplate.query(DataScopeConstant.dataByMapper(roleIds.size()), args.toArray(), new BeanPropertyRowMapper<>(DataScopeModel.class));
if (CollectionUtil.isNotEmpty(list)) {
dataScope = list.iterator().next();
dataScope.setSearched(Boolean.TRUE);
} else {
dataScope = SEARCHED_DATA_SCOPE_MODEL;
}
return dataScope;
}
@ -75,10 +82,15 @@ public class DataScopeClient implements IDataScopeClient {
@Override
@GetMapping(GET_DATA_SCOPE_BY_CODE)
public DataScopeModel getDataScopeByCode(String code) {
DataScopeModel dataScope = null;
// 增加searched字段防止未配置的参数重复读库导致缓存击穿
// 后续若有新增配置则会清空缓存重新加载
DataScopeModel dataScope;
List<DataScopeModel> list = jdbcTemplate.query(DataScopeConstant.DATA_BY_CODE, new Object[]{code}, new BeanPropertyRowMapper<>(DataScopeModel.class));
if (CollectionUtil.isNotEmpty(list)) {
dataScope = list.iterator().next();
dataScope.setSearched(Boolean.TRUE);
} else {
dataScope = SEARCHED_DATA_SCOPE_MODEL;
}
return dataScope;
}

Loading…
Cancel
Save