This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit b5cf590973fedb97dca97abb63f3597cb5b89840 Author: Thomas Vandahl <[email protected]> AuthorDate: Wed Mar 18 18:33:34 2026 +0100 Use java.time.Instant instead of Unix time --- .../apache/commons/jcs4/admin/JCSAdminBean.java | 13 ++--- .../jcs4/auxiliary/disk/jdbc/JDBCDiskCache.java | 4 +- .../commons/jcs4/engine/ElementAttributes.java | 55 ++++++++++++++-------- .../jcs4/engine/behavior/IElementAttributes.java | 16 +++++-- .../jcs4/engine/control/CompositeCache.java | 17 +++---- .../engine/memory/shrinking/ShrinkerThread.java | 5 +- .../jcs4/JCSCacheElementRetrievalUnitTest.java | 9 ++-- .../indexed/AbstractIndexDiskCacheUnitTest.java | 9 ++-- .../disk/indexed/IndexDiskCacheSizeUnitTest.java | 16 ++----- .../IndexedDiskCacheConcurrentUnitTest.java | 1 - .../commons/jcs4/engine/TestElementAttributes.java | 17 ++++--- .../control/event/SimpleEventHandlingUnitTest.java | 10 ++-- .../memory/shrinking/ShrinkerThreadUnitTest.java | 23 ++++----- .../commons/jcs4/jcache/JCSCachingManager.java | 3 -- 14 files changed, 110 insertions(+), 88 deletions(-) diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/admin/JCSAdminBean.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/admin/JCSAdminBean.java index e32a5a60..2d16b720 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/admin/JCSAdminBean.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/admin/JCSAdminBean.java @@ -22,8 +22,8 @@ package org.apache.commons.jcs4.admin; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.text.DateFormat; -import java.util.Date; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -132,10 +132,7 @@ public class JCSAdminBean implements JCSJMXBean .collect(Collectors.toMap(Object::toString, k -> k))); final LinkedList<CacheElementInfo> records = new LinkedList<>(); - - final DateFormat format = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT ); - - final long now = System.currentTimeMillis(); + final DateTimeFormatter format = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault()); for (final Map.Entry<String, ?> key : keys.entrySet()) { @@ -145,9 +142,9 @@ public class JCSAdminBean implements JCSJMXBean final CacheElementInfo elementInfo = new CacheElementInfo( key.getKey(), attributes.IsEternal(), - format.format(new Date(attributes.createTime())), + format.format(attributes.createTime()), attributes.MaxLife(), - (now - attributes.createTime() - attributes.MaxLife() * 1000 ) / -1000); + attributes.getTimeToLive().toSeconds()); records.add( elementInfo ); } 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 731b6aa5..1d3a07af 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 @@ -430,7 +430,7 @@ public class JDBCDiskCache<K, V> psInsert.setLong( 4, ce.elementAttributes().MaxLife() ); psInsert.setString( 5, ce.elementAttributes().IsEternal() ? "T" : "F" ); - final Timestamp createTime = new Timestamp( ce.elementAttributes().createTime() ); + final Timestamp createTime = Timestamp.from(ce.elementAttributes().createTime()); psInsert.setTimestamp( 6, createTime ); final long now = System.currentTimeMillis() / 1000; @@ -477,7 +477,7 @@ public class JDBCDiskCache<K, V> { psUpdate.setBytes( 1, element ); - final Timestamp createTime = new Timestamp( ce.elementAttributes().createTime() ); + final Timestamp createTime = Timestamp.from(ce.elementAttributes().createTime()); psUpdate.setTimestamp( 2, createTime ); final long now = System.currentTimeMillis() / 1000; diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/ElementAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/ElementAttributes.java index d593dc3e..ae4f8b82 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/ElementAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/ElementAttributes.java @@ -19,8 +19,10 @@ package org.apache.commons.jcs4.engine; * under the License. */ +import java.io.Serializable; +import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicLong; import org.apache.commons.jcs4.engine.behavior.IElementAttributes; import org.apache.commons.jcs4.engine.control.event.behavior.IElementEventHandler; @@ -63,10 +65,10 @@ public record ElementAttributes( long MaxIdleTime, /** The creation time. This is used to enforce the max life. */ - long createTime, + Instant createTime, /** The last access time. This is used to enforce the max idle time. */ - AtomicLong mutableLastAccessTime, + LastAccessHolder mutableLastAccessTime, /** The time factor to convert durations to milliseconds */ long timeFactorForMilliseconds, @@ -84,6 +86,20 @@ public record ElementAttributes( /** Don't change. */ private static final long serialVersionUID = 7814990748035017441L; + public static final class LastAccessHolder implements Serializable + { + private static final long serialVersionUID = 3995308126685776408L; + public Instant lastAccessTime; + + /** + * @param lastAccessTime + */ + public LastAccessHolder(Instant lastAccessTime) + { + this.lastAccessTime = lastAccessTime; + } + } + /** Default */ private static final boolean DEFAULT_IS_SPOOL = true; /** Default */ @@ -107,8 +123,8 @@ public record ElementAttributes( DEFAULT_IS_ETERNAL, DEFAULT_MAX_LIFE, DEFAULT_MAX_IDLE_TIME, - 0, - new AtomicLong(), + Instant.EPOCH, + new LastAccessHolder(Instant.EPOCH), DEFAULT_TIME_FACTOR, new ArrayList<>()); @@ -126,7 +142,7 @@ public record ElementAttributes( public ElementAttributes() { this(defaults()); - this.mutableLastAccessTime.set(createTime()); + this.mutableLastAccessTime.lastAccessTime = createTime(); } /** @@ -140,8 +156,8 @@ public record ElementAttributes( from.IsEternal(), from.MaxLife(), from.MaxIdleTime(), - System.currentTimeMillis(), - new AtomicLong(from.lastAccessTime()), + Instant.now(), + new LastAccessHolder(from.lastAccessTime()), from.timeFactorForMilliseconds(), new ArrayList<>(from.elementEventHandlers())); } @@ -160,10 +176,10 @@ public record ElementAttributes( ) { this(isSpool, isLateral, isRemote, isEternal, maxLife, maxIdleTime, - System.currentTimeMillis(), new AtomicLong(), timeFactorForMilliseconds, + Instant.now(), new LastAccessHolder(Instant.EPOCH), timeFactorForMilliseconds, new ArrayList<>()); - this.mutableLastAccessTime.set(createTime()); + this.mutableLastAccessTime.lastAccessTime = createTime(); } /** @@ -187,9 +203,9 @@ public record ElementAttributes( * @return The LastAccess value. */ @Override - public long lastAccessTime() + public Instant lastAccessTime() { - return mutableLastAccessTime().get(); + return mutableLastAccessTime().lastAccessTime; } /** @@ -198,19 +214,20 @@ public record ElementAttributes( @Override public void setLastAccessTimeNow() { - this.mutableLastAccessTime.set(System.currentTimeMillis()); + this.mutableLastAccessTime.lastAccessTime = Instant.now(); } /** * Gets the time left to live of the IElementAttributes object. * <p> * This is the (max life + create time) - current time. - * @return The TimeToLiveSeconds value + * @return The TimeToLive value */ - private long getTimeToLiveSeconds() + @Override + public Duration getTimeToLive() { - final long now = System.currentTimeMillis(); - return ( createTime() + MaxLife() * timeFactorForMilliseconds() - now ) / 1000; + final Instant now = Instant.now(); + return Duration.between(now, createTime().plusMillis(MaxLife() * timeFactorForMilliseconds())); } /** @@ -227,11 +244,11 @@ public record ElementAttributes( dump.append( ", isSpool = " ).append( IsSpool() ); dump.append( ", isRemote = " ).append( IsRemote() ); dump.append( ", isEternal = " ).append( IsEternal() ); - dump.append( ", MaxLifeSeconds = " ).append( MaxLife() ); + dump.append( ", MaxLife = " ).append( MaxLife() ); dump.append( ", MaxIdleTime = " ).append( MaxIdleTime() ); dump.append( ", CreateTime = " ).append( createTime() ); dump.append( ", LastAccessTime = " ).append( lastAccessTime() ); - dump.append( ", getTimeToLiveSeconds() = " ).append(getTimeToLiveSeconds()); + dump.append( ", getTimeToLive() = " ).append(getTimeToLive()); dump.append( " ]" ); return dump.toString(); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/IElementAttributes.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/IElementAttributes.java index 317ea27d..43ffeee9 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/IElementAttributes.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/behavior/IElementAttributes.java @@ -20,6 +20,8 @@ package org.apache.commons.jcs4.engine.behavior; */ import java.io.Serializable; +import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import org.apache.commons.jcs4.engine.control.event.behavior.IElementEventHandler; @@ -50,13 +52,13 @@ public interface IElementAttributes extends Serializable /** * Gets the createTime attribute of the IAttributes object. * <p> - * This should be the current time in milliseconds returned by the sysutem call when the element + * This should be the current time returned by the system call when the element * is put in the cache. * <p> * Putting an item in the cache overrides any existing items. * @return The createTime value */ - long createTime(); + Instant createTime(); /** * Gets the idleTime attribute of the IAttributes object @@ -99,7 +101,7 @@ public interface IElementAttributes extends Serializable * * @return The LastAccess value. */ - long lastAccessTime(); + Instant lastAccessTime(); /** * Sets the MaxLife attribute of the IAttributes object. How many seconds it can live after @@ -121,4 +123,12 @@ public interface IElementAttributes extends Serializable * Sets the LastAccessTime as now of the IElementAttributes object */ void setLastAccessTimeNow(); + + /** + * Gets the time left to live of the IElementAttributes object. + * <p> + * This is the (max life + create time) - current time. + * @return The TimeToLive value + */ + public Duration getTimeToLive(); } 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 6a947864..2d419f33 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 @@ -20,6 +20,7 @@ package org.apache.commons.jcs4.engine.control; */ import java.io.IOException; +import java.time.Instant; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -973,7 +974,7 @@ public class CompositeCache<K, V> */ public boolean isExpired(final ICacheElement<K, V> element) { - return isExpired(element, System.currentTimeMillis(), + return isExpired(element, Instant.now(), ElementEventType.EXCEEDED_MAXLIFE_ONREQUEST, ElementEventType.EXCEEDED_IDLETIME_ONREQUEST); } @@ -987,7 +988,7 @@ public class CompositeCache<K, V> * @param eventIdle the event to fire in case the idle time is exceeded * @return true if the element is expired */ - public boolean isExpired(final ICacheElement<K, V> element, final long timestamp, + public boolean isExpired(final ICacheElement<K, V> element, final Instant timestamp, final ElementEventType eventMaxlife, final ElementEventType eventIdle) { try @@ -996,12 +997,12 @@ public class CompositeCache<K, V> if (!attributes.IsEternal()) { - // Remove if maxLifeSeconds exceeded - final long maxLifeSeconds = attributes.MaxLife(); - final long createTime = attributes.createTime(); + // Remove if maxLife exceeded + final long maxLife = attributes.MaxLife(); + final Instant createTime = attributes.createTime(); final long timeFactorForMilliseconds = attributes.timeFactorForMilliseconds(); - if (maxLifeSeconds != -1 && timestamp - createTime > maxLifeSeconds * timeFactorForMilliseconds) + if (maxLife != -1 && timestamp.isAfter(createTime.plusMillis(maxLife * timeFactorForMilliseconds))) { log.debug("Exceeded maxLife: {0}", element::key); @@ -1009,13 +1010,13 @@ public class CompositeCache<K, V> return true; } final long idleTime = attributes.MaxIdleTime(); - final long lastAccessTime = attributes.lastAccessTime(); + final Instant lastAccessTime = attributes.lastAccessTime(); // Remove if maxIdleTime exceeded // If you have a 0 size memory cache, then the last access will // not get updated. // you will need to set the idle time to -1. - if (idleTime != -1 && timestamp - lastAccessTime > idleTime * timeFactorForMilliseconds) + if (idleTime != -1 && timestamp.isAfter(lastAccessTime.plusMillis(idleTime * timeFactorForMilliseconds))) { log.debug("Exceeded maxIdle: {0}", element::key); diff --git a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/memory/shrinking/ShrinkerThread.java b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/memory/shrinking/ShrinkerThread.java index e895c479..83a2ca85 100644 --- a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/memory/shrinking/ShrinkerThread.java +++ b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/engine/memory/shrinking/ShrinkerThread.java @@ -125,7 +125,7 @@ public class ShrinkerThread<K, V> // removed and remove it if so. if ( !attributes.IsEternal() ) { - remove = cache.isExpired( cacheElement, now.toEpochMilli(), + remove = cache.isExpired( cacheElement, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); @@ -142,8 +142,7 @@ public class ShrinkerThread<K, V> { if (!spoolLimit || spoolCount < this.maxSpoolPerRun) { - final Instant idleExpiryTime = Instant - .ofEpochMilli(attributes.lastAccessTime()) + final Instant idleExpiryTime = attributes.lastAccessTime() .plus(maxMemoryIdleTime); if (idleExpiryTime.isBefore(now)) diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/JCSCacheElementRetrievalUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/JCSCacheElementRetrievalUnitTest.java index 9d418bc7..3dfeba61 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/JCSCacheElementRetrievalUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/JCSCacheElementRetrievalUnitTest.java @@ -22,6 +22,9 @@ package org.apache.commons.jcs4; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.time.Duration; +import java.time.Instant; + import org.apache.commons.jcs4.access.CacheAccess; import org.apache.commons.jcs4.engine.behavior.ICacheElement; import org.junit.jupiter.api.Test; @@ -42,12 +45,12 @@ class JCSCacheElementRetrievalUnitTest jcs.put( "test_key", "test_data" ); - final long now = System.currentTimeMillis(); + final Instant now = Instant.now(); final ICacheElement<String, String> elem = jcs.getCacheElement( "test_key" ); assertEquals( "testCache1", elem.cacheName(), "Name wasn't right" ); - final long diff = now - elem.elementAttributes().createTime(); - assertTrue( diff >= 0, "Create time should have been at or after the call" ); + final Duration diff = Duration.between(elem.elementAttributes().createTime(), now); + assertTrue( diff.toMillis() >= 0, "Create time should have been at or after the call" ); } } diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/AbstractIndexDiskCacheUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/AbstractIndexDiskCacheUnitTest.java index 0905a4b7..9af4033c 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/AbstractIndexDiskCacheUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/AbstractIndexDiskCacheUnitTest.java @@ -46,7 +46,8 @@ import org.junit.jupiter.api.Test; /** * Tests for common functionality. */ -public abstract class AbstractIndexDiskCacheUnitTest{ +public abstract class AbstractIndexDiskCacheUnitTest +{ public abstract IndexedDiskCacheAttributes getCacheAttributes(); /** @@ -567,7 +568,7 @@ public abstract class AbstractIndexDiskCacheUnitTest{ cattr.setCacheName("testRecyleBinSize"); cattr.setDiskPath("target/test-sandbox/UnitTest"); cattr.setOptimizeAtRemoveCount(numberToInsert); - cattr.setMaxKeySize(numberToInsert * 2); + cattr.setMaxKeySize(numberToInsert * 3); cattr.setMaxPurgatorySize(numberToInsert); final IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<>(cattr); @@ -613,7 +614,7 @@ public abstract class AbstractIndexDiskCacheUnitTest{ cattr.setCacheName("testRecyleBinUsage"); cattr.setDiskPath("target/test-sandbox/UnitTest"); cattr.setOptimizeAtRemoveCount(numberToInsert); - cattr.setMaxKeySize(numberToInsert * 2); + cattr.setMaxKeySize(numberToInsert * 3); cattr.setMaxPurgatorySize(numberToInsert); final IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<>(cattr); @@ -651,7 +652,7 @@ public abstract class AbstractIndexDiskCacheUnitTest{ // verify that we used the correct number of spots assertEquals( numberToAdd, disk.getRecyleCount(), - "The recycle bin should have the number removed." + disk.getStatistics() ); + "The recycle bin should have the number removed: " + disk.getStatistics() ); } /** diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexDiskCacheSizeUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexDiskCacheSizeUnitTest.java index 3c992a0a..f67db585 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexDiskCacheSizeUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexDiskCacheSizeUnitTest.java @@ -49,11 +49,12 @@ public class IndexDiskCacheSizeUnitTest extends AbstractIndexDiskCacheUnitTest final IndexedDiskCacheAttributes cattr = getCacheAttributes(); cattr.setCacheName( "testRemoveItems" ); cattr.setOptimizeAtRemoveCount(7); - // 1kb DiskTestObject takes 1420 bytes, so 5*1420 = 7100, so to keep 5 objects, we need max key size of 8 - cattr.setMaxKeySize(8); + // 1kb DiskTestObject takes 2065 bytes, so 5*2065 = 10325, so to keep 5 objects, we need max key size of 12 + cattr.setMaxKeySize(12); cattr.setMaxPurgatorySize( 0 ); cattr.setDiskPath( "target/test-sandbox/BreakIndexTest" ); final IndexedDiskCache<String, DiskTestObject> disk = new IndexedDiskCache<>( cattr ); + disk.removeAll(); // Make sure that cache is empty final String[] test = { "a", "bb", "ccc", "dddd", "eeeee", "ffffff", "ggggggg", "hhhhhhhhh", "iiiiiiiiii" }; final String[] expect = { null, "bb", "ccc", null, null, "ffffff", null, "hhhhhhhhh", "iiiiiiiiii" }; @@ -61,7 +62,7 @@ public class IndexDiskCacheSizeUnitTest extends AbstractIndexDiskCacheUnitTest for ( int i = 0; i < 6; i++ ) { - final ICacheElement<String, DiskTestObject> element = new CacheElement<>( "testRecycleBin", "key:" + test[i], value); + final ICacheElement<String, DiskTestObject> element = new CacheElement<>("testRecycleBin", "key:" + test[i], value); disk.processUpdate( element ); } @@ -85,15 +86,6 @@ public class IndexDiskCacheSizeUnitTest extends AbstractIndexDiskCacheUnitTest for ( int i = 0; i < 9; i++ ) { final ICacheElement<String, DiskTestObject> element = disk.get( "key:" + test[i]); - if ( element != null ) - { - //System.out.println( "element = " + element.getVal() ); - } - else - { - //System.out.println( "null --key:" + test[i]); - } - final String expectedValue = expect[i]; if ( expectedValue == null ) { diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCacheConcurrentUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCacheConcurrentUnitTest.java index a64fcd89..e90b037e 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCacheConcurrentUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/auxiliary/disk/indexed/IndexedDiskCacheConcurrentUnitTest.java @@ -39,7 +39,6 @@ import org.junit.jupiter.api.Test; */ public class IndexedDiskCacheConcurrentUnitTest { - private static final int items = 200; @BeforeEach diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/TestElementAttributes.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/TestElementAttributes.java index 29947fbb..37e5bf39 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/TestElementAttributes.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/TestElementAttributes.java @@ -1,5 +1,7 @@ package org.apache.commons.jcs4.engine; +import java.time.Instant; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -20,7 +22,8 @@ package org.apache.commons.jcs4.engine; */ import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicLong; + +import org.apache.commons.jcs4.engine.ElementAttributes.LastAccessHolder; /** * Allow test access to parametrized ElementAttributes objects @@ -41,12 +44,12 @@ public class TestElementAttributes false, maxLife, ElementAttributes.defaults().MaxIdleTime(), - System.currentTimeMillis(), - new AtomicLong(), + Instant.now(), + new LastAccessHolder(Instant.EPOCH), ElementAttributes.defaults().timeFactorForMilliseconds(), new ArrayList<>()); - element.mutableLastAccessTime().set(element.createTime()); + element.mutableLastAccessTime().lastAccessTime = element.createTime(); return element; } @@ -65,12 +68,12 @@ public class TestElementAttributes false, maxLife, maxIdleTime, - System.currentTimeMillis(), - new AtomicLong(), + Instant.now(), + new LastAccessHolder(Instant.EPOCH), ElementAttributes.defaults().timeFactorForMilliseconds(), new ArrayList<>()); - element.mutableLastAccessTime().set(element.createTime()); + element.mutableLastAccessTime().lastAccessTime = element.createTime(); return element; } } diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/control/event/SimpleEventHandlingUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/control/event/SimpleEventHandlingUnitTest.java index e70b5497..b3fe61e5 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/control/event/SimpleEventHandlingUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/control/event/SimpleEventHandlingUnitTest.java @@ -19,11 +19,13 @@ package org.apache.commons.jcs4.engine.control.event; * under the License. */ -import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.time.Instant; + import org.apache.commons.jcs4.JCS; import org.apache.commons.jcs4.access.CacheAccess; import org.apache.commons.jcs4.engine.ElementAttributes; @@ -166,14 +168,14 @@ class SimpleEventHandlingUnitTest throws Exception { final ElementAttributes elem1 = new ElementAttributes(); - final long ctime1 = elem1.createTime(); + final Instant ctime1 = elem1.createTime(); Thread.sleep(10); final IElementAttributes elem2 = new ElementAttributes(elem1); - final long ctime2 = elem2.createTime(); + final Instant ctime2 = elem2.createTime(); - assertFalse( ctime1 == ctime2, "Creation times should be different" ); + assertNotEquals(ctime1, ctime2, "Creation times should be different" ); } /** diff --git a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/shrinking/ShrinkerThreadUnitTest.java b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/shrinking/ShrinkerThreadUnitTest.java index 806c0277..3005ee46 100644 --- a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/shrinking/ShrinkerThreadUnitTest.java +++ b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/shrinking/ShrinkerThreadUnitTest.java @@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; import java.time.Duration; +import java.time.Instant; import org.apache.commons.jcs4.engine.CacheElement; import org.apache.commons.jcs4.engine.CompositeCacheAttributes; @@ -67,9 +68,9 @@ class ShrinkerThreadUnitTest final ICacheElement<String, String> element = new CacheElement<>( "testRegion", key, value, TestElementAttributes.withEternalFalseAndMaxLife(1)); - long now = System.currentTimeMillis(); + Instant now = Instant.now(); // add two seconds - now += 2000; + now = now.plusSeconds(2); // DO WORK final boolean result = cache.isExpired( element, now, @@ -102,9 +103,9 @@ class ShrinkerThreadUnitTest final ICacheElement<String, String> element = new CacheElement<>( "testRegion", key, value, TestElementAttributes.withEternalFalseAndMaxLifeAndMaxIdleTime(100, 1)); - long now = System.currentTimeMillis(); + Instant now = Instant.now(); // add two seconds - now += 2000; + now = now.plusSeconds(2); // DO WORK final boolean result = cache.isExpired( element, now, @@ -137,9 +138,9 @@ class ShrinkerThreadUnitTest final ICacheElement<String, String> element = new CacheElement<>( "testRegion", key, value, TestElementAttributes.withEternalFalseAndMaxLife(1)); - long now = System.currentTimeMillis(); + Instant now = Instant.now(); // subtract two seconds - now -= 2000; + now = now.minusSeconds(2); // DO WORK final boolean result = cache.isExpired( element, now, @@ -172,9 +173,9 @@ class ShrinkerThreadUnitTest final ICacheElement<String, String> element = new CacheElement<>( "testRegion", key, value, TestElementAttributes.withEternalFalseAndMaxLifeAndMaxIdleTime(100, 1)); - long now = System.currentTimeMillis(); + Instant now = Instant.now(); // subtract two seconds - now -= 2000; + now = now.minusSeconds(2); // DO WORK final boolean result = cache.isExpired( element, now, @@ -217,7 +218,7 @@ class ShrinkerThreadUnitTest assertNotNull( returnedElement1, "We should have received an element" ); // set this to 2 seconds ago. - elementAttr.mutableLastAccessTime().set(System.currentTimeMillis() - 2000); + elementAttr.mutableLastAccessTime().lastAccessTime = Instant.now().minusSeconds(2); // DO WORK final ShrinkerThread<String, String> shrinker = new ShrinkerThread<>( cache ); @@ -264,7 +265,7 @@ class ShrinkerThreadUnitTest assertNotNull( returnedElement1, "We should have received an element" ); // set this to 2 seconds ago. - elementAttr.mutableLastAccessTime().set(System.currentTimeMillis() - 2000); + elementAttr.mutableLastAccessTime().lastAccessTime = Instant.now().minusSeconds(2); } // DO WORK @@ -315,7 +316,7 @@ class ShrinkerThreadUnitTest assertNotNull( returnedElement1, "We should have received an element" ); // set this to 2 seconds ago. - elementAttr.mutableLastAccessTime().set(System.currentTimeMillis() - 2000); + elementAttr.mutableLastAccessTime().lastAccessTime = Instant.now().minusSeconds(2); } // DO WORK diff --git a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCachingManager.java b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCachingManager.java index b7f458ec..317ee426 100644 --- a/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCachingManager.java +++ b/commons-jcs4-jcache/src/main/java/org/apache/commons/jcs4/jcache/JCSCachingManager.java @@ -78,13 +78,10 @@ public class JCSCachingManager implements CacheManager private static final String DEFAULT_CONFIG = """ jcs.default=DC - jcs.default.cacheattributes=org.apache.commons.jcs4.engine.CompositeCacheAttributes jcs.default.cacheattributes.MaxObjects=200001 - jcs.default.cacheattributes.MemoryCacheName=org.apache.commons.jcs4.engine.memory.lru.LRUMemoryCache jcs.default.cacheattributes.UseMemoryShrinker=true jcs.default.cacheattributes.MaxMemoryIdleTime=PT1h jcs.default.cacheattributes.ShrinkerInterval=PT1m - jcs.default.elementattributes=org.apache.commons.jcs4.engine.ElementAttributes jcs.default.elementattributes.IsEternal=false jcs.default.elementattributes.MaxLife=700 jcs.default.elementattributes.MaxIdleTime=1800
