|
35 | 35 | import java.util.concurrent.ConcurrentHashMap; |
36 | 36 |
|
37 | 37 | /** |
38 | | - * a soft (reference) or limited (hard) cache implementation |
| 38 | + * a cache of a kind |
39 | 39 | * |
40 | 40 | * @author kares |
41 | 41 | */ |
42 | | -final class Cache<K, T> { |
| 42 | +public class Cache<K, T> { |
43 | 43 |
|
44 | | - private final Map<K, SoftRef<K, T>> map; // SoftHashMap |
| 44 | + private static Cache NULL; |
45 | 45 |
|
46 | | - private final ReferenceQueue<T> refQueue = new ReferenceQueue<T>(); |
| 46 | + private Cache() { /* empty-cache */ } |
47 | 47 |
|
48 | | - private final int limit; |
| 48 | + @SuppressWarnings("unchecked") |
| 49 | + public static <K, T> Cache<K, T> getNullCache() { |
| 50 | + if ( NULL != null ) return NULL; |
| 51 | + return NULL = new Cache<K, T>(); |
| 52 | + } |
49 | 53 |
|
50 | | - private final SortedMap<SoftRef<K, T>, T> hardRefs; // final Deque<T> hard; |
| 54 | + /** |
| 55 | + * a soft-reference cache |
| 56 | + * @param <K> |
| 57 | + * @param <T> |
| 58 | + * @return new cache instance |
| 59 | + */ |
| 60 | + public static <K, T> Cache<K, T> newSoftCache() { |
| 61 | + return new SoftCache<K, T>(); |
| 62 | + } |
51 | 63 |
|
52 | | - private Cache() { |
53 | | - this.limit = 0; |
54 | | - this.map = new ConcurrentHashMap<K, SoftRef<K, T>>(); |
55 | | - this.hardRefs = null; |
| 64 | + /** |
| 65 | + * a soft-reference cache which holds strong references up to the specified |
| 66 | + * size, these are arranged in LRU order |
| 67 | + * @param <K> |
| 68 | + * @param <T> |
| 69 | + * @param size |
| 70 | + * @return new cache instance |
| 71 | + */ |
| 72 | + public static <K, T> Cache<K, T> newStrongSoftCache(final int size) { |
| 73 | + return new SoftCache<K, T>(size); |
56 | 74 | } |
57 | 75 |
|
58 | | - private Cache(final int size) { |
59 | | - this.limit = size; |
60 | | - final int capacity = Math.min(size, 32); |
61 | | - this.map = new ConcurrentHashMap<K, SoftRef<K, T>>(capacity); |
62 | | - this.hardRefs = new TreeMap<SoftRef<K, T>, T>(); |
| 76 | + public T get(K key) { |
| 77 | + return null; |
63 | 78 | } |
64 | 79 |
|
65 | | - public static <K, T> Cache<K, T> newSoftCache() { |
66 | | - return new Cache<K, T>(); |
| 80 | + public T put(K key, T value) { |
| 81 | + return null; |
67 | 82 | } |
68 | 83 |
|
69 | | - public static <K, T> Cache<K, T> newLRUCache(final int size) { |
70 | | - return new Cache<K, T>(size); |
| 84 | + public T remove(K key) { |
| 85 | + return null; |
71 | 86 | } |
72 | 87 |
|
73 | | - public T get(K key) { |
74 | | - T result = null; |
75 | | - final SoftRef<K, T> ref = map.get(key); |
76 | | - if (ref != null) { |
77 | | - result = ref.get(); |
78 | | - if ( result == null ) { |
79 | | - map.remove(key); |
80 | | - } |
81 | | - else { |
82 | | - if ( hardRefs != null ) { |
83 | | - synchronized (hardRefs) { |
84 | | - hardRefs.remove(ref); |
85 | | - hardRefs.put(ref.recordAccess(), result); |
86 | | - if ( limit > 0 && hardRefs.size() > limit ) { |
87 | | - hardRefs.remove( hardRefs.firstKey() ); |
| 88 | + public void clear() { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + public int size() { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + static final class SoftCache<K, T> extends Cache<K, T> { |
| 97 | + |
| 98 | + private final Map<K, Ref<K, T>> cache; // SoftHashMap |
| 99 | + |
| 100 | + private final ReferenceQueue<T> refQueue = new ReferenceQueue<T>(); |
| 101 | + |
| 102 | + private final int strongLimit; |
| 103 | + private final SortedMap<Ref<K, T>, T> strongRefs; // final Deque<T> strong; |
| 104 | + |
| 105 | + private SoftCache() { |
| 106 | + this.strongLimit = 0; |
| 107 | + this.cache = new ConcurrentHashMap<K, Ref<K, T>>(); |
| 108 | + this.strongRefs = null; |
| 109 | + } |
| 110 | + |
| 111 | + private SoftCache(final int limit) { |
| 112 | + this.strongLimit = limit; |
| 113 | + final int capacity = Math.min(limit, 32); |
| 114 | + this.cache = new ConcurrentHashMap<K, Ref<K, T>>(capacity); |
| 115 | + this.strongRefs = new TreeMap<Ref<K, T>, T>(); |
| 116 | + } |
| 117 | + |
| 118 | + public T get(K key) { |
| 119 | + T result = null; |
| 120 | + final Ref<K, T> ref = cache.get(key); |
| 121 | + if ( ref != null ) { |
| 122 | + result = ref.get(); |
| 123 | + if ( result == null ) cache.remove(key); |
| 124 | + else { |
| 125 | + if ( strongRefs != null ) { |
| 126 | + synchronized (strongRefs) { |
| 127 | + strongRefs.remove(ref); |
| 128 | + strongRefs.put(ref.recordAccess(), result); |
| 129 | + if ( strongLimit > 0 && strongRefs.size() > strongLimit ) { |
| 130 | + strongRefs.remove( strongRefs.firstKey() ); |
| 131 | + } |
88 | 132 | } |
89 | 133 | } |
90 | 134 | } |
91 | 135 | } |
92 | | - } |
93 | | - return result; |
94 | | - } |
95 | | - |
96 | | - private static class SoftRef<K, T> extends SoftReference<T> implements Comparable<SoftRef> { |
97 | | - private final K key; |
98 | | - volatile long access; |
| 136 | + return result; |
| 137 | + } |
99 | 138 |
|
100 | | - SoftRef(T value, K key, ReferenceQueue<T> queue) { |
101 | | - super(value, queue); |
102 | | - this.key = key; |
103 | | - recordAccess(); |
| 139 | + public T put(K key, T value) { |
| 140 | + purgeRefQueue(); |
| 141 | + final SoftReference<T> prev = cache.put(key, new Ref<K, T>(value, key, refQueue)); |
| 142 | + return prev == null ? null : prev.get(); |
104 | 143 | } |
105 | 144 |
|
106 | | - final SoftRef<K, T> recordAccess() { access = System.currentTimeMillis(); return this; } |
| 145 | + public T remove(K key) { |
| 146 | + purgeRefQueue(); |
| 147 | + final SoftReference<T> removed = cache.remove(key); |
| 148 | + return removed == null ? null : removed.get(); |
| 149 | + } |
107 | 150 |
|
108 | | - @Override |
109 | | - public boolean equals(Object obj) { |
110 | | - if ( obj instanceof SoftRef ) { |
111 | | - return this.key.equals( ((SoftRef) obj).key ); |
| 151 | + public void clear() { |
| 152 | + if ( strongRefs != null ) { |
| 153 | + synchronized (strongRefs) { strongRefs.clear(); } |
112 | 154 | } |
113 | | - return false; |
| 155 | + purgeRefQueue(); |
| 156 | + cache.clear(); |
| 157 | + purgeRefQueue(); |
114 | 158 | } |
115 | 159 |
|
116 | | - @Override |
117 | | - public int hashCode() { |
118 | | - return key.hashCode(); |
| 160 | + public int size() { |
| 161 | + purgeRefQueue(); |
| 162 | + return cache.size(); |
119 | 163 | } |
120 | 164 |
|
121 | | - @Override // order by access time - more recent first (less than) others |
122 | | - public int compareTo(final SoftRef that) { |
123 | | - final long diff = this.access - that.access; |
124 | | - if ( diff == 0 ) return 0; |
125 | | - // diff > 0 ... this.access > that.access ... this > that |
126 | | - return diff > 0 ? +1 : -1; // this accessed after that |
| 165 | + @SuppressWarnings("unchecked") |
| 166 | + private void purgeRefQueue() { |
| 167 | + Ref<K, T> ref; |
| 168 | + while ( ( ref = (Ref) refQueue.poll() ) != null ) { |
| 169 | + synchronized (refQueue) { cache.remove( ref.key ); } |
| 170 | + } |
127 | 171 | } |
128 | | - } |
129 | 172 |
|
130 | | - @SuppressWarnings("unchecked") |
131 | | - private void purgeRefQueue() { |
132 | | - SoftRef<K, T> ref; |
133 | | - while ( ( ref = (SoftRef) refQueue.poll() ) != null ) { |
134 | | - synchronized (refQueue) { map.remove( ref.key ); } |
135 | | - } |
136 | | - } |
| 173 | + private static class Ref<K, T> extends SoftReference<T> implements Comparable<Ref> { |
| 174 | + private final K key; |
| 175 | + volatile long access; |
137 | 176 |
|
138 | | - public T put(K key, T value) { |
139 | | - purgeRefQueue(); |
140 | | - final SoftReference<T> prev = map.put(key, new SoftRef<K, T>(value, key, refQueue)); |
141 | | - return prev == null ? null : prev.get(); |
142 | | - } |
| 177 | + private Ref(T value, K key, ReferenceQueue<T> queue) { |
| 178 | + super(value, queue); |
| 179 | + this.key = key; |
| 180 | + recordAccess(); |
| 181 | + } |
143 | 182 |
|
144 | | - public T remove(K key) { |
145 | | - purgeRefQueue(); |
146 | | - final SoftReference<T> removed = map.remove(key); |
147 | | - return removed == null ? null : removed.get(); |
148 | | - } |
| 183 | + final Ref<K, T> recordAccess() { access = System.currentTimeMillis(); return this; } |
149 | 184 |
|
150 | | - public void clear() { |
151 | | - if ( hardRefs != null ) { |
152 | | - synchronized (hardRefs) { hardRefs.clear(); } |
| 185 | + @Override |
| 186 | + public boolean equals(Object obj) { |
| 187 | + if ( obj instanceof Ref ) { |
| 188 | + return this.key.equals( ((Ref) obj).key ); |
| 189 | + } |
| 190 | + return false; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public int hashCode() { |
| 195 | + return key.hashCode(); |
| 196 | + } |
| 197 | + |
| 198 | + @Override // order by access time - more recent first (less than) others |
| 199 | + public int compareTo(final Ref that) { |
| 200 | + final long diff = this.access - that.access; |
| 201 | + if ( diff == 0 ) return 0; |
| 202 | + // diff > 0 ... this.access > that.access ... this > that |
| 203 | + return diff > 0 ? +1 : -1; // this accessed after that |
| 204 | + } |
153 | 205 | } |
154 | | - purgeRefQueue(); |
155 | | - map.clear(); |
156 | | - } |
157 | 206 |
|
158 | | - public int size() { |
159 | | - purgeRefQueue(); |
160 | | - return map.size(); |
161 | 207 | } |
162 | 208 |
|
163 | 209 | } |
0 commit comments