|
@@ -0,0 +1,77 @@
|
|
|
+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;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description:
|
|
|
+ * @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);
|
|
|
+
|
|
|
+ private final Class<T> clazz;
|
|
|
+
|
|
|
+ public KryoRedisSerializer(Class<T> clazz) {
|
|
|
+ super();
|
|
|
+ this.clazz = clazz;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte[] serialize(T t) throws SerializationException {
|
|
|
+ if (t == null) {
|
|
|
+ return EMPTY_BYTE_ARRAY;
|
|
|
+ }
|
|
|
+
|
|
|
+ Kryo kryo = kryos.get();
|
|
|
+ kryo.setRegistrationRequired(false);
|
|
|
+ kryo.setReferences(false);
|
|
|
+ kryo.register(clazz);
|
|
|
+
|
|
|
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ Output output = new Output(baos)) {
|
|
|
+ kryo.writeClassAndObject(output, t);
|
|
|
+ output.flush();
|
|
|
+ return Snappy.compress(baos.toByteArray()); //压缩
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return EMPTY_BYTE_ARRAY;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public T deserialize(byte[] bytes) throws SerializationException {
|
|
|
+ if (bytes == null || bytes.length <= 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Kryo kryo = kryos.get();
|
|
|
+ kryo.setRegistrationRequired(false);
|
|
|
+ kryo.setReferences(false);
|
|
|
+ kryo.register(clazz);
|
|
|
+
|
|
|
+ try (Input input = new Input(Snappy.uncompress(bytes))) { //解压
|
|
|
+ return (T) kryo.readClassAndObject(input);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|