Skip to content

Commit 9739d13

Browse files
author
Dan Blaisdell
committed
support for custom serializer
1 parent 99ea324 commit 9739d13

File tree

4 files changed

+81
-17
lines changed

4 files changed

+81
-17
lines changed

src/main/java/com/arhs/spring/cache/mongo/MongoCache.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
package com.arhs.spring.cache.mongo;
2525

2626
import com.arhs.spring.cache.mongo.domain.CacheDocument;
27+
import com.arhs.spring.cache.mongo.serializer.JavaSerializer;
28+
import com.arhs.spring.cache.mongo.serializer.Serializer;
2729
import com.mongodb.DBCollection;
2830
import com.mongodb.DBObject;
29-
import org.mockito.internal.matchers.Null;
3031
import org.slf4j.Logger;
3132
import org.slf4j.LoggerFactory;
3233
import org.springframework.cache.Cache;
@@ -68,6 +69,7 @@ public class MongoCache implements Cache {
6869
private final long ttl;
6970

7071
private final Object lock = new Object();
72+
private Serializer serializer;
7173

7274
/**
7375
* Constructor.
@@ -89,7 +91,20 @@ public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTe
8991
* @param ttl a time-to-live (in seconds).
9092
*/
9193
public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTemplate, long ttl) {
92-
this(cacheName, collectionName, mongoTemplate, ttl, false);
94+
this(cacheName, collectionName, mongoTemplate, ttl, false, new JavaSerializer());
95+
}
96+
97+
/**
98+
* Constructor.
99+
*
100+
* @param cacheName a cache name.
101+
* @param collectionName a collection name.
102+
* @param mongoTemplate a {@link MongoTemplate} instance.
103+
* @param ttl a time-to-live (in seconds).
104+
* @param serializer a serializer.
105+
*/
106+
public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTemplate, long ttl, Serializer serializer) {
107+
this(cacheName, collectionName, mongoTemplate, ttl, false, serializer);
93108
}
94109

95110
/**
@@ -102,6 +117,20 @@ public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTe
102117
* @param flushOnBoot a value that indicates if the collection must be always flush.
103118
*/
104119
public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTemplate, long ttl, boolean flushOnBoot) {
120+
this(cacheName, collectionName, mongoTemplate, ttl, flushOnBoot, new JavaSerializer());
121+
}
122+
123+
/**
124+
* Constructor.
125+
*
126+
* @param cacheName a cache name.
127+
* @param collectionName a collection name.
128+
* @param mongoTemplate a {@link MongoTemplate} instance.
129+
* @param ttl a time-to-live (in seconds).
130+
* @param flushOnBoot a value that indicates if the collection must be always flush.
131+
* @param serializer a serializer.
132+
*/
133+
public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTemplate, long ttl, boolean flushOnBoot, Serializer serializer) {
105134
Assert.hasText(cacheName, "'cacheName' must be not null and not empty.");
106135
Assert.notNull(collectionName, "'collectionName' must be not null.");
107136
Assert.notNull(collectionName, "'mongoTemplate' must be not null.");
@@ -111,6 +140,7 @@ public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTe
111140
this.mongoTemplate = mongoTemplate;
112141
this.cacheName = cacheName;
113142
this.ttl = ttl;
143+
this.serializer = serializer == null ? new JavaSerializer() : serializer;
114144

115145
initialize();
116146
}
@@ -269,10 +299,7 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
269299
private Object deserialize(String value) throws IOException, ClassNotFoundException {
270300
final Base64.Decoder decoder = Base64.getDecoder();
271301
final byte[] data = decoder.decode(value);
272-
try (final ByteArrayInputStream buffer = new ByteArrayInputStream(data);
273-
final ObjectInputStream output = new ObjectInputStream(buffer)) {
274-
return output.readObject();
275-
}
302+
return serializer.deserialize(data);
276303
}
277304

278305
private Object getFromCache(Object key) {
@@ -307,16 +334,8 @@ private void initialize() {
307334
}
308335

309336
private String serialize(Object value) throws IOException {
310-
try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
311-
final ObjectOutputStream output = new ObjectOutputStream(buffer)) {
312-
313-
output.writeObject(value);
314-
315-
final byte[] data = buffer.toByteArray();
316-
317-
final Base64.Encoder encoder = Base64.getEncoder();
318-
return encoder.encodeToString(data);
319-
}
337+
final Base64.Encoder encoder = Base64.getEncoder();
338+
return encoder.encodeToString(serializer.serialize(value));
320339
}
321340

322341
private Index createExpireIndex() {

src/main/java/com/arhs/spring/cache/mongo/MongoCacheBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.arhs.spring.cache.mongo;
2525

26+
import com.arhs.spring.cache.mongo.serializer.Serializer;
2627
import org.springframework.data.mongodb.core.MongoTemplate;
2728
import org.springframework.util.Assert;
2829

@@ -42,6 +43,7 @@ public class MongoCacheBuilder {
4243
private String collectionName;
4344
private MongoTemplate mongoTemplate;
4445
private long ttl;
46+
private Serializer serializer;
4547

4648
/**
4749
* Constructor.
@@ -79,7 +81,7 @@ public static MongoCacheBuilder newInstance(String collectionName, MongoTemplate
7981
* @return a {@link MongoCache} instance.
8082
*/
8183
public MongoCache build() {
82-
return new MongoCache(cacheName, collectionName, mongoTemplate, ttl, flushOnBoot);
84+
return new MongoCache(cacheName, collectionName, mongoTemplate, ttl, flushOnBoot, serializer);
8385
}
8486

8587
/**
@@ -104,4 +106,9 @@ public MongoCacheBuilder withTTL(long ttl) {
104106
return this;
105107
}
106108

109+
public MongoCacheBuilder withSerializer(Serializer serializer) {
110+
this.serializer = serializer;
111+
return this;
112+
}
113+
107114
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.arhs.spring.cache.mongo.serializer;
2+
3+
import java.io.*;
4+
5+
public class JavaSerializer implements Serializer {
6+
@Override
7+
public byte[] serialize(Object obj) throws IOException {
8+
try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
9+
final ObjectOutputStream output = new ObjectOutputStream(buffer)) {
10+
11+
output.writeObject(obj);
12+
13+
return buffer.toByteArray();
14+
}
15+
}
16+
17+
@Override
18+
public Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
19+
try (final ByteArrayInputStream buffer = new ByteArrayInputStream(bytes);
20+
final ObjectInputStream output = new ObjectInputStream(buffer)) {
21+
return output.readObject();
22+
}
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.arhs.spring.cache.mongo.serializer;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* Created by dan on 3/1/17.
7+
*/
8+
public interface Serializer {
9+
10+
public byte[] serialize(Object obj) throws IOException;
11+
12+
public Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException;
13+
14+
}

0 commit comments

Comments
 (0)