66import com .xiaozhi .entity .SysRole ;
77import com .xiaozhi .service .SysRoleService ;
88import jakarta .annotation .Resource ;
9- import org .springframework .cache .annotation .CacheEvict ;
9+ import org .springframework .beans .factory .annotation .Autowired ;
10+ import org .springframework .cache .Cache ;
11+ import org .springframework .cache .CacheManager ;
1012import org .springframework .cache .annotation .Cacheable ;
1113import org .springframework .stereotype .Service ;
1214import org .springframework .transaction .annotation .Transactional ;
@@ -26,6 +28,9 @@ public class SysRoleServiceImpl extends BaseServiceImpl implements SysRoleServic
2628
2729 @ Resource
2830 private RoleMapper roleMapper ;
31+
32+ @ Autowired (required = false )
33+ private CacheManager cacheManager ;
2934
3035 /**
3136 * 添加角色
@@ -67,13 +72,28 @@ public List<SysRole> query(SysRole role, PageFilter pageFilter) {
6772 */
6873 @ Override
6974 @ Transactional (transactionManager = "transactionManager" )
70- @ CacheEvict (value = CACHE_NAME , key = "#role.roleId" , condition = "#role.roleId != null" )
7175 public int update (SysRole role ) {
7276 // 如果当前配置被设置为默认,则将同类型同用户的其他配置设置为非默认
7377 if (role .getIsDefault () != null && role .getIsDefault ().equals ("1" )) {
7478 roleMapper .resetDefault (role );
7579 }
76- return roleMapper .update (role );
80+
81+ int result = roleMapper .update (role );
82+
83+ // 如果更新成功且roleId不为空,直接将更新后的完整对象加载到缓存中
84+ if (result > 0 && role .getRoleId () != null && cacheManager != null ) {
85+ // 直接从数据库查询最新数据
86+ SysRole updatedRole = roleMapper .selectRoleById (role .getRoleId ());
87+ // 手动更新缓存
88+ if (updatedRole != null ) {
89+ Cache cache = cacheManager .getCache (CACHE_NAME );
90+ if (cache != null ) {
91+ cache .put (updatedRole .getRoleId (), updatedRole );
92+ }
93+ }
94+ }
95+
96+ return result ;
7797 }
7898
7999 @ Override
0 commit comments