perf:调整授权缓存

This commit is contained in:
曾文豪
2024-08-07 23:13:55 +08:00
parent 5e308be3ba
commit 316909dd61
2 changed files with 4 additions and 8 deletions

View File

@@ -26,7 +26,7 @@ import java.util.stream.Collectors;
@Component
public class RoleAuthorityAspect {
public static final String CACHE_HAS_AUTHORITY = "CACHE:HAS_AUTHORITY:{}";
public static final String CACHE_HAS_AUTHORITY = "CACHE:HAS_AUTHORITY:{}:{}";
@Resource
TsTokenConfig tsTokenConfig;
@@ -53,7 +53,7 @@ public class RoleAuthorityAspect {
String authority = StrUtil.join("_", classAnnotation.group(),
classAnnotation.value(), annotation.value());
String cacheKey = StrUtil.format(CACHE_HAS_AUTHORITY, tokenBean.getId());
String cacheKey = StrUtil.format(CACHE_HAS_AUTHORITY, tokenBean.getRoleId(), tokenBean.getId());
List<String> authorityList = StrUtil.split(TsCacheService.of().get(cacheKey), ";")
.stream().filter(StrUtil::isNotEmpty).collect(Collectors.toList());

View File

@@ -162,12 +162,8 @@ public class CoreRoleService extends TsServiceBase<CoreRoleGroupMapper, CoreRole
* 当授权发生变化时
*/
public void onRoleChange(String roleId, String userId) {
if (StrUtil.isNotEmpty(userId)) {
TsCacheService.of().remove(StrUtil.format(RoleAuthorityAspect.CACHE_HAS_AUTHORITY, userId));
} else {
TsCacheService.of().keys(StrUtil.replace(RoleAuthorityAspect.CACHE_HAS_AUTHORITY,
"{}", "")).forEach(key -> TsCacheService.of().remove(key));
}
TsCacheService.of().keys(StrUtil.format(RoleAuthorityAspect.CACHE_HAS_AUTHORITY,
roleId, userId)).forEach(key -> TsCacheService.of().remove(key));
}