-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
ConcurrentLruCache.java
608 lines (511 loc) · 14.9 KB
/
ConcurrentLruCache.java
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
* Copyright 2002-2022 the original author or authors.
*
* 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
*
* https://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 org.springframework.util;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import org.springframework.lang.Nullable;
/**
* Simple LRU (Least Recently Used) cache, bounded by a specified cache capacity.
* <p>This is a simplified, opinionated implementation of an LRU cache for internal
* use in Spring Framework. It is inspired from
* <a href="https://github.com/ben-manes/concurrentlinkedhashmap">ConcurrentLinkedHashMap</a>.
* <p>Read and write operations are internally recorded in dedicated buffers,
* then drained at chosen times to avoid contention.
*
* @author Brian Clozel
* @author Ben Manes
* @since 5.3
* @param <K> the type of the key used for cache retrieval
* @param <V> the type of the cached values, does not allow null values
* @see #get(Object)
*/
@SuppressWarnings({"unchecked"})
public final class ConcurrentLruCache<K, V> {
private final int capacity;
private final AtomicInteger currentSize = new AtomicInteger();
private final ConcurrentMap<K, Node<K, V>> cache;
private final Function<K, V> generator;
private final ReadOperations<K, V> readOperations;
private final WriteOperations writeOperations;
private final Lock evictionLock = new ReentrantLock();
/*
* Queue that contains all ACTIVE cache entries, ordered with least recently used entries first.
* Read and write operations are buffered and periodically processed to reorder the queue.
*/
private final EvictionQueue<K, V> evictionQueue = new EvictionQueue<>();
private final AtomicReference<DrainStatus> drainStatus = new AtomicReference<>(DrainStatus.IDLE);
/**
* Create a new cache instance with the given capacity and generator function.
* @param capacity the maximum number of entries in the cache
* (0 indicates no caching, always generating a new value)
* @param generator a function to generate a new value for a given key
*/
public ConcurrentLruCache(int capacity, Function<K, V> generator) {
this(capacity, generator, 16);
}
private ConcurrentLruCache(int capacity, Function<K, V> generator, int concurrencyLevel) {
Assert.isTrue(capacity > 0, "Capacity must be > 0");
this.capacity = capacity;
this.cache = new ConcurrentHashMap<>(16, 0.75f, concurrencyLevel);
this.generator = generator;
this.readOperations = new ReadOperations<>(this.evictionQueue);
this.writeOperations = new WriteOperations();
}
/**
* Retrieve an entry from the cache, potentially triggering generation of the value.
* @param key the key to retrieve the entry for
* @return the cached or newly generated value
*/
public V get(K key) {
final Node<K, V> node = this.cache.get(key);
if (node == null) {
V value = this.generator.apply(key);
put(key, value);
return value;
}
processRead(node);
return node.getValue();
}
private void put(K key, V value) {
Assert.notNull(key, "key must not be null");
Assert.notNull(value, "value must not be null");
final CacheEntry<V> cacheEntry = new CacheEntry<>(value, CacheEntryState.ACTIVE);
final Node<K, V> node = new Node<>(key, cacheEntry);
final Node<K, V> prior = this.cache.putIfAbsent(node.key, node);
if (prior == null) {
processWrite(new AddTask(node));
}
else {
processRead(prior);
}
}
private void processRead(Node<K, V> node) {
boolean drainRequested = this.readOperations.recordRead(node);
final DrainStatus status = this.drainStatus.get();
if (status.shouldDrainBuffers(drainRequested)) {
drainOperations();
}
}
private void processWrite(Runnable task) {
this.writeOperations.add(task);
this.drainStatus.lazySet(DrainStatus.REQUIRED);
drainOperations();
}
private void drainOperations() {
if (this.evictionLock.tryLock()) {
try {
this.drainStatus.lazySet(DrainStatus.PROCESSING);
this.readOperations.drain();
this.writeOperations.drain();
}
finally {
this.drainStatus.compareAndSet(DrainStatus.PROCESSING, DrainStatus.IDLE);
this.evictionLock.unlock();
}
}
}
/**
* Return the maximum number of entries in the cache.
* @see #size()
*/
public int capacity() {
return this.capacity;
}
/**
* Return the maximum number of entries in the cache.
* @deprecated in favor of {@link #capacity()} as of 6.0.
*/
@Deprecated(since = "6.0")
public int sizeLimit() {
return this.capacity;
}
/**
* Return the current size of the cache.
* @see #capacity()
*/
public int size() {
return this.cache.size();
}
/**
* Immediately remove all entries from this cache.
*/
public void clear() {
this.evictionLock.lock();
try {
Node<K, V> node;
while ((node = this.evictionQueue.poll()) != null) {
this.cache.remove(node.key, node);
markAsRemoved(node);
}
this.readOperations.clear();
this.writeOperations.drainAll();
}
finally {
this.evictionLock.unlock();
}
}
/*
* Transition the node to the {@code removed} state and decrement the current size of the cache.
*/
private void markAsRemoved(Node<K, V> node) {
for (; ; ) {
CacheEntry<V> current = node.get();
CacheEntry<V> removed = new CacheEntry<>(current.value, CacheEntryState.REMOVED);
if (node.compareAndSet(current, removed)) {
this.currentSize.lazySet(this.currentSize.get() - 1);
return;
}
}
}
/**
* Determine whether the given key is present in this cache.
* @param key the key to check for
* @return {@code true} if the key is present, {@code false} if there was no matching key
*/
public boolean contains(K key) {
return this.cache.containsKey(key);
}
/**
* Immediately remove the given key and any associated value.
* @param key the key to evict the entry for
* @return {@code true} if the key was present before,
* {@code false} if there was no matching key
*/
@Nullable
public boolean remove(K key) {
final Node<K, V> node = this.cache.remove(key);
if (node == null) {
return false;
}
markForRemoval(node);
processWrite(new RemovalTask(node));
return true;
}
/*
* Transition the node from the {@code active} state to the {@code pending removal} state,
* if the transition is valid.
*/
private void markForRemoval(Node<K, V> node) {
for (; ; ) {
final CacheEntry<V> current = node.get();
if (!current.isActive()) {
return;
}
final CacheEntry<V> pendingRemoval = new CacheEntry<>(current.value, CacheEntryState.PENDING_REMOVAL);
if (node.compareAndSet(current, pendingRemoval)) {
return;
}
}
}
/**
* Write operation recorded when a new entry is added to the cache.
*/
private final class AddTask implements Runnable {
final Node<K, V> node;
AddTask(Node<K, V> node) {
this.node = node;
}
@Override
public void run() {
currentSize.lazySet(currentSize.get() + 1);
if (this.node.get().isActive()) {
evictionQueue.add(this.node);
evictEntries();
}
}
private void evictEntries() {
while (currentSize.get() > capacity) {
final Node<K, V> node = evictionQueue.poll();
if (node == null) {
return;
}
cache.remove(node.key, node);
markAsRemoved(node);
}
}
}
/**
* Write operation recorded when an entry is removed to the cache.
*/
private final class RemovalTask implements Runnable {
final Node<K, V> node;
RemovalTask(Node<K, V> node) {
this.node = node;
}
@Override
public void run() {
evictionQueue.remove(this.node);
markAsRemoved(this.node);
}
}
/*
* Draining status for the read/write buffers.
*/
private enum DrainStatus {
/*
* No drain operation currently running.
*/
IDLE {
@Override
boolean shouldDrainBuffers(boolean delayable) {
return !delayable;
}
},
/*
* A drain operation is required due to a pending write modification.
*/
REQUIRED {
@Override
boolean shouldDrainBuffers(boolean delayable) {
return true;
}
},
/*
* A drain operation is in progress.
*/
PROCESSING {
@Override
boolean shouldDrainBuffers(boolean delayable) {
return false;
}
};
/**
* Determine whether the buffers should be drained.
* @param delayable if a drain should be delayed until required
* @return if a drain should be attempted
*/
abstract boolean shouldDrainBuffers(boolean delayable);
}
private enum CacheEntryState {
ACTIVE, PENDING_REMOVAL, REMOVED
}
private record CacheEntry<V>(V value, CacheEntryState state) {
boolean isActive() {
return this.state == CacheEntryState.ACTIVE;
}
}
private static final class ReadOperations<K, V> {
private static final int BUFFER_COUNT = detectNumberOfBuffers();
private static int detectNumberOfBuffers() {
int availableProcessors = Runtime.getRuntime().availableProcessors();
int nextPowerOfTwo = 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(availableProcessors - 1));
return Math.min(4, nextPowerOfTwo);
}
private static final int BUFFERS_MASK = BUFFER_COUNT - 1;
private static final int MAX_PENDING_OPERATIONS = 32;
private static final int MAX_DRAIN_COUNT = 2 * MAX_PENDING_OPERATIONS;
private static final int BUFFER_SIZE = 2 * MAX_DRAIN_COUNT;
private static final int BUFFER_INDEX_MASK = BUFFER_SIZE - 1;
/*
* Number of operations recorded, for each buffer
*/
private final AtomicLongArray recordedCount = new AtomicLongArray(BUFFER_COUNT);
/*
* Number of operations read, for each buffer
*/
private final long[] readCount = new long[BUFFER_COUNT];
/*
* Number of operations processed, for each buffer
*/
private final AtomicLongArray processedCount = new AtomicLongArray(BUFFER_COUNT);
@SuppressWarnings("rawtypes")
private final AtomicReferenceArray<Node<K, V>>[] buffers = new AtomicReferenceArray[BUFFER_COUNT];
private final EvictionQueue<K, V> evictionQueue;
@SuppressWarnings("rawtypes")
ReadOperations(EvictionQueue<K, V> evictionQueue) {
this.evictionQueue = evictionQueue;
for (int i = 0; i < BUFFER_COUNT; i++) {
this.buffers[i] = new AtomicReferenceArray<>(BUFFER_SIZE);
}
}
private static int getBufferIndex() {
return ((int) Thread.currentThread().getId()) & BUFFERS_MASK;
}
boolean recordRead(Node<K, V> node) {
int bufferIndex = getBufferIndex();
final long writeCount = this.recordedCount.get(bufferIndex);
this.recordedCount.lazySet(bufferIndex, writeCount + 1);
final int index = (int) (writeCount & BUFFER_INDEX_MASK);
this.buffers[bufferIndex].lazySet(index, node);
final long pending = (writeCount - this.processedCount.get(bufferIndex));
return (pending < MAX_PENDING_OPERATIONS);
}
void drain() {
final int start = (int) Thread.currentThread().getId();
final int end = start + BUFFER_COUNT;
for (int i = start; i < end; i++) {
drainReadBuffer(i & BUFFERS_MASK);
}
}
void clear() {
for (int i = 0; i < BUFFER_COUNT; i++) {
AtomicReferenceArray<Node<K, V>> buffer = this.buffers[i];
for (int j = 0; j < BUFFER_SIZE; j++) {
buffer.lazySet(j, null);
}
}
}
private void drainReadBuffer(int bufferIndex) {
final long writeCount = this.recordedCount.get(bufferIndex);
for (int i = 0; i < MAX_DRAIN_COUNT; i++) {
final int index = (int) (this.readCount[bufferIndex] & BUFFER_INDEX_MASK);
final AtomicReferenceArray<Node<K, V>> buffer = this.buffers[bufferIndex];
final Node<K, V> node = buffer.get(index);
if (node == null) {
break;
}
buffer.lazySet(index, null);
this.evictionQueue.moveToBack(node);
this.readCount[bufferIndex]++;
}
this.processedCount.lazySet(bufferIndex, writeCount);
}
}
private static final class WriteOperations {
private static final int DRAIN_THRESHOLD = 16;
private final Queue<Runnable> operations = new ConcurrentLinkedQueue<>();
public void add(Runnable task) {
this.operations.add(task);
}
public void drain() {
for (int i = 0; i < DRAIN_THRESHOLD; i++) {
final Runnable task = this.operations.poll();
if (task == null) {
break;
}
task.run();
}
}
public void drainAll() {
Runnable task;
while ((task = this.operations.poll()) != null) {
task.run();
}
}
}
@SuppressWarnings("serial")
private static final class Node<K, V> extends AtomicReference<CacheEntry<V>> {
final K key;
@Nullable
Node<K, V> prev;
@Nullable
Node<K, V> next;
Node(K key, CacheEntry<V> cacheEntry) {
super(cacheEntry);
this.key = key;
}
@Nullable
public Node<K, V> getPrevious() {
return this.prev;
}
public void setPrevious(@Nullable Node<K, V> prev) {
this.prev = prev;
}
@Nullable
public Node<K, V> getNext() {
return this.next;
}
public void setNext(@Nullable Node<K, V> next) {
this.next = next;
}
V getValue() {
return get().value;
}
}
private static final class EvictionQueue<K, V> {
@Nullable
Node<K, V> first;
@Nullable
Node<K, V> last;
@Nullable
Node<K, V> poll() {
if (this.first == null) {
return null;
}
final Node<K, V> f = this.first;
final Node<K, V> next = f.getNext();
f.setNext(null);
this.first = next;
if (next == null) {
this.last = null;
}
else {
next.setPrevious(null);
}
return f;
}
void add(Node<K, V> e) {
if (contains(e)) {
return;
}
linkLast(e);
}
private boolean contains(Node<K, V> e) {
return (e.getPrevious() != null)
|| (e.getNext() != null)
|| (e == this.first);
}
private void linkLast(final Node<K, V> e) {
final Node<K, V> l = this.last;
this.last = e;
if (l == null) {
this.first = e;
}
else {
l.setNext(e);
e.setPrevious(l);
}
}
private void unlink(Node<K, V> e) {
final Node<K, V> prev = e.getPrevious();
final Node<K, V> next = e.getNext();
if (prev == null) {
this.first = next;
}
else {
prev.setNext(next);
e.setPrevious(null);
}
if (next == null) {
this.last = prev;
}
else {
next.setPrevious(prev);
e.setNext(null);
}
}
void moveToBack(Node<K, V> e) {
if (contains(e) && e != this.last) {
unlink(e);
linkLast(e);
}
}
void remove(Node<K, V> e) {
if (contains(e)) {
unlink(e);
}
}
}
}