From 5a308ad5bd57203b610b14637b0fdde16f82bfa3 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Sat, 29 Jun 2019 00:15:30 +0200 Subject: [PATCH] Polish MimeTypeUtils LRU cache This commit improves the cache implementation by skipping the ordering of most recently used cached keys when the cache is at least half empty. See gh-23211 --- .../main/java/org/springframework/util/MimeTypeUtils.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java index c73cd4d8de..25fcc672f2 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -433,7 +433,13 @@ public abstract class MimeTypeUtils { public V get(K key) { this.lock.readLock().lock(); try { - if (this.queue.remove(key)) { + if (this.queue.size() < this.maxSize / 2) { + V cached = this.cache.get(key); + if (cached != null) { + return cached; + } + } + else if (this.queue.remove(key)) { this.queue.add(key); return this.cache.get(key); }