REM:
1. 优化代码
This commit is contained in:
HuangXin 2020-05-14 17:49:26 +08:00
parent 41029fd9c3
commit 4f16bc1dcc
1 changed files with 10 additions and 9 deletions

View File

@ -242,13 +242,15 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
*/ */
@Override @Override
public String getUserToken(String username) throws NoSuchAlgorithmException { public String getUserToken(String username) throws NoSuchAlgorithmException {
UserAccountCache uc;
// 根据用户名在缓存中查找用户 // 根据用户名在缓存中查找用户
Optional<UserAccountCache> findRet = userAccountCache.values().stream() Optional<UserAccountCache> findRet = userAccountCache.values().stream()
.filter(userAccountCache -> username.equals(userAccountCache.getUsername())) .filter(userAccountCache -> username.equals(userAccountCache.getUsername()))
.findFirst(); .findFirst();
if (findRet.isPresent()) { if (findRet.isPresent()) {
UserAccountCache uc = findRet.get(); uc = findRet.get();
// token过期获取以前没有token创建一个新token // token过期获取以前没有token创建一个新token
if ((System.currentTimeMillis() - uc.getLastAccess()) if ((System.currentTimeMillis() - uc.getLastAccess())
@ -262,10 +264,9 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
uc.setLastAccess(System.currentTimeMillis()); uc.setLastAccess(System.currentTimeMillis());
return uc.getToken();
} else { } else {
// 用户不存在说明没有登陆过创建一个新的用户缓存 // 用户不存在说明没有登陆过创建一个新的用户缓存
UserAccountCache uc = UserAccountCache.builder() uc = UserAccountCache.builder()
.username(username) .username(username)
.token(createUserToken(username)) .token(createUserToken(username))
.lastAccess(System.currentTimeMillis()) .lastAccess(System.currentTimeMillis())
@ -276,8 +277,8 @@ public class UserAccountCacheManagerImpl implements UserAccountCacheManager {
log.info("Create {} Token:{}", username, uc.getToken()); log.info("Create {} Token:{}", username, uc.getToken());
return uc.getToken();
} }
return uc.getToken();
} }
/** /**