From 8837119aad5ca91747fc198fcca51090c3ca7cf6 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期日, 14 八月 2022 01:50:00 +0800 Subject: [PATCH] update 优化 在线用户功能 使用 spring-cache 管理 --- ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java | 19 +++++++++++-------- 1 files changed, 11 insertions(+), 8 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java index 1e3c22c..d6268eb 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java @@ -5,19 +5,17 @@ import cn.dev33.satoken.stp.SaLoginModel; import cn.hutool.http.useragent.UserAgent; import cn.hutool.http.useragent.UserAgentUtil; -import com.ruoyi.common.constant.CacheConstants; +import com.ruoyi.common.constant.CacheNames; import com.ruoyi.common.core.domain.dto.UserOnlineDTO; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.enums.UserType; import com.ruoyi.common.helper.LoginHelper; import com.ruoyi.common.utils.ServletUtils; +import com.ruoyi.common.utils.redis.CacheUtils; import com.ruoyi.common.utils.ip.AddressUtils; -import com.ruoyi.common.utils.redis.RedisUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; - -import java.time.Duration; /** * 鐢ㄦ埛琛屼负 渚﹀惉鍣ㄧ殑瀹炵幇 @@ -50,7 +48,12 @@ dto.setTokenId(tokenValue); dto.setUserName(user.getUsername()); dto.setDeptName(user.getDeptName()); - RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(tokenConfig.getTimeout())); + String cacheNames = CacheNames.ONLINE_TOKEN; + if (tokenConfig.getTimeout() > 0) { + // 澧炲姞 ttl 杩囨湡鏃堕棿 鍗曚綅绉� + cacheNames = CacheNames.ONLINE_TOKEN + "#" + tokenConfig.getTimeout() + "s"; + } + CacheUtils.put(cacheNames, tokenValue, dto); log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue); } else if (userType == UserType.APP_USER) { // app绔� 鑷鏍规嵁涓氬姟缂栧啓 @@ -62,7 +65,7 @@ */ @Override public void doLogout(String loginType, Object loginId, String tokenValue) { - RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue); + CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue); log.info("user doLogout, userId:{}, token:{}", loginId, tokenValue); } @@ -71,7 +74,7 @@ */ @Override public void doKickout(String loginType, Object loginId, String tokenValue) { - RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue); + CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue); log.info("user doLogoutByLoginId, userId:{}, token:{}", loginId, tokenValue); } @@ -80,7 +83,7 @@ */ @Override public void doReplaced(String loginType, Object loginId, String tokenValue) { - RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue); + CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue); log.info("user doReplaced, userId:{}, token:{}", loginId, tokenValue); } -- Gitblit v1.9.3