fix 修正 spring cache 获取在线用户失败问题, 回退为使用 redis 工具缓存 ;

update 优化 DictUtils, 使用 CacheUtils 缓存字典数据 ;
This commit is contained in:
zlyx
2022-08-30 12:36:18 +08:00
parent 4c48108625
commit cb1e93d3f5
3 changed files with 16 additions and 37 deletions

View File

@@ -6,11 +6,11 @@ import cn.dev33.satoken.stp.SaLoginModel;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.ruoyi.common.core.constant.CacheNames;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.enums.UserType;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.ip.AddressUtils;
import com.ruoyi.common.redis.utils.CacheUtils;
import com.ruoyi.common.redis.utils.RedisUtils;
import com.ruoyi.common.satoken.utils.LoginHelper;
import com.ruoyi.system.api.domain.SysUserOnline;
import com.ruoyi.system.api.model.LoginUser;
@@ -18,6 +18,8 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.time.Duration;
/**
* 用户行为 侦听器的实现
*
@@ -51,12 +53,7 @@ public class UserActionListener implements SaTokenListener {
if (ObjectUtil.isNotNull(user.getDeptName())) {
userOnline.setDeptName(user.getDeptName());
}
String cacheNames = CacheNames.ONLINE_TOKEN;
if (tokenConfig.getTimeout() > 0) {
// 增加 ttl 过期时间 单位秒
cacheNames = CacheNames.ONLINE_TOKEN + "#" + tokenConfig.getTimeout() + "s";
}
CacheUtils.put(cacheNames, tokenValue, userOnline);
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline, Duration.ofSeconds(tokenConfig.getTimeout()));
log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue);
} else if (userType == UserType.APP_USER) {
// app端 自行根据业务编写
@@ -68,7 +65,7 @@ public class UserActionListener implements SaTokenListener {
*/
@Override
public void doLogout(String loginType, Object loginId, String tokenValue) {
CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue);
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
log.info("user doLogout, useId:{}, token:{}", loginId, tokenValue);
}
@@ -77,7 +74,7 @@ public class UserActionListener implements SaTokenListener {
*/
@Override
public void doKickout(String loginType, Object loginId, String tokenValue) {
CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue);
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
log.info("user doLogoutByLoginId, useId:{}, token:{}", loginId, tokenValue);
}
@@ -86,7 +83,7 @@ public class UserActionListener implements SaTokenListener {
*/
@Override
public void doReplaced(String loginType, Object loginId, String tokenValue) {
CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue);
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
log.info("user doReplaced, useId:{}, token:{}", loginId, tokenValue);
}