This commit is contained in:
caozehui
2024-11-26 10:03:29 +08:00
parent 8637eb946c
commit ce2a8c3bc6
3 changed files with 19 additions and 10 deletions

View File

@@ -3,7 +3,6 @@ package com.njcn.common.bean;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUnit;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.stereotype.Component;
import java.util.Objects;
@@ -19,7 +18,7 @@ public class CustomCacheUtil {
//初始化一个定时缓存
this.timedCache = initTimedCache();
//启动定时任务每60秒清理一次过期条目
this.timedCache.schedulePrune(60*1000);
this.timedCache.schedulePrune(60 * 1000);
}
/**
@@ -38,7 +37,7 @@ public class CustomCacheUtil {
//初始化一个定时缓存
this.timedCache = initTimedCache();
//启动定时任务每60秒清理一次过期条目
timedCache.schedulePrune(60*1000);
timedCache.schedulePrune(60 * 1000);
}
timedCache.put(key, value);
}
@@ -52,7 +51,7 @@ public class CustomCacheUtil {
//初始化一个定时缓存
this.timedCache = initTimedCache();
//启动定时任务每60秒清理一次过期条目
timedCache.schedulePrune(60*1000);
timedCache.schedulePrune(60 * 1000);
}
timedCache.put(key, value, expireTime);
}
@@ -60,16 +59,25 @@ public class CustomCacheUtil {
/**
* 获取缓存值,
* @param key 缓存key
*
* @param key 缓存key
* @param flush 是否重置生命周期 true 重置false 不重置
*/
public String get(String key,boolean flush) {
public String get(String key, boolean flush) {
if (Objects.isNull(timedCache)) {
//初始化一个定时缓存
this.timedCache = initTimedCache();
//启动定时任务每60秒清理一次过期条目
timedCache.schedulePrune(60*1000);
timedCache.schedulePrune(60 * 1000);
}
return timedCache.get(key,flush);
return timedCache.get(key, flush);
}
public void remove(String key) {
if (Objects.isNull(timedCache)) {
this.timedCache = initTimedCache();
timedCache.schedulePrune(60 * 1000);
}
timedCache.remove(key);
}
}

View File

@@ -42,7 +42,7 @@ public interface SecurityConstants {
/**
* 认证成功后,返回信息包含的内容-
*/
String USER_INDEX_KEY = "userIndex";
String USER_ID = "userId";
String USER_TYPE = "userType";
String USER_NAME_KEY = "user_name";
String USER_HEAD_KEY = "headSculpture";

View File

@@ -3,6 +3,7 @@ package com.njcn.common.utils;
import cn.hutool.core.date.DateUnit;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.njcn.common.pojo.constant.SecurityConstants;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
@@ -36,7 +37,7 @@ public class JwtUtil {
headers.put("typ", "JWT");
headers.put("alg", "HS256");
Map<String, Object> payload = new HashMap<>();
payload.put("userId", userId);
payload.put(SecurityConstants.USER_ID, userId);
payload.put("exp", Instant.now().plusSeconds(ACCESS_EXPIRE).getEpochSecond());
payload.put("sub", SUBJECT);
payload.put("iss", JWT_ISS);