mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-12-26 14:15:41 +08:00
修复jFinal,jboot插件Redis缓存方法与通用方法不一致的问题
增加缓存序列化方法
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
<dependency>
|
||||
<groupId>com.jfinal</groupId>
|
||||
<artifactId>jfinal-undertow</artifactId>
|
||||
<version>2.7</version>
|
||||
<version>2.8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jfinal</groupId>
|
||||
@@ -54,7 +54,6 @@
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package cn.dev33.satoken.jfinal;
|
||||
|
||||
import com.jfinal.kit.LogKit;
|
||||
import com.jfinal.plugin.redis.serializer.ISerializer;
|
||||
import com.jfinal.plugin.redis.serializer.JdkSerializer;
|
||||
import redis.clients.jedis.util.SafeEncoder;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class SaJdkSerializer implements ISerializer {
|
||||
|
||||
public static final ISerializer me = new JdkSerializer();
|
||||
|
||||
public byte[] keyToBytes(String key) {
|
||||
return SafeEncoder.encode(key);
|
||||
}
|
||||
|
||||
public String keyFromBytes(byte[] bytes) {
|
||||
return SafeEncoder.encode(bytes);
|
||||
}
|
||||
|
||||
public byte[] fieldToBytes(Object field) {
|
||||
return valueToBytes(field);
|
||||
}
|
||||
|
||||
public Object fieldFromBytes(byte[] bytes) {
|
||||
return valueFromBytes(bytes);
|
||||
}
|
||||
|
||||
public byte[] valueToBytes(Object value) {
|
||||
ObjectOutputStream objectOut = null;
|
||||
try {
|
||||
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(1024);
|
||||
objectOut = new ObjectOutputStream(bytesOut);
|
||||
objectOut.writeObject(value);
|
||||
objectOut.flush();
|
||||
return bytesOut.toByteArray();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
finally {
|
||||
if(objectOut != null)
|
||||
try {objectOut.close();} catch (Exception e) {
|
||||
LogKit.error(e.getMessage(), e);}
|
||||
}
|
||||
}
|
||||
|
||||
public Object valueFromBytes(byte[] bytes) {
|
||||
if(bytes == null || bytes.length == 0)
|
||||
return null;
|
||||
|
||||
ObjectInputStream objectInput = null;
|
||||
try {
|
||||
ByteArrayInputStream bytesInput = new ByteArrayInputStream(bytes);
|
||||
objectInput = new ObjectInputStream(bytesInput);
|
||||
return objectInput.readObject();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
finally {
|
||||
if (objectInput != null)
|
||||
try {objectInput.close();} catch (Exception e) {LogKit.error(e.getMessage(), e);}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return redis.get(key);
|
||||
return redis.getJedis().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,9 +44,9 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
return;
|
||||
}
|
||||
if(timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
redis.set(key, value);
|
||||
redis.getJedis().set(key, value);
|
||||
}else{
|
||||
redis.setex(key,Integer.parseInt(timeout+""),value);
|
||||
redis.getJedis().setex(key,timeout,value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
*/
|
||||
@Override
|
||||
public void delete(String key) {
|
||||
redis.del(key);
|
||||
redis.getJedis().del(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +81,7 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
*/
|
||||
@Override
|
||||
public long getTimeout(String key) {
|
||||
return redis.ttl(key);
|
||||
return redis.getJedis().ttl(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +102,7 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
}
|
||||
return;
|
||||
}
|
||||
redis.expire(key,Integer.parseInt(timeout+""));
|
||||
redis.getJedis().expire(key,timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user