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 package org.apache.hc.client5.http.impl.cache.ehcache;
28
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
34 import org.apache.hc.client5.http.cache.HttpCacheStorageEntry;
35 import org.apache.hc.client5.http.cache.ResourceIOException;
36 import org.apache.hc.client5.http.impl.cache.AbstractSerializingCacheStorage;
37 import org.apache.hc.client5.http.impl.cache.ByteArrayCacheEntrySerializer;
38 import org.apache.hc.client5.http.impl.cache.CacheConfig;
39 import org.apache.hc.client5.http.impl.cache.NoopCacheEntrySerializer;
40 import org.apache.hc.core5.util.Args;
41 import org.ehcache.Cache;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public class EhcacheHttpCacheStorage<T> extends AbstractSerializingCacheStorage<T, T> {
61
62
63
64
65
66
67 public static EhcacheHttpCacheStorage<HttpCacheStorageEntry> createObjectCache(
68 final Cache<String, HttpCacheStorageEntry> cache, final CacheConfig config) {
69 return new EhcacheHttpCacheStorage<>(cache, config, NoopCacheEntrySerializer.INSTANCE);
70 }
71
72
73
74
75
76
77 public static EhcacheHttpCacheStorage<byte[]> createSerializedCache(
78 final Cache<String, byte[]> cache, final CacheConfig config) {
79 return new EhcacheHttpCacheStorage<>(cache, config, ByteArrayCacheEntrySerializer.INSTANCE);
80 }
81
82 private final Cache<String, T> cache;
83
84
85
86
87
88
89
90
91
92
93
94 public EhcacheHttpCacheStorage(
95 final Cache<String, T> cache,
96 final CacheConfig config,
97 final HttpCacheEntrySerializer<T> serializer) {
98 super((config != null ? config : CacheConfig.DEFAULT).getMaxUpdateRetries(), serializer);
99 this.cache = Args.notNull(cache, "Ehcache");
100 }
101
102 @Override
103 protected String digestToStorageKey(final String key) {
104 return key;
105 }
106
107 @Override
108 protected void store(final String storageKey, final T storageObject) throws ResourceIOException {
109 cache.put(storageKey, storageObject);
110 }
111
112 @Override
113 protected T restore(final String storageKey) throws ResourceIOException {
114 return cache.get(storageKey);
115 }
116
117 @Override
118 protected T getForUpdateCAS(final String storageKey) throws ResourceIOException {
119 return cache.get(storageKey);
120 }
121
122 @Override
123 protected T getStorageObject(final T element) throws ResourceIOException {
124 return element;
125 }
126
127 @Override
128 protected boolean updateCAS(
129 final String storageKey, final T oldStorageObject, final T storageObject) throws ResourceIOException {
130 return cache.replace(storageKey, oldStorageObject, storageObject);
131 }
132
133 @Override
134 protected void delete(final String storageKey) throws ResourceIOException {
135 cache.remove(storageKey);
136 }
137
138 @Override
139 protected Map<String, T> bulkRestore(final Collection<String> storageKeys) throws ResourceIOException {
140 final Map<String, T> resultMap = new HashMap<>();
141 for (final String storageKey: storageKeys) {
142 final T storageObject = cache.get(storageKey);
143 if (storageObject != null) {
144 resultMap.put(storageKey, storageObject);
145 }
146 }
147 return resultMap;
148 }
149
150 }