@@ -88,14 +88,15 @@ boolean isExpired() {
88
88
* Constructs a new {@code LIFOCache} instance using the provided {@link Builder}.
89
89
*
90
90
* <p>This constructor initializes the cache with the specified capacity and default TTL,
91
- * sets up internal data structures (a {@code LinkedHashMap} for cache entries and configures eviction.
91
+ * sets up internal data structures (a {@code HashMap} for cache entries,
92
+ * {an @code ArrayDeque}, for key storage, and configures eviction.
92
93
*
93
94
* @param builder the {@code Builder} object containing configuration parameters
94
95
*/
95
96
private LIFOCache (Builder <K , V > builder ) {
96
97
this .capacity = builder .capacity ;
97
98
this .defaultTTL = builder .defaultTTL ;
98
- this .cache = HashMap . newHashMap (builder .capacity );
99
+ this .cache = new HashMap <> (builder .capacity );
99
100
this .keys = new ArrayDeque <>(builder .capacity );
100
101
this .lock = new ReentrantLock ();
101
102
this .evictionListener = builder .evictionListener ;
@@ -123,7 +124,7 @@ public V get(K key) {
123
124
try {
124
125
evictionStrategy .onAccess (this );
125
126
126
- CacheEntry <V > entry = cache .get (key );
127
+ final CacheEntry <V > entry = cache .get (key );
127
128
if (entry == null || entry .isExpired ()) {
128
129
if (entry != null ) {
129
130
cache .remove (key );
@@ -178,7 +179,7 @@ public void put(K key, V value, long ttlMillis) {
178
179
try {
179
180
// If key already exists, remove it. It will later be re-inserted at top of stack
180
181
keys .remove (key );
181
- CacheEntry <V > oldEntry = cache .remove (key );
182
+ final CacheEntry <V > oldEntry = cache .remove (key );
182
183
if (oldEntry != null && !oldEntry .isExpired ()) {
183
184
notifyEviction (key , oldEntry .value );
184
185
}
@@ -188,8 +189,8 @@ public void put(K key, V value, long ttlMillis) {
188
189
189
190
// If no expired entry was removed, remove the youngest
190
191
if (cache .size () >= capacity ) {
191
- K youngestKey = keys .pollLast ();
192
- CacheEntry <V > youngestEntry = cache .remove (youngestKey );
192
+ final K youngestKey = keys .pollLast ();
193
+ final CacheEntry <V > youngestEntry = cache .remove (youngestKey );
193
194
notifyEviction (youngestKey , youngestEntry .value );
194
195
}
195
196
@@ -210,11 +211,11 @@ public void put(K key, V value, long ttlMillis) {
210
211
*/
211
212
private int evictExpired () {
212
213
int count = 0 ;
213
- Iterator <K > it = keys .iterator ();
214
+ final Iterator <K > it = keys .iterator ();
214
215
215
216
while (it .hasNext ()) {
216
- K k = it .next ();
217
- CacheEntry <V > entry = cache .get (k );
217
+ final K k = it .next ();
218
+ final CacheEntry <V > entry = cache .get (k );
218
219
if (entry != null && entry .isExpired ()) {
219
220
it .remove ();
220
221
cache .remove (k );
@@ -238,7 +239,7 @@ public V removeKey(K key) {
238
239
}
239
240
lock .lock ();
240
241
try {
241
- CacheEntry <V > entry = cache .remove (key );
242
+ final CacheEntry <V > entry = cache .remove (key );
242
243
keys .remove (key );
243
244
244
245
// No such key in cache
@@ -361,7 +362,7 @@ public void clear() {
361
362
public Set <K > getAllKeys () {
362
363
lock .lock ();
363
364
try {
364
- Set <K > result = new HashSet <>();
365
+ final Set <K > result = new HashSet <>();
365
366
366
367
for (Map .Entry <K , CacheEntry <V >> entry : cache .entrySet ()) {
367
368
if (!entry .getValue ().isExpired ()) {
@@ -397,7 +398,7 @@ public EvictionStrategy<K, V> getEvictionStrategy() {
397
398
public String toString () {
398
399
lock .lock ();
399
400
try {
400
- Map <K , V > visible = new LinkedHashMap <>();
401
+ final Map <K , V > visible = new LinkedHashMap <>();
401
402
for (Map .Entry <K , CacheEntry <V >> entry : cache .entrySet ()) {
402
403
if (!entry .getValue ().isExpired ()) {
403
404
visible .put (entry .getKey (), entry .getValue ().value );
0 commit comments