mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 17:37:53 +08:00
perf: sa-token-redisx 调整 SaTokenDaoOfRedisJson 类保持与 SaTokenDaoForRedisTemplate 相似的处理逻辑
This commit is contained in:
parent
caeb4eba15
commit
905f6714e2
@ -28,12 +28,6 @@
|
||||
<artifactId>redisx</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.noear</groupId>
|
||||
<artifactId>snack3</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.noear</groupId>
|
||||
<artifactId>solon-test</artifactId>
|
||||
|
@ -1,89 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2099 sa-token.cc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import org.noear.snack.ONode;
|
||||
|
||||
/**
|
||||
* Snack3 定制版 SaSession,重写类型转换API
|
||||
*
|
||||
* @author noear
|
||||
* @since 1.34.0
|
||||
*/
|
||||
@Deprecated
|
||||
public class SaSessionForJson extends SaSession {
|
||||
|
||||
private static final long serialVersionUID = -7600983549653130681L;
|
||||
|
||||
public SaSessionForJson() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建一个 SaSession 对象
|
||||
* @param id Session 的 id
|
||||
*/
|
||||
public SaSessionForJson(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取值 (指定转换类型)
|
||||
* @param <T> 泛型
|
||||
* @param key key
|
||||
* @param cs 指定转换类型
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
public <T> T getModel(String key, Class<T> cs) {
|
||||
if(SaFoxUtil.isBasicType(cs)) {
|
||||
return SaFoxUtil.getValueByType(get(key), cs);
|
||||
}
|
||||
return ONode.deserialize(getString(key), cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取值 (指定转换类型, 并指定值为Null时返回的默认值)
|
||||
* @param <T> 泛型
|
||||
* @param key key
|
||||
* @param cs 指定转换类型
|
||||
* @param defaultValue 值为Null时返回的默认值
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getModel(String key, Class<T> cs, Object defaultValue) {
|
||||
Object value = get(key);
|
||||
if(valueIsNull(value)) {
|
||||
return (T)defaultValue;
|
||||
}
|
||||
if(SaFoxUtil.isBasicType(cs)) {
|
||||
return SaFoxUtil.getValueByType(get(key), cs);
|
||||
}
|
||||
return ONode.deserialize(getString(key), cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略 timeout 字段的序列化
|
||||
*/
|
||||
@Override
|
||||
public long timeout() {
|
||||
return super.timeout();
|
||||
}
|
||||
|
||||
}
|
@ -15,11 +15,10 @@
|
||||
*/
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import cn.dev33.satoken.dao.auto.SaTokenDaoBySessionFollowObject;
|
||||
import cn.dev33.satoken.dao.auto.SaTokenDaoByObjectFollowString;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import org.noear.redisx.RedisClient;
|
||||
import org.noear.redisx.plus.RedisBucket;
|
||||
import org.noear.snack.ONode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -31,20 +30,17 @@ import java.util.Set;
|
||||
*
|
||||
* @author noear
|
||||
* @since 1.34.0
|
||||
* @since 1.41.0
|
||||
*/
|
||||
public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject {
|
||||
public class SaTokenDaoForRedisx implements SaTokenDaoByObjectFollowString {
|
||||
private final RedisBucket redisBucket;
|
||||
|
||||
public SaTokenDaoOfRedisJson(Properties props) {
|
||||
public SaTokenDaoForRedisx(Properties props) {
|
||||
this(new RedisClient(props));
|
||||
}
|
||||
|
||||
public SaTokenDaoOfRedisJson(RedisClient redisClient) {
|
||||
public SaTokenDaoForRedisx(RedisClient redisClient) {
|
||||
redisBucket = redisClient.getBucket();
|
||||
|
||||
// 重写 SaSession 生成策略
|
||||
//SaStrategy.instance.createSession = (sessionId) -> new SaSessionForJson(sessionId);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -60,7 +56,14 @@ public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject {
|
||||
*/
|
||||
@Override
|
||||
public void set(String key, String value, long timeout) {
|
||||
if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 判断是否为永不过期
|
||||
if (timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
redisBucket.store(key, value);
|
||||
} else {
|
||||
redisBucket.store(key, value, (int) timeout);
|
||||
}
|
||||
}
|
||||
@ -71,6 +74,11 @@ public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject {
|
||||
@Override
|
||||
public void update(String key, String value) {
|
||||
long expire = getTimeout(key);
|
||||
// -2 = 无此键
|
||||
if (expire == SaTokenDao.NOT_VALUE_EXPIRE) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.set(key, value, expire);
|
||||
}
|
||||
|
||||
@ -95,74 +103,22 @@ public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject {
|
||||
*/
|
||||
@Override
|
||||
public void updateTimeout(String key, long timeout) {
|
||||
if (redisBucket.exists(key)) {
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
// 判断是否想要设置为永久
|
||||
if (timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
long expire = getTimeout(key);
|
||||
if (expire == SaTokenDao.NEVER_EXPIRE) {
|
||||
// 如果其已经被设置为永久,则不作任何处理
|
||||
} else {
|
||||
// 如果尚未被设置为永久,那么再次set一次
|
||||
this.set(key, this.get(key), timeout);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Object,如无返空
|
||||
*/
|
||||
@Override
|
||||
public Object getObject(String key) {
|
||||
String value = get(key);
|
||||
return ONode.deserialize(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getObject(String key, Class<T> classType) {
|
||||
String value = get(key);
|
||||
return ONode.deserialize(value, classType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Object,并设定存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void setObject(String key, Object object, long timeout) {
|
||||
if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
String value = ONode.serialize(object);
|
||||
set(key, value, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Object (过期时间不变)
|
||||
*/
|
||||
@Override
|
||||
public void updateObject(String key, Object object) {
|
||||
long expire = getObjectTimeout(key);
|
||||
this.setObject(key, object, expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Object
|
||||
*/
|
||||
@Override
|
||||
public void deleteObject(String key) {
|
||||
redisBucket.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Object的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public long getObjectTimeout(String key) {
|
||||
return redisBucket.ttl(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改Object的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void updateObjectTimeout(String key, long timeout) {
|
||||
if (redisBucket.exists(key)) {
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 搜索数据
|
||||
*/
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2099 sa-token.cc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import org.noear.redisx.RedisClient;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* SaTokenDao 的 redis 适配
|
||||
*
|
||||
* @author noear
|
||||
* @since 1.34.0
|
||||
*/
|
||||
public class SaTokenDaoOfRedis extends SaTokenDaoOfRedisBase64 {
|
||||
|
||||
public SaTokenDaoOfRedis(Properties props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
public SaTokenDaoOfRedis(RedisClient redisClient) {
|
||||
super(redisClient);
|
||||
}
|
||||
}
|
@ -1,168 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020-2099 sa-token.cc
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import cn.dev33.satoken.dao.auto.SaTokenDaoBySessionFollowObject;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import org.noear.redisx.RedisClient;
|
||||
import org.noear.redisx.plus.RedisBucket;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* SaTokenDao 的 redis 适配(可以完全精准还原所有序列化类型)
|
||||
*
|
||||
* @author noear
|
||||
* @since 1.34.0
|
||||
*/
|
||||
public class SaTokenDaoOfRedisBase64 implements SaTokenDaoBySessionFollowObject {
|
||||
private final RedisBucket redisBucket;
|
||||
|
||||
public SaTokenDaoOfRedisBase64(Properties props) {
|
||||
this(new RedisClient(props));
|
||||
}
|
||||
|
||||
public SaTokenDaoOfRedisBase64(RedisClient redisClient) {
|
||||
redisBucket = redisClient.getBucket();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Value,如无返空
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return redisBucket.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Value,并设定存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void set(String key, String value, long timeout) {
|
||||
if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
redisBucket.store(key, value, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改指定key-value键值对 (过期时间不变)
|
||||
*/
|
||||
@Override
|
||||
public void update(String key, String value) {
|
||||
long expire = getTimeout(key);
|
||||
this.set(key, value, expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Value
|
||||
*/
|
||||
@Override
|
||||
public void delete(String key) {
|
||||
redisBucket.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Value的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public long getTimeout(String key) {
|
||||
return redisBucket.ttl(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改Value的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void updateTimeout(String key, long timeout) {
|
||||
if (redisBucket.exists(key)) {
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Object,如无返空
|
||||
*/
|
||||
@Override
|
||||
public Object getObject(String key) {
|
||||
return redisBucket.getAndDeserialize(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getObject(String key, Class<T> classType) {
|
||||
return redisBucket.getAndDeserialize(key, classType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Object,并设定存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void setObject(String key, Object object, long timeout) {
|
||||
if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
redisBucket.storeAndSerialize(key, object, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Object (过期时间不变)
|
||||
*/
|
||||
@Override
|
||||
public void updateObject(String key, Object object) {
|
||||
long expire = getObjectTimeout(key);
|
||||
this.setObject(key, object, expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Object
|
||||
*/
|
||||
@Override
|
||||
public void deleteObject(String key) {
|
||||
redisBucket.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Object的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public long getObjectTimeout(String key) {
|
||||
return redisBucket.ttl(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改Object的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void updateObjectTimeout(String key, long timeout) {
|
||||
if (redisBucket.exists(key)) {
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 搜索数据
|
||||
*/
|
||||
@Override
|
||||
public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
|
||||
Set<String> keys = redisBucket.keys(prefix + "*" + keyword + "*");
|
||||
List<String> list = new ArrayList<String>(keys);
|
||||
return SaFoxUtil.searchList(list, start, size, sortType);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user