24
24
package com .arhs .spring .cache .mongo ;
25
25
26
26
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 ;
27
29
import com .mongodb .DBCollection ;
28
30
import com .mongodb .DBObject ;
29
- import org .mockito .internal .matchers .Null ;
30
31
import org .slf4j .Logger ;
31
32
import org .slf4j .LoggerFactory ;
32
33
import org .springframework .cache .Cache ;
@@ -68,6 +69,7 @@ public class MongoCache implements Cache {
68
69
private final long ttl ;
69
70
70
71
private final Object lock = new Object ();
72
+ private Serializer serializer ;
71
73
72
74
/**
73
75
* Constructor.
@@ -89,7 +91,20 @@ public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTe
89
91
* @param ttl a time-to-live (in seconds).
90
92
*/
91
93
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 );
93
108
}
94
109
95
110
/**
@@ -102,6 +117,20 @@ public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTe
102
117
* @param flushOnBoot a value that indicates if the collection must be always flush.
103
118
*/
104
119
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 ) {
105
134
Assert .hasText (cacheName , "'cacheName' must be not null and not empty." );
106
135
Assert .notNull (collectionName , "'collectionName' must be not null." );
107
136
Assert .notNull (collectionName , "'mongoTemplate' must be not null." );
@@ -111,6 +140,7 @@ public MongoCache(String cacheName, String collectionName, MongoTemplate mongoTe
111
140
this .mongoTemplate = mongoTemplate ;
112
141
this .cacheName = cacheName ;
113
142
this .ttl = ttl ;
143
+ this .serializer = serializer == null ? new JavaSerializer () : serializer ;
114
144
115
145
initialize ();
116
146
}
@@ -269,10 +299,7 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
269
299
private Object deserialize (String value ) throws IOException , ClassNotFoundException {
270
300
final Base64 .Decoder decoder = Base64 .getDecoder ();
271
301
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 );
276
303
}
277
304
278
305
private Object getFromCache (Object key ) {
@@ -307,16 +334,8 @@ private void initialize() {
307
334
}
308
335
309
336
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 ));
320
339
}
321
340
322
341
private Index createExpireIndex () {
0 commit comments