|
17 | 17 |
|
18 | 18 | import java.util.Iterator;
|
19 | 19 | import java.util.concurrent.ConcurrentHashMap;
|
| 20 | +import java.util.concurrent.atomic.AtomicInteger; |
20 | 21 | import java.util.function.Function;
|
21 | 22 | import software.amazon.awssdk.annotations.SdkProtectedApi;
|
22 | 23 | import software.amazon.awssdk.annotations.ThreadSafe;
|
23 |
| -import software.amazon.awssdk.utils.Logger; |
24 | 24 | import software.amazon.awssdk.utils.Validate;
|
25 | 25 |
|
26 | 26 | /**
|
27 |
| - * A thread-safe cache implementation that returns the value for a specified key, |
28 |
| - * retrieving it by either getting the stored value from the cache or using a supplied function to calculate that value |
29 |
| - * and add it to the cache. |
| 27 | + * A thread-safe cache implementation that returns the value for a specified key, retrieving it by either getting the stored |
| 28 | + * value from the cache or using a supplied function to calculate that value and add it to the cache. |
30 | 29 | * <p>
|
31 |
| - * When the cache is full, a new value will push out an unspecified value. |
| 30 | + * When the cache is full, batch eviction of random values will be performed, with a default evictionBatchSize of 10. |
32 | 31 | * <p>
|
33 |
| - * The user can configure the maximum size of the cache, which is set to a default of 100. |
| 32 | + * The user can configure the maximum size of the cache, which is set to a default of 150. |
34 | 33 | * <p>
|
35 |
| - * Null values are not cached. |
| 34 | + * Keys must not be null, otherwise an error will be thrown. Null values are not cached. |
36 | 35 | */
|
37 | 36 | @SdkProtectedApi
|
38 | 37 | @ThreadSafe
|
39 | 38 | public final class BoundedCache<K, V> {
|
40 |
| - |
41 |
| - private static final Logger log = Logger.loggerFor(BoundedCache.class); |
42 |
| - |
43 |
| - private static final int DEFAULT_SIZE = 100; |
| 39 | + private static final int DEFAULT_CACHE_SIZE = 150; |
| 40 | + private static final int DEFAULT_EVICTION_BATCH_SIZE = 10; |
44 | 41 |
|
45 | 42 | private final ConcurrentHashMap<K, V> cache;
|
46 |
| - private final Function<K, V> valueSupplier; |
| 43 | + private final Function<K, V> valueMappingFunction; |
47 | 44 | private final int maxCacheSize;
|
| 45 | + private final int evictionBatchSize; |
48 | 46 | private final Object cacheLock;
|
49 |
| - |
50 |
| - private BoundedCache(Builder<K, V> builder) { |
51 |
| - this.valueSupplier = builder.supplier; |
52 |
| - this.maxCacheSize = builder.maxSize != null ? |
53 |
| - Validate.isPositive(builder.maxSize, "maxSize") |
54 |
| - : DEFAULT_SIZE; |
| 47 | + private final AtomicInteger cacheSize; |
| 48 | + |
| 49 | + private BoundedCache(Builder<K, V> b) { |
| 50 | + this.valueMappingFunction = b.mappingFunction; |
| 51 | + this.maxCacheSize = b.maxSize != null ? Validate.isPositive(b.maxSize, "maxSize") : DEFAULT_CACHE_SIZE; |
| 52 | + this.evictionBatchSize = b.evictionBatchSize != null ? |
| 53 | + Validate.isPositive(b.evictionBatchSize, "evictionBatchSize") : |
| 54 | + DEFAULT_EVICTION_BATCH_SIZE; |
55 | 55 | this.cache = new ConcurrentHashMap<>();
|
56 | 56 | this.cacheLock = new Object();
|
| 57 | + this.cacheSize = new AtomicInteger(); |
57 | 58 | }
|
58 | 59 |
|
59 | 60 | /**
|
60 |
| - * Get a value based on the key. If the value exists in the cache, it's returned. |
| 61 | + * Get a value based on the key. The key must not be null, otherwise an error is thrown. |
| 62 | + * If the value exists in the cache, it's returned. |
61 | 63 | * Otherwise, the value is calculated based on the supplied function {@link Builder#builder(Function)}.
|
62 | 64 | */
|
63 | 65 | public V get(K key) {
|
| 66 | + Validate.paramNotNull(key, "key"); |
64 | 67 | V value = cache.get(key);
|
65 | 68 | if (value != null) {
|
66 | 69 | return value;
|
67 | 70 | }
|
68 | 71 |
|
69 |
| - V newValue = valueSupplier.apply(key); |
| 72 | + V newValue = valueMappingFunction.apply(key); |
| 73 | + |
| 74 | + // If the value is null, just return it without caching |
70 | 75 | if (newValue == null) {
|
71 | 76 | return null;
|
72 | 77 | }
|
73 | 78 |
|
74 | 79 | synchronized (cacheLock) {
|
| 80 | + // Check again inside the synchronized block in case another thread added the value |
75 | 81 | value = cache.get(key);
|
76 | 82 | if (value != null) {
|
77 | 83 | return value;
|
78 | 84 | }
|
79 | 85 |
|
80 |
| - if (cache.size() >= maxCacheSize) { |
| 86 | + if (cacheSize.get() >= maxCacheSize) { |
81 | 87 | cleanup();
|
82 | 88 | }
|
83 | 89 |
|
84 | 90 | cache.put(key, newValue);
|
| 91 | + cacheSize.incrementAndGet(); |
85 | 92 | return newValue;
|
86 | 93 | }
|
87 | 94 | }
|
88 | 95 |
|
89 | 96 | /**
|
90 |
| - * Clean up the cache by removing an unspecified entry |
| 97 | + * Clean up the cache by batch removing random entries of evictionBatchSize |
91 | 98 | */
|
92 | 99 | private void cleanup() {
|
93 | 100 | Iterator<K> iterator = cache.keySet().iterator();
|
94 |
| - if (iterator.hasNext()) { |
95 |
| - K key = iterator.next(); |
96 |
| - cache.remove(key); |
| 101 | + int count = 0; |
| 102 | + while (iterator.hasNext() && count < evictionBatchSize) { |
| 103 | + iterator.next(); |
| 104 | + iterator.remove(); |
| 105 | + count++; |
| 106 | + cacheSize.decrementAndGet(); |
97 | 107 | }
|
98 | 108 | }
|
99 | 109 |
|
100 | 110 | public int size() {
|
101 |
| - return cache.size(); |
| 111 | + return cacheSize.get(); |
102 | 112 | }
|
103 | 113 |
|
104 |
| - public static <K, V> BoundedCache.Builder<K, V> builder(Function<K, V> supplier) { |
105 |
| - return new Builder<>(supplier); |
| 114 | + public boolean containsKey(K key) { |
| 115 | + return cache.containsKey(key); |
| 116 | + } |
| 117 | + |
| 118 | + public static <K, V> BoundedCache.Builder<K, V> builder(Function<K, V> mappingFunction) { |
| 119 | + return new Builder<>(mappingFunction); |
106 | 120 | }
|
107 | 121 |
|
108 | 122 | public static final class Builder<K, V> {
|
109 | 123 |
|
110 |
| - private final Function<K, V> supplier; |
| 124 | + private final Function<K, V> mappingFunction; |
111 | 125 | private Integer maxSize;
|
| 126 | + private Integer evictionBatchSize; |
112 | 127 |
|
113 |
| - private Builder(Function<K, V> supplier) { |
114 |
| - this.supplier = supplier; |
| 128 | + private Builder(Function<K, V> mappingFunction) { |
| 129 | + this.mappingFunction = mappingFunction; |
115 | 130 | }
|
116 | 131 |
|
117 | 132 | public Builder<K, V> maxSize(Integer maxSize) {
|
118 | 133 | this.maxSize = maxSize;
|
119 | 134 | return this;
|
120 | 135 | }
|
121 | 136 |
|
| 137 | + public Builder<K, V> evictionBatchSize(Integer evictionBatchSize) { |
| 138 | + this.evictionBatchSize = evictionBatchSize; |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
122 | 142 | public BoundedCache<K, V> build() {
|
123 | 143 | return new BoundedCache<>(this);
|
124 | 144 | }
|
|
0 commit comments