修复在用户管理中修改用户的角色时缓存不刷新的问题

pull/62/head
zhengjie 2019-05-08 15:13:41 +08:00
parent 5f43a5dabc
commit 0baad04445
6 changed files with 37 additions and 2 deletions

View File

@ -74,7 +74,6 @@ public class RedisServiceImpl implements RedisService {
jedis.close(); // 释放资源还给连接池
}
}
}
@Override

View File

@ -20,6 +20,11 @@ public class JwtPermissionService {
@Autowired
private RoleRepository roleRepository;
/**
* key UserServiceImpl update
* @param user
* @return
*/
@Cacheable(key = "'loadPermissionByUser:' + #p0.username")
public Collection<GrantedAuthority> mapToGrantedAuthorities(User user) {

View File

@ -10,6 +10,7 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Objects;
import java.util.Set;
/**
@ -69,5 +70,18 @@ public class Role implements Serializable {
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Role role = (Role) o;
return Objects.equals(id, role.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public interface Update{}
}

View File

@ -48,11 +48,12 @@ public interface RoleService {
void delete(Long id);
/**
* key UserServiceImpl update
* findByUsers_Id
* @param id
* @return
*/
@Cacheable(keyGenerator = "keyGenerator")
@Cacheable(key = "'findByUsers_Id:' + #p0")
List<Role> findByUsers_Id(Long id);
/**

View File

@ -1,5 +1,6 @@
package me.zhengjie.modules.system.service.impl;
import me.zhengjie.modules.monitor.service.RedisService;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.exception.EntityNotFoundException;
@ -29,6 +30,9 @@ public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private RedisService redisService;
@Override
public UserDTO findById(long id) {
Optional<User> user = userRepository.findById(id);
@ -73,6 +77,14 @@ public class UserServiceImpl implements UserService {
throw new EntityExistException(User.class,"email",resources.getEmail());
}
// 如果用户的角色改变了,需要手动清理下缓存
if (!resources.getRoles().equals(user.getRoles())) {
String key = "role::loadPermissionByUser:" + user.getUsername();
redisService.delete(key);
key = "role::findByUsers_Id:" + user.getId();
redisService.delete(key);
}
user.setUsername(resources.getUsername());
user.setEmail(resources.getEmail());
user.setEnabled(resources.getEnabled());

View File

@ -1,13 +1,17 @@
package me.zhengjie;
import me.zhengjie.modules.monitor.service.RedisService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EladminSystemApplicationTests {
@Autowired
private RedisService redisService;
@Test
public void contextLoads() {