update 移除 satoken 自带集成 redis 改为使用 框架自身 Redission 实现 统一方案 性能优异

This commit is contained in:
疯狂的狮子li
2021-11-26 13:42:20 +08:00
parent 2fe8291f1d
commit e2d370bd9d
4 changed files with 213 additions and 12 deletions

View File

@@ -34,11 +34,6 @@
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
</dependency>
<!-- Sa-Token 整合 Redis 使用jackson序列化方式 -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId>
</dependency>
<!-- Sa-Token整合SpringAOP实现注解鉴权 -->
<dependency>
<groupId>cn.dev33</groupId>

View File

@@ -91,6 +91,29 @@ public class RedisUtils {
client.getBucket(key).set(value);
}
/**
* 缓存基本的对象,保留当前对象 TTL 有效期
*
* @param key 缓存的键值
* @param value 缓存的值
* @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90)
* @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案
*/
public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) {
RBucket<Object> bucket = client.getBucket(key);
if (isSaveTtl) {
try {
bucket.setAndKeepTTL(value);
} catch (Exception e) {
long timeToLive = bucket.remainTimeToLive();
bucket.set(value);
bucket.expire(timeToLive, TimeUnit.MILLISECONDS);
}
} else {
bucket.set(value);
}
}
/**
* 缓存基本的对象Integer、String、实体类等
*
@@ -99,7 +122,7 @@ public class RedisUtils {
* @param timeout 时间
* @param timeUnit 时间颗粒度
*/
public static <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
public static <T> void setCacheObject(final String key, final T value, final long timeout, final TimeUnit timeUnit) {
RBucket<T> result = client.getBucket(key);
result.set(value);
result.expire(timeout, timeUnit);
@@ -140,6 +163,17 @@ public class RedisUtils {
return rBucket.get();
}
/**
* 获得key剩余存活时间
*
* @param key 缓存键值
* @return 剩余存活时间
*/
public static <T> long getTimeToLive(final String key) {
RBucket<T> rBucket = client.getBucket(key);
return rBucket.remainTimeToLive();
}
/**
* 删除单个对象
*