From b56f518b9085a0bbb11e8711ddc5811575d35dd9 Mon Sep 17 00:00:00 2001 From: JingZhang Chen Date: Thu, 22 Aug 2024 16:44:59 +0800 Subject: [PATCH 1/5] fix: expiration time did not expect environment #915 --- .../com/alicp/jetcache/MultiLevelCache.java | 44 +++++++++---------- .../jetcache/MultiLevelCacheBuilder.java | 11 +---- .../alicp/jetcache/MultiLevelCacheConfig.java | 9 ---- .../alicp/jetcache/SimpleCacheManager.java | 2 - .../alicp/jetcache/MultiLevelCacheTest.java | 2 - .../jetcache/anno/filed/CreateCacheTest.java | 2 - 6 files changed, 23 insertions(+), 47 deletions(-) diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java index fb870318..ffdf4e37 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java @@ -15,7 +15,6 @@ * @author huangli */ public class MultiLevelCache extends AbstractCache { - private Cache[] caches; private MultiLevelCacheConfig config; @@ -28,7 +27,6 @@ public MultiLevelCache(Cache... caches) throws CacheConfigException { CacheConfig lastConfig = caches[caches.length - 1].config(); config = new MultiLevelCacheConfig<>(); config.setCaches(Arrays.asList(caches)); - config.setExpireAfterWriteInMillis(lastConfig.getExpireAfterWriteInMillis()); config.setCacheNullValue(lastConfig.isCacheNullValue()); } @@ -61,20 +59,14 @@ public MultiLevelCacheConfig config() { @Override public CacheResult PUT(K key, V value) { - if (config.isUseExpireOfSubCache()) { - return PUT(key, value, 0, null); - } else { - return PUT(key, value, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS); - } + // let each level cache decide the expiration time + return PUT(key, value, 0, null); } @Override public CacheResult PUT_ALL(Map map) { - if (config.isUseExpireOfSubCache()) { - return PUT_ALL(map, 0, null); - } else { - return PUT_ALL(map, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS); - } + // let each level cache decide the expiration time + return PUT_ALL(map, 0, null); } @Override @@ -109,13 +101,16 @@ private void checkResultAndFillUpperCache(K key, int i, CacheValueHolder h) { long currentExpire = h.getExpireTime(); long now = System.currentTimeMillis(); if (now <= currentExpire) { - if(config.isUseExpireOfSubCache()){ - PUT_caches(i, key, h.getValue(), 0, null); - } else { - long restTtl = currentExpire - now; - if (restTtl > 0) { - PUT_caches(i, key, h.getValue(), restTtl, TimeUnit.MILLISECONDS); - } + /* + 原来这里有两种情况: + - 如果 isUseExpireOfSubCache,那么让 Local 自己选择过期时间 + - 如果为 false,那么选择远程的剩余时间作为过期时间 + 现在的代码,总是选择远程的剩余时间作为过期时间,但是增加了参数 isForce 来控制不能超过配置的本地缓存时间 + 而对于用户直接 PUT_EXPIRED 则允许此类操作 + */ + long restTtl = currentExpire - now; + if (restTtl > 0) { + PUT_caches(i, key, h.getValue(), restTtl, TimeUnit.MILLISECONDS,false); } } } @@ -151,7 +146,7 @@ protected MultiGetResult do_GET_ALL(Set keys) { @Override protected CacheResult do_PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) { - return PUT_caches(caches.length, key, value, expireAfterWrite, timeUnit); + return PUT_caches(caches.length, key, value, expireAfterWrite, timeUnit, true); } @Override @@ -169,7 +164,7 @@ protected CacheResult do_PUT_ALL(Map map, long expireA return new CacheResult(future); } - private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeUnit timeUnit) { + private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeUnit timeUnit, boolean isForce) { CompletableFuture future = CompletableFuture.completedFuture(null); for (int i = 0; i < lastIndex; i++) { Cache cache = caches[i]; @@ -177,7 +172,12 @@ private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeU if (timeUnit == null) { r = cache.PUT(key, value); } else { - r = cache.PUT(key, value, expire, timeUnit); + // 只有 isForce = 用户强制 && expire <= 本地缓存时间 才会使用参数的 expire + if (isForce && expire <= cache.config().getExpireAfterWriteInMillis()) { + r = cache.PUT(key, value, expire, timeUnit); + } else { + r = cache.PUT(key, value); + } } future = combine(future, r); } diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java index 2006b511..440ab692 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java @@ -40,15 +40,6 @@ public void setCaches(List caches) { getConfig().setCaches(caches); } - public T useExpireOfSubCache(boolean useExpireOfSubCache) { - getConfig().setUseExpireOfSubCache(useExpireOfSubCache); - return self(); - } - - public void setUseExpireOfSubCache(boolean useExpireOfSubCache) { - getConfig().setUseExpireOfSubCache(useExpireOfSubCache); - } - @Override public T keyConvertor(Function keyConvertor) { throw new UnsupportedOperationException("MultiLevelCache do not need a key convertor"); @@ -69,4 +60,4 @@ public void setExpireAfterAccessInMillis(long expireAfterAccessInMillis) { throw new UnsupportedOperationException("MultiLevelCache do not support expireAfterAccess"); } -} \ No newline at end of file +} diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java index 2d63646c..0c359eb8 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java @@ -10,7 +10,6 @@ */ public class MultiLevelCacheConfig extends CacheConfig { private List> caches = new ArrayList<>(); - private boolean useExpireOfSubCache; @Override public MultiLevelCacheConfig clone() { @@ -28,12 +27,4 @@ public List> getCaches() { public void setCaches(List> caches) { this.caches = caches; } - - public boolean isUseExpireOfSubCache() { - return useExpireOfSubCache; - } - - public void setUseExpireOfSubCache(boolean useExpireOfSubCache) { - this.useExpireOfSubCache = useExpireOfSubCache; - } } diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/SimpleCacheManager.java b/jetcache-core/src/main/java/com/alicp/jetcache/SimpleCacheManager.java index 5185db55..ad133c76 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/SimpleCacheManager.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/SimpleCacheManager.java @@ -119,11 +119,9 @@ private Cache create(QuickConfig config) { Cache remote = buildRemote(config); - boolean useExpireOfSubCache = config.getLocalExpire() != null; cache = MultiLevelCacheBuilder.createMultiLevelCacheBuilder() .expireAfterWrite(remote.config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS) .addCache(local, remote) - .useExpireOfSubCache(useExpireOfSubCache) .cacheNullValue(config.getCacheNullValue() != null ? config.getCacheNullValue() : DEFAULT_CACHE_NULL_VALUE) .buildCache(); diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/MultiLevelCacheTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/MultiLevelCacheTest.java index e4bb830f..52eeac0b 100644 --- a/jetcache-test/src/test/java/com/alicp/jetcache/MultiLevelCacheTest.java +++ b/jetcache-test/src/test/java/com/alicp/jetcache/MultiLevelCacheTest.java @@ -169,7 +169,6 @@ private void doTest(int expireMillis) throws Exception { private void testUseExpireOfSubCache() throws Exception { long oldExpire = l1Cache.config().getExpireAfterWriteInMillis(); - ((MultiLevelCacheConfig)cache.config()).setUseExpireOfSubCache(true); l1Cache.config().setExpireAfterWriteInMillis(15); cache.put("useSubExpire_key", "V1"); @@ -194,7 +193,6 @@ private void testUseExpireOfSubCache() throws Exception { Assert.assertNull(l1Cache.get("useSubExpire_key")); cache.remove("useSubExpire_key"); - ((MultiLevelCacheConfig)cache.config()).setUseExpireOfSubCache(false); l1Cache.config().setExpireAfterAccessInMillis(oldExpire); } diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/filed/CreateCacheTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/filed/CreateCacheTest.java index a0b3fc1e..b8576158 100644 --- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/filed/CreateCacheTest.java +++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/filed/CreateCacheTest.java @@ -189,13 +189,11 @@ public void test() throws Exception { private void testCacheWithLocalExpire() { MultiLevelCacheConfig config = (MultiLevelCacheConfig) cacheWithLocalExpire_1.config(); - Assert.assertTrue(config.isUseExpireOfSubCache()); Assert.assertEquals(2000, config.getExpireAfterWriteInMillis()); Assert.assertEquals(1000, config.getCaches().get(0).config().getExpireAfterWriteInMillis()); Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis()); config = (MultiLevelCacheConfig) cacheWithLocalExpire_2.config(); - Assert.assertFalse(config.isUseExpireOfSubCache()); Assert.assertEquals(2000, config.getExpireAfterWriteInMillis()); Assert.assertEquals(2000, config.getCaches().get(0).config().getExpireAfterWriteInMillis()); Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis()); From fb1e4c7cfcec600856811978ddaabeed24df145f Mon Sep 17 00:00:00 2001 From: JingZhang Chen Date: Thu, 22 Aug 2024 17:39:26 +0800 Subject: [PATCH 2/5] fix condition mistake --- .../src/main/java/com/alicp/jetcache/MultiLevelCache.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java index ffdf4e37..4e12eb61 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java @@ -172,8 +172,8 @@ private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeU if (timeUnit == null) { r = cache.PUT(key, value); } else { - // 只有 isForce = 用户强制 && expire <= 本地缓存时间 才会使用参数的 expire - if (isForce && expire <= cache.config().getExpireAfterWriteInMillis()) { + // 只有 isForce = 用户强制 或者 expire <= 本地缓存时间 才会使用参数的 expire + if (isForce || expire <= cache.config().getExpireAfterWriteInMillis()) { r = cache.PUT(key, value, expire, timeUnit); } else { r = cache.PUT(key, value); From 687a0f64a5f8f646a7a2491fd3150415ed890803 Mon Sep 17 00:00:00 2001 From: JingZhang Chen Date: Thu, 22 Aug 2024 17:57:19 +0800 Subject: [PATCH 3/5] add deprecated and translate comment --- .../java/com/alicp/jetcache/MultiLevelCache.java | 13 +++++++------ .../com/alicp/jetcache/MultiLevelCacheBuilder.java | 9 +++++++++ .../com/alicp/jetcache/MultiLevelCacheConfig.java | 11 +++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java index 4e12eb61..72cfece3 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java @@ -15,6 +15,7 @@ * @author huangli */ public class MultiLevelCache extends AbstractCache { + private Cache[] caches; private MultiLevelCacheConfig config; @@ -102,11 +103,11 @@ private void checkResultAndFillUpperCache(K key, int i, CacheValueHolder h) { long now = System.currentTimeMillis(); if (now <= currentExpire) { /* - 原来这里有两种情况: - - 如果 isUseExpireOfSubCache,那么让 Local 自己选择过期时间 - - 如果为 false,那么选择远程的剩余时间作为过期时间 - 现在的代码,总是选择远程的剩余时间作为过期时间,但是增加了参数 isForce 来控制不能超过配置的本地缓存时间 - 而对于用户直接 PUT_EXPIRED 则允许此类操作 + There are two situations here from the legacy version: + - if isUseExpireOfSubCache, let subordinate cache choose the expiration time by itself + - if not, choose the remaining time of the current cache as the expiration time + In the current version, the remaining time of the current cache is always chosen as the expiration time, + but a parameter isForce is added to control that it cannot exceed the expiration time that subordinate cache configured */ long restTtl = currentExpire - now; if (restTtl > 0) { @@ -172,7 +173,7 @@ private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeU if (timeUnit == null) { r = cache.PUT(key, value); } else { - // 只有 isForce = 用户强制 或者 expire <= 本地缓存时间 才会使用参数的 expire + // only use argument expiration time when user force and expiration minus than configuration if (isForce || expire <= cache.config().getExpireAfterWriteInMillis()) { r = cache.PUT(key, value, expire, timeUnit); } else { diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java index 440ab692..abd0faf5 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java @@ -40,6 +40,15 @@ public void setCaches(List caches) { getConfig().setCaches(caches); } + @Deprecated + public T useExpireOfSubCache(boolean useExpireOfSubCache) { + return self(); + } + + @Deprecated + public void setUseExpireOfSubCache(boolean useExpireOfSubCache) { + } + @Override public T keyConvertor(Function keyConvertor) { throw new UnsupportedOperationException("MultiLevelCache do not need a key convertor"); diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java index 0c359eb8..9244743e 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java @@ -10,6 +10,7 @@ */ public class MultiLevelCacheConfig extends CacheConfig { private List> caches = new ArrayList<>(); + private boolean useExpireOfSubCache; @Override public MultiLevelCacheConfig clone() { @@ -27,4 +28,14 @@ public List> getCaches() { public void setCaches(List> caches) { this.caches = caches; } + + @Deprecated + public boolean isUseExpireOfSubCache() { + return useExpireOfSubCache; + } + + @Deprecated + public void setUseExpireOfSubCache(boolean useExpireOfSubCache) { + this.useExpireOfSubCache = useExpireOfSubCache; + } } From fb305d63f70d39c53941bfe6d695262d854b3acc Mon Sep 17 00:00:00 2001 From: JingZhang Chen Date: Thu, 22 Aug 2024 17:58:07 +0800 Subject: [PATCH 4/5] rollback method --- .../main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java index abd0faf5..702f713c 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java @@ -42,11 +42,13 @@ public void setCaches(List caches) { @Deprecated public T useExpireOfSubCache(boolean useExpireOfSubCache) { + getConfig().setUseExpireOfSubCache(useExpireOfSubCache); return self(); } @Deprecated public void setUseExpireOfSubCache(boolean useExpireOfSubCache) { + getConfig().setUseExpireOfSubCache(useExpireOfSubCache); } @Override From e2264219df0947a541724befecc4392376cbb34c Mon Sep 17 00:00:00 2001 From: JingZhang Chen Date: Wed, 4 Sep 2024 14:29:07 +0800 Subject: [PATCH 5/5] remove force argument --- .../com/alicp/jetcache/MultiLevelCache.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java index 72cfece3..06af78ec 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java @@ -28,6 +28,7 @@ public MultiLevelCache(Cache... caches) throws CacheConfigException { CacheConfig lastConfig = caches[caches.length - 1].config(); config = new MultiLevelCacheConfig<>(); config.setCaches(Arrays.asList(caches)); + config.setExpireAfterWriteInMillis(lastConfig.getExpireAfterWriteInMillis()); config.setCacheNullValue(lastConfig.isCacheNullValue()); } @@ -60,14 +61,12 @@ public MultiLevelCacheConfig config() { @Override public CacheResult PUT(K key, V value) { - // let each level cache decide the expiration time - return PUT(key, value, 0, null); + return PUT(key, value, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS); } @Override public CacheResult PUT_ALL(Map map) { - // let each level cache decide the expiration time - return PUT_ALL(map, 0, null); + return PUT_ALL(map, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS); } @Override @@ -111,7 +110,7 @@ private void checkResultAndFillUpperCache(K key, int i, CacheValueHolder h) { */ long restTtl = currentExpire - now; if (restTtl > 0) { - PUT_caches(i, key, h.getValue(), restTtl, TimeUnit.MILLISECONDS,false); + PUT_caches(i, key, h.getValue(), restTtl, TimeUnit.MILLISECONDS); } } } @@ -147,7 +146,7 @@ protected MultiGetResult do_GET_ALL(Set keys) { @Override protected CacheResult do_PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) { - return PUT_caches(caches.length, key, value, expireAfterWrite, timeUnit, true); + return PUT_caches(caches.length, key, value, expireAfterWrite, timeUnit); } @Override @@ -165,7 +164,7 @@ protected CacheResult do_PUT_ALL(Map map, long expireA return new CacheResult(future); } - private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeUnit timeUnit, boolean isForce) { + private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeUnit timeUnit) { CompletableFuture future = CompletableFuture.completedFuture(null); for (int i = 0; i < lastIndex; i++) { Cache cache = caches[i]; @@ -173,12 +172,8 @@ private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeU if (timeUnit == null) { r = cache.PUT(key, value); } else { - // only use argument expiration time when user force and expiration minus than configuration - if (isForce || expire <= cache.config().getExpireAfterWriteInMillis()) { - r = cache.PUT(key, value, expire, timeUnit); - } else { - r = cache.PUT(key, value); - } + long useExpireTime = Math.min(expire, cache.config().getExpireAfterWriteInMillis()); + r = cache.PUT(key, value, useExpireTime, timeUnit); } future = combine(future, r); }