|
@@ -3,8 +3,6 @@ package com.diagbot.config;
|
|
|
import com.esotericsoftware.kryo.Kryo;
|
|
|
import com.esotericsoftware.kryo.io.Input;
|
|
|
import com.esotericsoftware.kryo.io.Output;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
|
import org.springframework.data.redis.serializer.SerializationException;
|
|
|
import org.xerial.snappy.Snappy;
|
|
@@ -12,13 +10,11 @@ import org.xerial.snappy.Snappy;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
|
/**
|
|
|
- * @Description:
|
|
|
+ * @Description: KryoRedis 序列化工具
|
|
|
* @author: gaodm
|
|
|
* @time: 2021/12/16 13:12
|
|
|
*/
|
|
|
public class KryoRedisSerializer<T> implements RedisSerializer<T> {
|
|
|
- Logger logger = LoggerFactory.getLogger(KryoRedisSerializer.class);
|
|
|
-
|
|
|
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
|
|
|
|
|
private static final ThreadLocal<Kryo> kryos = ThreadLocal.withInitial(Kryo::new);
|
|
@@ -47,10 +43,8 @@ public class KryoRedisSerializer<T> implements RedisSerializer<T> {
|
|
|
output.flush();
|
|
|
return Snappy.compress(baos.toByteArray()); //压缩
|
|
|
} catch (Exception e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
+ throw new SerializationException("Could not write Data: " + e.getMessage(), e);
|
|
|
}
|
|
|
-
|
|
|
- return EMPTY_BYTE_ARRAY;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -67,10 +61,8 @@ public class KryoRedisSerializer<T> implements RedisSerializer<T> {
|
|
|
try (Input input = new Input(Snappy.uncompress(bytes))) { //解压
|
|
|
return (T) kryo.readClassAndObject(input);
|
|
|
} catch (Exception e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
+ throw new SerializationException("Could not read Data: " + e.getMessage(), e);
|
|
|
}
|
|
|
-
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
}
|