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 41686f4eaf1e3d99c6e9fafd8a99624a1ad14408 Author: Thomas Vandahl <[email protected]> AuthorDate: Sat Sep 5 12:21:09 2026 +0200 Move misc code out of the lock --- .../jcs4/engine/memory/AbstractMemoryCache.java | 48 +++++++++++++--------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/memory/AbstractMemoryCache.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/memory/AbstractMemoryCache.java index 8b871784..8d6a75fb 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/memory/AbstractMemoryCache.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/memory/AbstractMemoryCache.java @@ -162,8 +162,7 @@ public abstract class AbstractMemoryCache<K, V> { ICacheElement<K, V> ce = null; - log.debug("{0}: getting item for key {1}", this::getCacheName, - () -> key); + log.debug("{0}: getting item for key {1}", this::getCacheName, () -> key); lock.writeLock().lock(); try @@ -172,19 +171,8 @@ public abstract class AbstractMemoryCache<K, V> if (me != null) { - hitCnt.incrementAndGet(); lockedGetElement(me); ce = me.getCacheElement(); - - log.debug("{0}: MemoryCache hit for {1}", this::getCacheName, - () -> key); - } - else - { - missCnt.incrementAndGet(); - - log.debug("{0}: MemoryCache miss for {1}", this::getCacheName, - () -> key); } } finally @@ -192,6 +180,17 @@ public abstract class AbstractMemoryCache<K, V> lock.writeLock().unlock(); } + if (ce == null) + { + missCnt.incrementAndGet(); + log.debug("{0}: MemoryCache miss for {1}", this::getCacheName, () -> key); + } + else + { + hitCnt.incrementAndGet(); + log.debug("{0}: MemoryCache hit for {1}", this::getCacheName, () -> key); + } + return ce; } @@ -270,18 +269,27 @@ public abstract class AbstractMemoryCache<K, V> { ICacheElement<K, V> ce = null; - final MemoryElementDescriptor<K, V> me = map.get( key ); - if ( me != null ) + lock.readLock().lock(); + try + { + final MemoryElementDescriptor<K, V> me = map.get( key ); + if ( me != null ) + { + ce = me.getCacheElement(); + } + } + finally { - log.debug( "{0}: MemoryCache quiet hit for {1}", - this::getCacheName, () -> key ); + lock.readLock().unlock(); + } - ce = me.getCacheElement(); + if (ce == null) + { + log.debug("{0}: MemoryCache quiet miss for {1}", this::getCacheName, () -> key); } else { - log.debug( "{0}: MemoryCache quiet miss for {1}", - this::getCacheName, () -> key ); + log.debug("{0}: MemoryCache quiet hit for {1}", this::getCacheName, () -> key); } return ce;
