spring boot + redis序列化方式

Kryo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

/**
* kryo 序列化
*/
public class KryoSerializer<T> implements RedisSerializer<T> {

private static final int BUFFER_SIZE = 2048;
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
private static final ThreadLocal<Kryo> KRYOS = ThreadLocal.withInitial(Kryo::new);

@Override
public byte[] serialize(T t) throws SerializationException {
if (null == t) {
return EMPTY_BYTE_ARRAY;
}
// -1 代表无限制,实际中依业务修改
Output output = new Output(BUFFER_SIZE, -1);
Kryo kryo = KRYOS.get();
kryo.writeClassAndObject(output, t);
output.flush();
return output.toBytes();
}

@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (null == bytes || bytes.length <= 0) {
return null;
}
Input input = new Input(bytes);
Kryo kryo = KRYOS.get();
T t = (T) kryo.readClassAndObject(input);
input.close();
return t;
}
}

LZ4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4FastDecompressor;

/**
* LZ4 压缩
*/
public class LZ4Serializer implements RedisSerializer<Object> {

private static final int BUFFER_SIZE = 4096;

private RedisSerializer<Object> innerSerializer;
private LZ4FastDecompressor decompresser;
private LZ4Compressor compressor;

public LZ4Serializer(RedisSerializer<Object> innerSerializer) {
this.innerSerializer = innerSerializer;
LZ4Factory factory = LZ4Factory.fastestInstance();
this.compressor = factory.fastCompressor();
this.decompresser = factory.fastDecompressor();
}

@Override
public byte[] serialize(Object graph) throws SerializationException {
if (graph == null) {
return new byte[0];
}
ByteArrayOutputStream byteOutput = null;
LZ4BlockOutputStream compressedOutput = null;
try {
byte[] bytes = innerSerializer.serialize(graph);
byteOutput = new ByteArrayOutputStream();
compressedOutput = new LZ4BlockOutputStream(byteOutput, BUFFER_SIZE, compressor);
compressedOutput.write(bytes);
compressedOutput.finish();
byte[] compressBytes = byteOutput.toByteArray();
return compressBytes;
} catch (Exception e) {
throw new SerializationException("LZ4 Serialization Error", e);
} finally {
IOUtils.closeQuietly(compressedOutput);
IOUtils.closeQuietly(byteOutput);
}
}

@Override
public Object deserialize(byte[] bytes) throws SerializationException {

if (bytes == null || bytes.length == 0) {
return null;
}

ByteArrayOutputStream baos = null;
LZ4BlockInputStream lzis = null;
try {
baos = new ByteArrayOutputStream(BUFFER_SIZE);
lzis = new LZ4BlockInputStream(new ByteArrayInputStream(bytes), decompresser);
int count;
byte[] buffer = new byte[BUFFER_SIZE];
while ((count = lzis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
Object result = innerSerializer.deserialize(baos.toByteArray());
return result;
} catch (Exception e) {
throw new SerializationException("LZ4 deserizelie error", e);
} finally {
IOUtils.closeQuietly(lzis);
IOUtils.closeQuietly(baos);
}
}
}

GZip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class GzipSerializer implements RedisSerializer<Object> {

public static final int BUFFER_SIZE = 4096;
private RedisSerializer<Object> innerSerializer;

public GzipSerializer(RedisSerializer<Object> innerSerializer) {
this.innerSerializer = innerSerializer;
}

@Override
public byte[] serialize(Object graph) throws SerializationException {
if (graph == null) {
return new byte[0];
}
ByteArrayOutputStream bos = null;
GZIPOutputStream gzip = null;
try {
// 序列化
byte[] bytes = innerSerializer.serialize(graph);
bos = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(bos);
// 压缩
gzip.write(bytes);
gzip.finish();
byte[] result = bos.toByteArray();
return result;
} catch (Exception e) {
throw new SerializationException("Gzip Serialization Error", e);
} finally {
IOUtils.closeQuietly(bos);
IOUtils.closeQuietly(gzip);
}
}

@Override
public Object deserialize(byte[] bytes) throws SerializationException {

if (bytes == null || bytes.length == 0) {
return null;
}

ByteArrayOutputStream bos = null;
ByteArrayInputStream bis = null;
GZIPInputStream gzip = null;
try {
bos = new ByteArrayOutputStream();
bis = new ByteArrayInputStream(bytes);
gzip = new GZIPInputStream(bis);
byte[] buff = new byte[BUFFER_SIZE];
int n;
// 解压
while ((n = gzip.read(buff, 0, BUFFER_SIZE)) > 0) {
bos.write(buff, 0, n);
}
// 反序列化
Object result = innerSerializer.deserialize(bos.toByteArray());
return result;
} catch (Exception e) {
throw new SerializationException("Gzip deserizelie error", e);
} finally {
IOUtils.closeQuietly(bos);
IOUtils.closeQuietly(bis);
IOUtils.closeQuietly(gzip);

}
}
}

FST

fastjson

  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/84a732bc.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!