Skip to content

Commit 039f53c

Browse files
committed
make GuavaCachedResponseImpl simple
1 parent ef7f643 commit 039f53c

File tree

1 file changed

+4
-31
lines changed

1 file changed

+4
-31
lines changed
Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.wego.httpcache.dao.impl.guava;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.DeserializationFeature;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.fasterxml.jackson.databind.SerializationFeature;
73
import com.google.common.cache.Cache;
84
import com.google.common.cache.CacheBuilder;
95
import com.wego.httpcache.dao.CachedResponseDao;
@@ -13,43 +9,20 @@
139

1410
public class GuavaCachedResponseImpl implements CachedResponseDao {
1511
private static final long CACHE_TTL = 30;
16-
private static final Cache<String, String> CACHE =
12+
private static final Cache<String, CachedResponseEntity> CACHE =
1713
CacheBuilder.newBuilder().expireAfterWrite(CACHE_TTL, TimeUnit.MINUTES).build();
1814

19-
private static final ObjectMapper OBJECT_MAPPER =
20-
new ObjectMapper()
21-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
22-
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
23-
2415
@Override
2516
public CachedResponseEntity save(CachedResponseEntity cachedResponseEntity, long ttl) {
26-
String jsonEntity = null;
27-
try {
28-
jsonEntity = OBJECT_MAPPER.writeValueAsString(cachedResponseEntity);
29-
} catch (JsonProcessingException ex) {
30-
ex.printStackTrace();
31-
}
32-
33-
if (jsonEntity != null) {
34-
CACHE.put(cachedResponseEntity.getId(), jsonEntity);
17+
if (cachedResponseEntity != null) {
18+
CACHE.put(cachedResponseEntity.getId(), cachedResponseEntity);
3519
}
3620

3721
return cachedResponseEntity;
3822
}
3923

4024
@Override
4125
public Optional<CachedResponseEntity> findById(String id) {
42-
CachedResponseEntity cachedResponseEntity = null;
43-
String jsonEntity = CACHE.getIfPresent(id);
44-
45-
if (jsonEntity != null) {
46-
try {
47-
cachedResponseEntity = OBJECT_MAPPER.readValue(jsonEntity, CachedResponseEntity.class);
48-
} catch (Exception ex) {
49-
ex.printStackTrace();
50-
}
51-
}
52-
53-
return Optional.ofNullable(cachedResponseEntity);
26+
return Optional.ofNullable(CACHE.getIfPresent(id));
5427
}
5528
}

0 commit comments

Comments
 (0)