Skip to content

Commit 0e41572

Browse files
committed
fix:修复设备在聆听时,修改角色配置导致缓存更新时多次查询数据库的问题
1 parent 1461fe1 commit 0e41572

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/main/java/com/xiaozhi/service/impl/SysRoleServiceImpl.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import com.xiaozhi.entity.SysRole;
77
import com.xiaozhi.service.SysRoleService;
88
import 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;
1012
import org.springframework.cache.annotation.Cacheable;
1113
import org.springframework.stereotype.Service;
1214
import 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

Comments
 (0)