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
commit 560fabaaecb544795a7fdc310a9b09d0b4657322 Author: Thomas Vandahl <[email protected]> AuthorDate: Fri Oct 2 18:16:19 2026 +0200 Improve Auxiliary lifecycle handling --- .../jcs4/auxiliary/disk/block/BlockDiskCache.java | 14 ++++---- .../jcs4/engine/control/CompositeCache.java | 12 +++++-- .../jcs4/engine/control/CompositeCacheManager.java | 39 +++++++++++++--------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCache.java index 96cbc251..b48b104d 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/auxiliary/disk/block/BlockDiskCache.java @@ -537,13 +537,6 @@ public class BlockDiskCache<K, V> @Override protected void processUpdate( final ICacheElement<K, V> element ) { - if ( !isAlive() ) - { - log.debug("{0}: No longer alive; aborting put of key = {1}", - () -> logCacheName, element::key); - return; - } - int[] old = null; // make sure this only locks for one particular cache region @@ -551,6 +544,13 @@ public class BlockDiskCache<K, V> try { + if ( !isAlive() ) + { + log.debug("{0}: No longer alive; aborting put of key = {1}", + () -> logCacheName, element::key); + return; + } + old = this.keyStore.get( element.key() ); if ( old != null ) 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 48505385..13940bc5 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 @@ -244,7 +244,7 @@ public class CompositeCache<K, V> // Dispose of each auxiliary cache, Remote auxiliaries will be // skipped if 'fromRemote' is true. - for (final ICache<K, V> aux : auxCaches) + for (final AuxiliaryCache<K, V> aux : auxCaches) { try { @@ -282,7 +282,15 @@ public class CompositeCache<K, V> } // Dispose of the auxiliary - aux.dispose(); + if (cacheManager == null) + { + aux.dispose(); + } + else + { + String auxName = aux.getAuxiliaryCacheAttributes().getName(); + cacheManager.freeAuxiliaryCache(auxName, getCacheName()); + } } catch (final IOException ex) { diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/control/CompositeCacheManager.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/control/CompositeCacheManager.java index 3c387566..454005ea 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/control/CompositeCacheManager.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/control/CompositeCacheManager.java @@ -82,11 +82,14 @@ public class CompositeCacheManager /** Default region prefix */ private static final String DEFAULT_REGION = "jcs.default"; + /** Format string for the key into the auxiliary cache map */ + private static final String AUX_REGION_FORMAT = "aux.%s.region.%s"; + /** Should we use system property substitutions. */ - private static final boolean DEFAULT_USE_SYSTEM_PROPERTIES = true; + private static boolean DEFAULT_USE_SYSTEM_PROPERTIES = true; /** Once configured, you can force a reconfiguration of sorts. */ - private static final boolean DEFAULT_FORCE_RECONFIGURATION = false; + private static boolean DEFAULT_FORCE_RECONFIGURATION = false; /** The Singleton Instance */ private static CompositeCacheManager instance; @@ -170,24 +173,22 @@ public class CompositeCacheManager } /** Caches managed by this cache manager */ - private final ConcurrentMap<String, ICache<?, ?>> caches = new ConcurrentHashMap<>(); + private final ConcurrentMap<String, ICache<?, ?>> caches; /** Number of clients accessing this cache manager */ - private final AtomicInteger clients = new AtomicInteger(); + private final AtomicInteger clients; /** Default cache attributes for this cache manager */ - private ICompositeCacheAttributes defaultCacheAttr = CompositeCacheAttributes.defaults(); + private ICompositeCacheAttributes defaultCacheAttr; /** Default element attributes for this cache manager */ - private IElementAttributes defaultElementAttr = new ElementAttributes(); + private IElementAttributes defaultElementAttr; /** Used to keep track of configured auxiliary factories */ - private final ConcurrentMap<String, AuxiliaryCacheFactory> auxiliaryFactoryRegistry = - new ConcurrentHashMap<>( ); + private final ConcurrentMap<String, AuxiliaryCacheFactory> auxiliaryFactoryRegistry; /** Used to keep track of configured auxiliaries */ - private final ConcurrentMap<String, AuxiliaryCache<?, ?>> auxiliaryCaches = - new ConcurrentHashMap<>( ); + private final ConcurrentMap<String, AuxiliaryCache<?, ?>> auxiliaryCaches; /** Properties with which this manager was configured. This is exposed for other managers. */ private Properties configurationProperties; @@ -196,7 +197,7 @@ public class CompositeCacheManager private String defaultAuxValues; /** Stack for those waiting for notification of a shutdown. */ - private final LinkedBlockingDeque<IShutdownObserver> shutdownObservers = new LinkedBlockingDeque<>(); + private final LinkedBlockingDeque<IShutdownObserver> shutdownObservers; /** The central background scheduler. */ private ScheduledExecutorService scheduledExecutor; @@ -223,7 +224,13 @@ public class CompositeCacheManager */ protected CompositeCacheManager() { - // empty + this.caches = new ConcurrentHashMap<>(); + this.clients = new AtomicInteger(); + this.defaultCacheAttr = CompositeCacheAttributes.defaults(); + this.defaultElementAttr = new ElementAttributes(); + this.auxiliaryFactoryRegistry = new ConcurrentHashMap<>(); + this.auxiliaryCaches = new ConcurrentHashMap<>(); + this.shutdownObservers = new LinkedBlockingDeque<>(); } /** @@ -235,7 +242,7 @@ public class CompositeCacheManager */ public void addAuxiliaryCache(final String auxName, final String cacheName, final AuxiliaryCache<?, ?> cache) { - final String key = String.format("aux.%s.region.%s", auxName, cacheName); + final String key = String.format(AUX_REGION_FORMAT, auxName, cacheName); auxiliaryCaches.put(key, cache); } @@ -429,7 +436,7 @@ public class CompositeCacheManager { final AuxiliaryCache<?, ?> aux = auxiliaryCaches.remove( key ); - if ( aux != null ) + if (aux != null) { aux.dispose(); } @@ -444,7 +451,7 @@ public class CompositeCacheManager */ public void freeAuxiliaryCache(final String auxName, final String cacheName) throws IOException { - final String key = String.format("aux.%s.region.%s", auxName, cacheName); + final String key = String.format(AUX_REGION_FORMAT, auxName, cacheName); freeAuxiliaryCache(key); } @@ -481,7 +488,7 @@ public class CompositeCacheManager @SuppressWarnings("unchecked") // because of common map for all auxiliary caches public <K, V> AuxiliaryCache<K, V> getAuxiliaryCache(final String auxName, final String cacheName) { - final String key = String.format("aux.%s.region.%s", auxName, cacheName); + final String key = String.format(AUX_REGION_FORMAT, auxName, cacheName); return (AuxiliaryCache<K, V>) auxiliaryCaches.get(key); }
