-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathReferenceCache.java
More file actions
373 lines (335 loc) · 14.5 KB
/
ReferenceCache.java
File metadata and controls
373 lines (335 loc) · 14.5 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/** Copyright 2018 Nick nickl- Lombard
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */
package bsh.util;
import static java.util.Objects.requireNonNull;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadFactory;
/** Asynchronous reference cache with weak, soft and hard reference support.
* Implementations supply values via the abstract create method, which is
* called from a future asynchronously. Garbage collected references are
* monitored and removed from the cache once cleared.
* @param <K> the type of keys maintained by this cache
* @param <V> the type of cached values */
public abstract class ReferenceCache<K,V> {
private final ConcurrentMap<CacheReference<K>,
Future<CacheReference<V>>> cache;
private final ReferenceFactory<K,V> keyFactory;
private final ReferenceFactory<K,V> valueFactory;
private final ReferenceFactory<K,V> lookupFactory;
private final ReferenceQueueMonitor<? super Object> queue;
private static final ExecutorService taskService
= Executors.newCachedThreadPool(new DaemonThreadFactory());
/** Definition of reference types. */
public static enum Type { Weak, Soft, Hard }
/** Instantiates a new cache of key type and value type references.
* @param keyType the type of key reference
* @param valueType the type of value reference */
public ReferenceCache(Type keyType, Type valueType) {
this(keyType, valueType, 0);
}
/** New cache with initial size of key type and value type references.
* @param keyType the type of key reference
* @param valueType the type of value reference
* @param initialSize initial cache size */
public ReferenceCache(Type keyType, Type valueType, int initialSize) {
keyFactory = toFactory(keyType);
valueFactory = toFactory(valueType);
lookupFactory = new HardReferenceFactory();
cache = new ConcurrentHashMap<>(initialSize);
queue = new ReferenceQueueMonitor<>();
Thread t = new Thread(queue);
t.setDaemon(true);
t.start();
}
/** Implementations create a value to associated with the supplied key.
* @param key the key for which a value needs to be created
* @return the value to cache */
abstract protected V create(K key);
/** Get a value from the cache for associated with the supplied key.
* New entries will be initialized if they don't exist or if they were
* cleared and will block to wait for a value to return.
* For asynchronous non blocking value creation use init.
* @param key associated with cache value
* @return value associated with the key */
public V get(K key) {
if (null == key)
return null;
CacheReference<K> refKey = lookupFactory.createKey(key, queue);
if (cache.containsKey(refKey)) {
V value = dereferenceValue(cache.get(refKey));
if (null != value)
return value;
cache.remove(refKey);
}
init(key);
return dereferenceValue(cache.get(refKey));
}
/** Asynchronously initialize a new cache value to associate with key.
* If key is null or key already exist will do nothing.
* Wraps the create method in a future task and starts a new process.
* @param key associated with cache value */
@SuppressWarnings("FutureReturnValueIgnored") // see dereferenceValue
public void init(K key) {
if (null == key)
return;
CacheReference<K> refKey = keyFactory.createKey(key, queue);
if (cache.containsKey(refKey))
return;
FutureTask<CacheReference<V>> task = new FutureTask<>(() -> {
V created = requireNonNull(create(key),
"Reference cache create value may not return null.");
return valueFactory.createValue(created, queue);
});
cache.put(refKey, task);
taskService.execute(task);
}
/** Remove cache entry associated with the given key.
* @param key associated with cache value
* @return true if there was an entry to remove */
public boolean remove(K key) {
if (null == key)
return false;
CacheReference<K> keyRef = lookupFactory.createKey(key, queue);
return CacheKey.class.cast(keyRef).removeCacheEntry();
}
/** Returns the number of cached entries in the cache.
* @return the number of entries cached */
public int size() { return cache.size(); }
/** Clears the cache and removes all of the cached entries.
* The cache will be empty after this call returns. */
public void clear() { cache.clear(); }
/** Create a reference factory of the given type.
* @param type type of reference factory
* @return a reference factory */
private final ReferenceFactory<K,V> toFactory(Type type) {
switch (type) {
case Hard : return new HardReferenceFactory();
case Weak : return new WeakReferenceFactory();
case Soft : return new SoftReferenceFactory();
default : return null;
}
}
/** Dereference a referenced cache value to retrieve the value.
* @param refValue referenced value
* @return dereferenced value */
private V dereferenceValue(CacheReference<V> refValue) {
return refValue.get();
}
/** Retrieve a referenced value from a future and dereference it.
* @param futureValue a future value
* @return dereferenced value */
private V dereferenceValue(Future<CacheReference<V>> futureValue) {
if (null == futureValue) return null;
try {
return dereferenceValue(futureValue.get());
} catch (final Throwable e) {
throw new CompletionException(e.getCause());
}
}
// Member classes
/** A generic type for all types of cache references.
* @param <T> the type of the value */
private interface CacheReference<T> {
/** Method to dereference the referenced value.
* @return the dereferenced value */
T get();
}
/** Defines a reference factory with create key and create value methods. */
private interface ReferenceFactory<K,V> {
/** Create a referenced cache key.
* @param key to reference
* @param queue associated reference queue
* @return cache key type reference */
CacheReference<K> createKey(K key, ReferenceQueue<? super K> queue);
/** Create a referenced cache value.
* @param value to reference
* @param queue associated reference queue
* @return cache value type reference */
CacheReference<V> createValue(V value, ReferenceQueue<? super V> queue);
}
/** A generic type for all types of cache key references.
* Provides equality based on the hash code of the actual key without
* needing to dereference the referenced key. Also implements remove
* key from cache.
* @param <T> the type of the value */
private abstract class CacheKey<T> implements CacheReference<T> {
private final int hashCode;
/** Create a new cache key and capture its hash code.
* @param key to provide equality for */
public CacheKey(T key) {
hashCode = key.hashCode() + key.toString().chars().sum();
}
/** Provides dereference for the key.
* {@inheritDoc} */
abstract public T get();
/** Remove cache entry associated with this cache key.
* @return true if there was an entry to remove */
public boolean removeCacheEntry() {
return null != ReferenceCache.this.cache.remove(this);
}
/** Compares the captured hash code with object hash code for equality.
* {@inheritDoc} */
@Override
public boolean equals(final Object obj) {
return hashCode == obj.hashCode();
}
/** Return the captured hash code for the referenced key.
* {@inheritDoc} */
@Override
public int hashCode() { return hashCode; }
}
/** Hard or strongly referenced key and value factory implementation. */
private class HardReferenceFactory implements ReferenceFactory<K,V> {
/** Provides a cache key for an unreferenced key.
* {@inheritDoc} */
@Override
public CacheReference<K> createKey(
final K key, ReferenceQueue<? super K> queue) {
return new CacheKey<K>(key) {
@Override
public K get() { return key; }
};
}
/** Provides a cache value for an unreferenced value.
* {@inheritDoc} */
@Override
public CacheReference<V> createValue(
final V value, ReferenceQueue<? super V> queue) {
return new CacheReference<V>() {
@Override
public V get() { return value; }
};
}
}
/** Weakly referenced key and value factory implementation. */
private class WeakReferenceFactory implements ReferenceFactory<K,V> {
/** Cache value implementation of a weak reference. */
private class WeakReferenceValue<T> extends WeakReference<T>
implements CacheReference<T> {
WeakReferenceValue(T value, ReferenceQueue<? super T> queue) {
super(value, queue);
}
}
/** Provides a weakly referenced cache key.
* Key removes itself from the cache when cleared.
* {@inheritDoc} */
@Override
public CacheReference<K> createKey(
final K key, final ReferenceQueue<? super K> queue) {
return new CacheKey<K>(key) {
final Reference<K> ref = new WeakReference<K>(key, queue) {
@Override
public void clear() {
removeCacheEntry();
super.clear();
}
};
@Override
public K get() { return ref.get(); }
};
}
/** Provides a weakly referenced cache value.
* {@inheritDoc} */
@Override
public CacheReference<V> createValue(
V value, ReferenceQueue<? super V> queue) {
return new WeakReferenceValue<>(value, queue);
}
}
/** Softly referenced key and value factory implementation. */
private class SoftReferenceFactory implements ReferenceFactory<K,V> {
/** Cache value implementation of a soft reference. */
private class SoftReferenceValue<T> extends SoftReference<T>
implements CacheReference<T> {
SoftReferenceValue(T value, ReferenceQueue<? super T> queue) {
super(value, queue);
}
}
/** Provides a softly referenced cache key.
* Key removes itself from the cache when cleared.
* {@inheritDoc} */
@Override
public CacheReference<K> createKey(
final K key, final ReferenceQueue<? super K> queue) {
return new CacheKey<K>(key) {
final Reference<K> ref = new SoftReference<K>(key, queue) {
@Override
public void clear() {
removeCacheEntry();
super.clear();
}
};
@Override
public K get() { return ref.get(); }
};
}
/** Provides a softly referenced cache value.
* {@inheritDoc} */
@Override
public CacheReference<V> createValue(
V value, ReferenceQueue<? super V> queue) {
return new SoftReferenceValue<>(value, queue);
}
}
/** Runnable monitor of the reference queue associated to all references.
* Registered reference objects are appended to this queue by the garbage
* collector after the appropriate reachability changes were detected.
* Ensures that all references are cleared and removed from the cache.
* @param <T> the type of the reference value */
private class ReferenceQueueMonitor<T> extends ReferenceQueue<T>
implements Runnable {
/** Uses the parent's remove method which is blocking until a cleared
* reference is added to the queue.
* Calls the overwritten clear method of the associated reference which
* will remove the key from the cache.
* {@inheritDoc} */
@Override
public void run() {
for (;;) try {
Reference<? extends T> reference = super.remove();
reference.clear();
} catch (InterruptedException e) { /* ignore try again */ }
}
}
/** For auto shutdown cached threadpool service. A max priority daemon
* thread factory to ensure java vm will shutdown and zombie processes
* cleaned up naturally. @see Executors$DefaultThreadFactory */
private static final class DaemonThreadFactory implements ThreadFactory {
private final ThreadGroup group = Thread.currentThread().getThreadGroup();
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix = "pool-referencecache-futuretask-thread-";
/** {@inheritDoc} */
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(), 0);
if (!t.isDaemon())
t.setDaemon(true);
if (t.getPriority() != Thread.MAX_PRIORITY)
t.setPriority(Thread.MAX_PRIORITY);
return t;
}
}
}