|
1 | 1 | package dev.voidframework.cache.engine; |
2 | 2 |
|
3 | | -import com.esotericsoftware.kryo.Kryo; |
4 | | -import com.esotericsoftware.kryo.io.ByteBufferOutputStream; |
5 | | -import com.esotericsoftware.kryo.io.Input; |
6 | | -import com.esotericsoftware.kryo.io.Output; |
| 3 | +import dev.voidframework.core.utils.KryoUtils; |
7 | 4 |
|
8 | | -import java.util.ArrayList; |
9 | 5 | import java.util.Arrays; |
10 | | -import java.util.HashMap; |
11 | | -import java.util.HashSet; |
12 | 6 | import java.util.Objects; |
13 | | -import java.util.Optional; |
14 | 7 |
|
15 | 8 | /** |
16 | 9 | * This abstract implementation provides the necessary tools for the |
|
20 | 13 | */ |
21 | 14 | public abstract class AbstractCacheEngine implements CacheEngine { |
22 | 15 |
|
23 | | - private final Kryo kryo; |
24 | | - |
25 | 16 | /** |
26 | 17 | * Build a new instance. |
27 | 18 | * |
28 | 19 | * @since 1.1.0 |
29 | 20 | */ |
30 | 21 | protected AbstractCacheEngine() { |
31 | | - |
32 | | - this.kryo = new Kryo(); |
33 | | - kryo.setRegistrationRequired(false); |
34 | | - kryo.register(ArrayList.class); |
35 | | - kryo.register(Class.class); |
36 | | - kryo.register(HashMap.class); |
37 | | - kryo.register(HashSet.class); |
38 | | - kryo.register(Optional.class); |
39 | 22 | } |
40 | 23 |
|
41 | 24 | /** |
@@ -47,11 +30,10 @@ protected AbstractCacheEngine() { |
47 | 30 | */ |
48 | 31 | public CachedElement wrap(final Object obj) { |
49 | 32 |
|
50 | | - final Output output = new Output(new ByteBufferOutputStream()); |
51 | 33 | final Class<?> classType = obj != null ? obj.getClass() : Object.class; |
| 34 | + final byte[] serializedContent = KryoUtils.serializeWithoutException(obj); |
52 | 35 |
|
53 | | - this.kryo.writeObjectOrNull(output, obj, classType); |
54 | | - return new CachedElement(classType, output.toBytes()); |
| 36 | + return new CachedElement(classType, serializedContent); |
55 | 37 | } |
56 | 38 |
|
57 | 39 | /** |
@@ -67,8 +49,7 @@ public Object unwrap(final CachedElement cachedElement) { |
67 | 49 | return null; |
68 | 50 | } |
69 | 51 |
|
70 | | - final Input input = new Input(cachedElement.content); |
71 | | - return this.kryo.readObjectOrNull(input, cachedElement.classType); |
| 52 | + return KryoUtils.deserializeWithoutException(cachedElement.content, cachedElement.classType); |
72 | 53 | } |
73 | 54 |
|
74 | 55 | /** |
|