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..06af78ec 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCache.java @@ -61,20 +61,12 @@ 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); - } + return PUT(key, value, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS); } @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); - } + return PUT_ALL(map, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS); } @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); - } + /* + 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) { + PUT_caches(i, key, h.getValue(), restTtl, TimeUnit.MILLISECONDS); } } } @@ -177,7 +172,8 @@ 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); + long useExpireTime = Math.min(expire, cache.config().getExpireAfterWriteInMillis()); + r = cache.PUT(key, value, useExpireTime, timeUnit); } 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..702f713c 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheBuilder.java @@ -40,11 +40,13 @@ public void setCaches(List caches) { getConfig().setCaches(caches); } + @Deprecated public T useExpireOfSubCache(boolean useExpireOfSubCache) { getConfig().setUseExpireOfSubCache(useExpireOfSubCache); return self(); } + @Deprecated public void setUseExpireOfSubCache(boolean useExpireOfSubCache) { getConfig().setUseExpireOfSubCache(useExpireOfSubCache); } @@ -69,4 +71,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..9244743e 100644 --- a/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java +++ b/jetcache-core/src/main/java/com/alicp/jetcache/MultiLevelCacheConfig.java @@ -29,10 +29,12 @@ public void setCaches(List> caches) { this.caches = caches; } + @Deprecated public boolean isUseExpireOfSubCache() { return useExpireOfSubCache; } + @Deprecated 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());