This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch 
essobedo/CAMEL-23267/remove-outdated-changes-on-compress
in repository https://gitbox.apache.org/repos/asf/camel.git

commit cb663449874aa1447cc8b42cea7278e4880a2bb0
Author: Nicolas Filotto <[email protected]>
AuthorDate: Thu Apr 9 08:19:21 2026 +0200

    CAMEL-23267: Remove outdated changes on compress
---
 .../org/apache/camel/support/cache/SimpleLRUCache.java | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java
index 29f1884a9e46..a2d14becb517 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java
@@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.function.BiFunction;
@@ -295,13 +296,14 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
 
     @Override
     public void clear() {
-        lock.writeLock().lock();
+        Lock writeLock = lock.writeLock();
+        writeLock.lock();
         try {
             lastChanges.getAndSet(new ConcurrentLinkedDeque<>());
             totalChanges.set(0);
             delegate.clear();
         } finally {
-            lock.writeLock().unlock();
+            writeLock.unlock();
         }
     }
 
@@ -383,7 +385,7 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
     }
 
     /**
-     * Removes duplicates from the queue of changes if the queue is full.
+     * Removes duplicates and already removed entries from the queue of 
changes if the queue is full.
      */
     private void compressChangesIfNeeded() {
         if (isQueueFull()) {
@@ -392,7 +394,10 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
             Set<K> keys = new HashSet<>();
             Entry<K, ValueHolder<V>> entry;
             while ((entry = currentChanges.pollLast()) != null) {
-                if (keys.add(entry.getKey())) {
+                K key = entry.getKey();
+                // Keep only entries whose key is still present and remove 
duplicates by keeping only the most recent
+                // entry for each key
+                if (delegate.containsKey(key) && keys.add(key)) {
                     newChanges.addFirst(entry);
                 }
             }
@@ -419,7 +424,8 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
      * that no changes are added to the cache while the eviction is in 
progress, to prevent infinite eviction.
      */
     private void callEviction() {
-        lock.writeLock().lock();
+        Lock writeLock = lock.writeLock();
+        writeLock.lock();
         try {
             compressChangesIfNeeded();
             while (isCacheFull()) {
@@ -429,7 +435,7 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
                 }
             }
         } finally {
-            lock.writeLock().unlock();
+            writeLock.unlock();
         }
     }
 

Reply via email to