代码优化

pull/702/head
Zheng Jie 2021-11-23 15:49:05 +08:00
parent 931ecb3ba7
commit b267258776
2 changed files with 9 additions and 11 deletions

View File

@ -35,7 +35,7 @@ public class UserCacheClean {
*/ */
public void cleanUserCache(String userName) { public void cleanUserCache(String userName) {
if (StringUtils.isNotEmpty(userName)) { if (StringUtils.isNotEmpty(userName)) {
UserDetailsServiceImpl.userDtoCache.remove(userName); UserDetailsServiceImpl.USER_DTO_CACHE.remove(userName);
} }
} }
@ -44,6 +44,6 @@ public class UserCacheClean {
* ,便 * ,便
*/ */
public void cleanAll() { public void cleanAll() {
UserDetailsServiceImpl.userDtoCache.clear(); UserDetailsServiceImpl.USER_DTO_CACHE.clear();
} }
} }

View File

@ -55,13 +55,13 @@ public class UserDetailsServiceImpl implements UserDetailsService {
* @see {@link UserCacheClean} * @see {@link UserCacheClean}
*/ */
final static Map<String, Future<JwtUserDto>> userDtoCache = new ConcurrentHashMap<>(); final static Map<String, Future<JwtUserDto>> USER_DTO_CACHE = new ConcurrentHashMap<>();
public static ExecutorService executor = newThreadPool(); public static ExecutorService executor = newThreadPool();
@Override @Override
public JwtUserDto loadUserByUsername(String username) { public JwtUserDto loadUserByUsername(String username) {
JwtUserDto jwtUserDto = null; JwtUserDto jwtUserDto = null;
Future<JwtUserDto> future = userDtoCache.get(username); Future<JwtUserDto> future = USER_DTO_CACHE.get(username);
if (!loginProperties.isCacheEnable()) { if (!loginProperties.isCacheEnable()) {
UserDto user; UserDto user;
try { try {
@ -86,9 +86,9 @@ public class UserDetailsServiceImpl implements UserDetailsService {
} }
if (future==null) { if (future==null) {
Callable<JwtUserDto> call=()->getJwtBySearchDB(username); Callable<JwtUserDto> call=()->getJwtBySearchDb(username);
FutureTask<JwtUserDto> ft=new FutureTask<>(call); FutureTask<JwtUserDto> ft=new FutureTask<>(call);
future=userDtoCache.putIfAbsent(username,ft); future=USER_DTO_CACHE.putIfAbsent(username,ft);
if(future==null){ if(future==null){
future=ft; future=ft;
executor.submit(ft); executor.submit(ft);
@ -96,7 +96,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
try{ try{
return future.get(); return future.get();
}catch(CancellationException e){ }catch(CancellationException e){
userDtoCache.remove(username); USER_DTO_CACHE.remove(username);
}catch (InterruptedException | ExecutionException e) { }catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e.getMessage()); throw new RuntimeException(e.getMessage());
} }
@ -116,8 +116,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
} }
private JwtUserDto getJwtBySearchDb(String username) {
private JwtUserDto getJwtBySearchDB(String username) {
UserDto user; UserDto user;
try { try {
user = userService.findByName(username); user = userService.findByName(username);
@ -131,12 +130,11 @@ public class UserDetailsServiceImpl implements UserDetailsService {
if (!user.getEnabled()) { if (!user.getEnabled()) {
throw new BadRequestException("账号未激活!"); throw new BadRequestException("账号未激活!");
} }
JwtUserDto jwtUserDto = new JwtUserDto( return new JwtUserDto(
user, user,
dataService.getDeptIds(user), dataService.getDeptIds(user),
roleService.mapToGrantedAuthorities(user) roleService.mapToGrantedAuthorities(user)
); );
return jwtUserDto;
} }
} }