update 优化 使用 spring-cache 注解优化缓存

This commit is contained in:
疯狂的狮子li
2022-08-15 10:59:20 +08:00
parent 32cd07d9a2
commit 74e075b4d3
18 changed files with 189 additions and 137 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.CacheConstants;
import com.ruoyi.common.core.constant.CacheNames;
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.RedisUtils;
import com.ruoyi.common.redis.utils.CacheUtils;
import com.ruoyi.common.satoken.utils.LoginHelper;
import com.ruoyi.system.api.domain.SysUserOnline;
import com.ruoyi.system.api.model.LoginUser;
@@ -18,8 +18,6 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.time.Duration;
/**
* 用户行为 侦听器的实现
*
@@ -53,7 +51,12 @@ public class UserActionListener implements SaTokenListener {
if (ObjectUtil.isNotNull(user.getDeptName())) {
userOnline.setDeptName(user.getDeptName());
}
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline, 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, userOnline);
log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue);
} else if (userType == UserType.APP_USER) {
// app端 自行根据业务编写
@@ -65,7 +68,7 @@ public class UserActionListener implements SaTokenListener {
*/
@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, useId:{}, token:{}", loginId, tokenValue);
}
@@ -74,7 +77,7 @@ public class UserActionListener implements SaTokenListener {
*/
@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, useId:{}, token:{}", loginId, tokenValue);
}
@@ -83,7 +86,7 @@ public class UserActionListener implements SaTokenListener {
*/
@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, useId:{}, token:{}", loginId, tokenValue);
}