刚过完六一,出来皮一下 记录下工作的问题和解决方法
# msgpack了解一下
importJava.io.IOException;
import org.msgpack.MessagePack;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
/**
* redis msgpack 序列化
* 可有效减小内存占用
* @author Administrator
*
* @param <T>
*/
public class MsgPackRedisSerializer<T> implements RedisSerializer<T> {
private MessagePack msgPack;
public MsgPackRedisSerializer(MessagePack msgPack) {
super();
this.msgPack = msgPack;
}
public MsgPackRedisSerializer() {
super();
}
@Override
public byte[] serialize(T t) throws SerializationException {
if(t==null) {
return new byte[0];
}
msgPack = new MessagePack();
try {
return msgPack.write(t);
} catch (IOException e) {
e.printStackTrace();
return new byte[0];
}
}
@SuppressWarnings("unchecked")
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if(bytes==null) {
return null;
}
msgPack = new MessagePack();
try {
T t = (T)msgPack.read(bytes,ObjectTemplate.getInstance());
return t;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
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
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