You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2025/03/24 14:45:51 UTC
(camel) 02/02: CAMEL-21888: camel-core - Compute cache changes size
This is an automated email from the ASF dual-hosted git repository.
nfilotto pushed a commit to branch essobedo/CAMEL-21888/compress-changes-only-if-needed-4.8
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 53cab109c109923d7fa5eb1579184f1283548af7
Author: Nicolas Filotto <ni...@qlik.com>
AuthorDate: Sun Mar 23 08:35:07 2025 +0100
CAMEL-21888: camel-core - Compute cache changes size
---
.../org/apache/camel/support/cache/SimpleLRUCache.java | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 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 85e8bda077f..5c3e79be57f 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
@@ -25,6 +25,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
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.ReadWriteLock;
@@ -66,6 +67,10 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
*/
private final AtomicReference<Deque<Entry<K, ValueHolder<V>>>> lastChanges
= new AtomicReference<>(new ConcurrentLinkedDeque<>());
+ /**
+ * The total amount of changes recorded.
+ */
+ private final AtomicInteger totalChanges = new AtomicInteger();
/**
* The function to call when an entry is evicted.
*/
@@ -103,6 +108,7 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
}
ValueHolder<V> holder = newValue(value);
lastChanges.get().add(Map.entry(key, holder));
+ totalChanges.incrementAndGet();
return holder;
}
@@ -292,6 +298,7 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
lock.writeLock().lock();
try {
lastChanges.getAndSet(new ConcurrentLinkedDeque<>());
+ totalChanges.set(0);
delegate.clear();
} finally {
lock.writeLock().unlock();
@@ -335,7 +342,7 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
* @return the size of the queue of changes.
*/
int getQueueSize() {
- return lastChanges.get().size();
+ return totalChanges.get();
}
/**
@@ -370,7 +377,9 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
* @return the oldest existing change.
*/
private Entry<K, ValueHolder<V>> nextOldestChange() {
- return lastChanges.get().poll();
+ Entry<K, ValueHolder<V>> oldestChange = lastChanges.get().poll();
+ totalChanges.decrementAndGet();
+ return oldestChange;
}
/**
@@ -383,6 +392,7 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
try {
if (isQueueFull()) {
newChanges = new ConcurrentLinkedDeque<>();
+ totalChanges.set(0);
currentChanges = lastChanges.getAndSet(newChanges);
} else {
return;
@@ -395,6 +405,7 @@ public class SimpleLRUCache<K, V> implements Map<K, V> {
while ((entry = currentChanges.pollLast()) != null) {
if (keys.add(entry.getKey())) {
newChanges.addFirst(entry);
+ totalChanges.incrementAndGet();
}
}
}