|
26 | 26 |
|
27 | 27 | import java.lang.ref.Reference;
|
28 | 28 | import java.lang.ref.ReferenceQueue;
|
29 |
| -import java.lang.ref.WeakReference; |
30 | 29 | import java.util.concurrent.ConcurrentHashMap;
|
31 | 30 | import java.util.concurrent.ConcurrentMap;
|
32 | 31 | import java.util.function.Function;
|
33 | 32 |
|
34 | 33 | public class Cache<K, V> {
|
35 |
| - private final ConcurrentMap<Reference<K>, Object> producerMutexes = new ConcurrentHashMap<>(); |
36 |
| - private final ConcurrentMap<Reference<K>, V> valueMap = new ConcurrentHashMap<>(); |
37 |
| - private final ReferenceQueue<K> queue = new ReferenceQueue<K>(); |
38 |
| - |
| 34 | + private final KeyedLocks<K> producerMutexes = new KeyedLocks<>(); |
| 35 | + private final ConcurrentMap<Object, Object> valueMap = new ConcurrentHashMap<>(); |
| 36 | + |
| 37 | + private final ReferenceType keyRefType; |
| 38 | + private final ReferenceType valueRefType; |
| 39 | + private final ReferenceQueue<K> queue; |
| 40 | + |
| 41 | + public Cache() { |
| 42 | + this(ReferenceType.WEAK, ReferenceType.SOFT); |
| 43 | + } |
| 44 | + |
| 45 | + public Cache(ReferenceType keyRefType, ReferenceType valueRefType) { |
| 46 | + this.keyRefType = keyRefType; |
| 47 | + this.valueRefType = valueRefType; |
| 48 | + this.queue = keyRefType.createKeyReferenceQueue(); |
| 49 | + } |
| 50 | + |
39 | 51 | public V get(K key, Function<? super K, ? extends V> producer) {
|
40 | 52 | expungeStaleEntries();
|
41 | 53 |
|
42 |
| - Reference<K> lookupKeyRef = new KeyReference<K>(key); |
43 |
| - V value; |
| 54 | + Object lookupKeyRef = keyRefType.createLookupKey(key); |
| 55 | + Object valueRef; |
44 | 56 |
|
45 | 57 | // Try to get a cached value.
|
46 |
| - value = valueMap.get(lookupKeyRef); |
47 |
| - |
48 |
| - if (value != null) { |
49 |
| - // A cached value was found. |
50 |
| - return value; |
| 58 | + valueRef = valueMap.get(lookupKeyRef); |
| 59 | + V value; |
| 60 | + |
| 61 | + if (valueRef != null) { |
| 62 | + value = valueRefType.dereference(valueRef); |
| 63 | + if (value != null) { |
| 64 | + // A cached value was found. |
| 65 | + return value; |
| 66 | + } |
51 | 67 | }
|
52 | 68 |
|
53 |
| - Object mutex = getOrCreateMutex(lookupKeyRef); |
54 |
| - synchronized (mutex) { |
55 |
| - try { |
56 |
| - // Double-check after getting mutex |
57 |
| - value = valueMap.get(lookupKeyRef); |
58 |
| - if (value == null) { |
59 |
| - value = producer.apply(key); |
60 |
| - final Reference<K> actualKeyRef = new KeyReference<K>(key, queue); |
61 |
| - valueMap.put(actualKeyRef, value); |
62 |
| - } |
63 |
| - } finally { |
64 |
| - producerMutexes.remove(lookupKeyRef, mutex); |
| 69 | + try (KeyedLocks.Lock lock = producerMutexes.acquire(key)) { |
| 70 | + // Double-check after getting mutex |
| 71 | + valueRef = valueMap.get(lookupKeyRef); |
| 72 | + value = valueRef == null ? null : valueRefType.dereference(valueRef); |
| 73 | + if (value == null) { |
| 74 | + value = producer.apply(key); |
| 75 | + valueMap.put( |
| 76 | + keyRefType.createKeyReference(key, queue), |
| 77 | + valueRefType.createValueReference(value) |
| 78 | + ); |
65 | 79 | }
|
| 80 | + } catch (InterruptedException ex) { |
| 81 | + throw new RuntimeException(ex); |
66 | 82 | }
|
67 |
| - |
68 | 83 | return value;
|
69 | 84 | }
|
70 | 85 |
|
| 86 | + |
71 | 87 | public V remove(K key) {
|
72 |
| - Reference<K> lookupKeyRef = new KeyReference<K>(key); |
73 |
| - Object mutex = getOrCreateMutex(lookupKeyRef); |
74 |
| - synchronized (mutex) { |
75 |
| - try { |
76 |
| - final V value = valueMap.remove(lookupKeyRef); |
77 |
| - return value; |
78 |
| - } finally { |
79 |
| - producerMutexes.remove(lookupKeyRef, mutex); |
80 |
| - } |
| 88 | + try (KeyedLocks.Lock lock = producerMutexes.acquire(key)) { |
| 89 | + Object valueRef = valueMap.remove(keyRefType.createLookupKey(key)); |
| 90 | + return valueRef == null ? null : valueRefType.dereference(valueRef); |
| 91 | + } catch (InterruptedException ex) { |
| 92 | + throw new RuntimeException(ex); |
81 | 93 | }
|
82 | 94 | }
|
83 |
| - |
84 |
| - protected Object getOrCreateMutex(final Reference<K> keyRef) { |
85 |
| - return producerMutexes.computeIfAbsent(keyRef, newProducerLock()); |
86 |
| - } |
87 |
| - |
| 95 | + |
88 | 96 | private void expungeStaleEntries() {
|
| 97 | + if (null == queue) { |
| 98 | + return; |
| 99 | + } |
89 | 100 | for (Reference<? extends K> ref; (ref = queue.poll()) != null;) {
|
90 | 101 | @SuppressWarnings("unchecked")
|
91 | 102 | Reference<K> keyRef = (Reference<K>) ref;
|
92 |
| - // keyRef now is equal only to itself while referent is cleared |
93 |
| - // already |
94 |
| - // so it's safe to remove it without ceremony (like |
95 |
| - // getOrCreateMutex(keyRef) usage) |
| 103 | + // keyRef now is equal only to itself while referent is cleared already |
| 104 | + // so it's safe to remove it without ceremony (like getOrCreateMutex(keyRef) usage) |
96 | 105 | valueMap.remove(keyRef);
|
97 | 106 | }
|
98 | 107 | }
|
99 |
| - |
100 |
| - |
101 |
| - @SuppressWarnings("unchecked") |
102 |
| - private static <K> Function<K, Object> newProducerLock() { |
103 |
| - return (Function<K, Object>)NEW_PRODUCER_LOCK; |
104 |
| - } |
105 |
| - |
106 |
| - static class KeyReference<K> extends WeakReference<K> { |
107 |
| - private final int referentHashCode; |
108 |
| - |
109 |
| - KeyReference(K key) { |
110 |
| - this(key, null); |
111 |
| - } |
112 |
| - |
113 |
| - KeyReference(K key, ReferenceQueue<K> queue) { |
114 |
| - super(key, queue); |
115 |
| - referentHashCode = key == null ? 0 : key.hashCode(); |
116 |
| - } |
117 |
| - |
118 |
| - public int hashCode() { |
119 |
| - return referentHashCode; |
120 |
| - } |
121 |
| - |
122 |
| - public boolean equals(Object other) { |
123 |
| - if (this == other) |
124 |
| - return true; |
125 |
| - if (null == other || other.getClass() != KeyReference.class) |
126 |
| - return false; |
127 |
| - Object r1 = this.get(); |
128 |
| - Object r2 = ((KeyReference<?>) other).get(); |
129 |
| - return null == r1 ? null == r2 : r1.equals(r2); |
130 |
| - } |
131 |
| - } |
132 |
| - |
133 |
| - |
134 |
| - |
135 |
| - private static final Function<Object, Object> NEW_PRODUCER_LOCK = k -> new Object(); |
136 | 108 | }
|
0 commit comments