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

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git


The following commit(s) were added to refs/heads/master by this push:
     new 9ac6bdd3 Use Atomic* for statistics
9ac6bdd3 is described below

commit 9ac6bdd3fba1448bacf3ce664b68c43b6557a5fa
Author: Thomas Vandahl <[email protected]>
AuthorDate: Sun Sep 6 14:14:54 2026 +0200

    Use Atomic* for statistics
---
 .../jcs4/auxiliary/disk/AbstractDiskCache.java     | 30 ++++++++-------
 .../auxiliary/disk/indexed/IndexedDiskCache.java   | 45 +++++++++++++---------
 .../jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java    |  8 ++--
 .../jcs4/engine/control/CompositeCache.java        |  4 +-
 4 files changed, 48 insertions(+), 39 deletions(-)

diff --git 
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java
 
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java
index b0f97a90..6af65439 100644
--- 
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java
+++ 
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/AbstractDiskCache.java
@@ -27,7 +27,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.apache.commons.jcs4.auxiliary.AbstractAuxiliaryCacheEventLogging;
@@ -123,7 +123,6 @@ public abstract class AbstractDiskCache<K, V>
                         // threads and still have removeAll requests come in 
that
                         // always win
                         removeAllLock.readLock().lock();
-
                         try
                         {
                             // If the element has already been removed from
@@ -239,7 +238,7 @@ public abstract class AbstractDiskCache<K, V>
     private final AtomicBoolean alive = new AtomicBoolean();
 
     /** DEBUG: Keeps a count of the number of purgatory hits for debug 
messages */
-    private final AtomicInteger purgHits = new AtomicInteger();
+    private final AtomicLong purgHits = new AtomicLong();
 
     /**
      * We lock here, so that we cannot get an update after a remove all. an 
individual removal locks
@@ -320,7 +319,7 @@ public abstract class AbstractDiskCache<K, V>
         // If the element was found in purgatory
         if ( pe != null )
         {
-            int p = purgHits.incrementAndGet();
+            long p = purgHits.incrementAndGet();
 
             if ( p % 100 == 0 )
             {
@@ -472,23 +471,26 @@ public abstract class AbstractDiskCache<K, V>
      */
     private void initPurgatory()
     {
+        final Map<K, PurgatoryElement<K, V>> newPurgatory;
+
+        int maxPurgatorySize = 
getAuxiliaryCacheAttributes().getMaxPurgatorySize();
+        if (maxPurgatorySize >= 0)
+        {
+            newPurgatory = Collections.synchronizedMap(new 
LRUMap<>(maxPurgatorySize));
+        }
+        else
+        {
+            newPurgatory = new ConcurrentHashMap<>();
+        }
+
         // we need this so we can stop the updates from happening after a
         // remove all
         removeAllLock.writeLock().lock();
-
         try
         {
             synchronized (this)
             {
-                int maxPurgatorySize = 
getAuxiliaryCacheAttributes().getMaxPurgatorySize();
-                if (maxPurgatorySize >= 0)
-                {
-                    purgatory = Collections.synchronizedMap(new 
LRUMap<>(maxPurgatorySize));
-                }
-                else
-                {
-                    purgatory = new ConcurrentHashMap<>();
-                }
+                purgatory = newPurgatory;
             }
         }
         finally
diff --git 
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCache.java
 
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCache.java
index 480c1e9a..7e580d51 100644
--- 
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCache.java
+++ 
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCache.java
@@ -241,7 +241,7 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
     private File rafDir;
 
     /** Should we keep adding to the recycle bin. False during optimization. */
-    private AtomicBoolean doRecycle = new AtomicBoolean(true);
+    private AtomicBoolean doRecycle;
 
     /** Should we optimize real time */
     private boolean isRealTimeOptimizationEnabled = true;
@@ -250,19 +250,19 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
     private boolean isShutdownOptimizationEnabled = true;
 
     /** Are we currently optimizing the files */
-    private final AtomicBoolean isOptimizing = new AtomicBoolean();
+    private final AtomicBoolean isOptimizing;
 
     /** The number of times the file has been optimized. */
-    private int timesOptimized;
+    private final AtomicInteger timesOptimized;
 
     /** The Executor for optimizing the file. */
     private volatile ExecutorService optimizationExecutor;
 
     /** Used for counting the number of requests */
-    private int removeCount;
+    private final AtomicInteger removeCount;
 
     /** Should we queue puts. True when optimizing. We write the queue post 
optimization. */
-    private boolean queueInput;
+    private final AtomicBoolean queueInput;
 
     /** List where puts made during optimization are made */
     private final ConcurrentSkipListSet<IndexedDiskElementDescriptor> 
queuedPutList;
@@ -271,7 +271,7 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
     private final ConcurrentSkipListSet<IndexedDiskElementDescriptor> recycle;
 
     /** How many slots have we recycled. */
-    private int recycleCnt;
+    private final AtomicInteger recycleCnt;
 
     /** How many items were there on startup. */
     private int startupSize;
@@ -283,7 +283,7 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
     private DiskLimitType diskLimitType = DiskLimitType.COUNT;
 
     /** Simple stat */
-    private final AtomicInteger hitCount = new AtomicInteger();
+    private final AtomicLong hitCount;
 
     /**
      * Use this lock to synchronize reads and writes to the underlying storage 
mechanism.
@@ -330,6 +330,13 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
         this.keyHash = createInitialKeyMap();
         this.queuedPutList = new 
ConcurrentSkipListSet<>(Comparator.comparing(ded1 -> ded1.pos()));
         this.recycle = new ConcurrentSkipListSet<>();
+        this.isOptimizing = new AtomicBoolean();
+        this.doRecycle = new AtomicBoolean(true);
+        this.queueInput = new AtomicBoolean();
+        this.timesOptimized = new AtomicInteger();
+        this.recycleCnt = new AtomicInteger();
+        this.removeCount = new AtomicInteger();
+        this.hitCount = new AtomicLong();
 
         try
         {
@@ -662,7 +669,7 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
     protected void doOptimizeRealTime()
     {
         int optRemoveCount = 
getAuxiliaryCacheAttributes().getOptimizeAtRemoveCount();
-        if (isRealTimeOptimizationEnabled && removeCount++ >= optRemoveCount)
+        if (isRealTimeOptimizationEnabled && removeCount.getAndIncrement() >= 
optRemoveCount)
         {
             if (isOptimizing.compareAndSet(false, true))
             {
@@ -822,7 +829,7 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
      */
     protected int getRecyleCount()
     {
-        return this.recycleCnt;
+        return this.recycleCnt.get();
     }
 
     /**
@@ -861,9 +868,9 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
         stats.addStatElement("Max Key Size", this.maxKeySize);
         stats.addStatElement("Hit Count", this.hitCount);
         stats.addStatElement("Bytes Free", this.bytesFree);
-        stats.addStatElement("Optimize Operation Count", 
Integer.valueOf(this.removeCount));
-        stats.addStatElement("Times Optimized", 
Integer.valueOf(this.timesOptimized));
-        stats.addStatElement("Recycle Count", 
Integer.valueOf(this.recycleCnt));
+        stats.addStatElement("Optimize Operation Count", this.removeCount);
+        stats.addStatElement("Times Optimized", this.timesOptimized);
+        stats.addStatElement("Recycle Count", this.recycleCnt);
         stats.addStatElement("Recycle Bin Size", 
Integer.valueOf(this.recycle.size()));
         stats.addStatElement("Startup Size", 
Integer.valueOf(this.startupSize));
 
@@ -882,7 +889,7 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
      */
     protected int getTimesOptimized()
     {
-        return timesOptimized;
+        return timesOptimized.get();
     }
 
     /**
@@ -1048,7 +1055,7 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
     protected void optimizeFile()
     {
         final ElapsedTimer timer = new ElapsedTimer();
-        timesOptimized++;
+        timesOptimized.incrementAndGet();
         log.info("{0}: Beginning Optimization #{1}", logCacheName, 
timesOptimized);
 
         // CREATE SNAPSHOT
@@ -1058,7 +1065,7 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
 
         try
         {
-            queueInput = true;
+            queueInput.set(true);
             // shut off recycle while we're optimizing,
             doRecycle.set(false);
             defragList = createPositionSortedDescriptorList();
@@ -1093,11 +1100,11 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
             }
 
             // RESTORE NORMAL OPERATION
-            removeCount = 0;
+            removeCount.set(0);
             resetBytesFree();
             this.recycle.clear();
             queuedPutList.clear();
-            queueInput = false;
+            queueInput.set(false);
             // turn recycle back on.
             doRecycle.set(true);
             isOptimizing.set(false);
@@ -1452,14 +1459,14 @@ public class IndexedDiskCache<K, V> extends 
AbstractDiskCache<K, V>
                             // remove element from recycle bin
                             recycle.remove(rep);
                             ded = new IndexedDiskElementDescriptor(rep.pos(), 
data.length);
-                            recycleCnt++;
+                            recycleCnt.incrementAndGet();
                             this.adjustBytesFree(ded, false);
                             log.debug("{0}: using recycled ded {1} rep.len = 
{2} ded.len = {3}",
                                     logCacheName, ded.pos(), rep.len(), 
ded.len());
                         }
                     }
 
-                    if (queueInput)
+                    if (queueInput.get())
                     {
                         queuedPutList.add(ded);
                         log.debug("{0}: added to queued put list. {1}",
diff --git 
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java
 
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java
index 0823a5d3..a639595a 100644
--- 
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java
+++ 
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java
@@ -29,7 +29,7 @@ import java.time.Instant;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
 
 import javax.sql.DataSource;
 
@@ -81,13 +81,13 @@ public class JDBCDiskCache<K, V>
     private JDBCDiskCacheAttributes jdbcDiskCacheAttributes;
 
     /** # of times update was called */
-    private final AtomicInteger updateCount = new AtomicInteger();
+    private final AtomicLong updateCount = new AtomicLong();
 
     /** # of times get was called */
-    private final AtomicInteger getCount = new AtomicInteger();
+    private final AtomicLong getCount = new AtomicLong();
 
     /** # of times getMatching was called */
-    private final AtomicInteger getMatchingCount = new AtomicInteger();
+    private final AtomicLong getMatchingCount = new AtomicLong();
 
     /** Db connection pool */
     private final DataSourceFactory dsFactory;
diff --git 
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/control/CompositeCache.java
 
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/control/CompositeCache.java
index 4c279daf..48505385 100644
--- 
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/control/CompositeCache.java
+++ 
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/control/CompositeCache.java
@@ -891,8 +891,8 @@ public class CompositeCache<K, V>
         stats.setRegionName(this.getCacheName());
 
         // store the composite cache stats first
-        stats.addStatElement("HitCountRam", Long.valueOf(getHitCountRam()));
-        stats.addStatElement("HitCountAux", Long.valueOf(getHitCountAux()));
+        stats.addStatElement("HitCountRam", hitCountRam);
+        stats.addStatElement("HitCountAux", hitCountAux);
 
         // memory + aux, memory is not considered an auxiliary internally
         stats.addAuxiliaryCacheStats(getMemoryCache().getStatistics());

Reply via email to