From a2aec02e73b7d402674402a1c63cc0e38b11de48 Mon Sep 17 00:00:00 2001 From: tangcent Date: Thu, 4 Feb 2021 11:45:27 +0800 Subject: [PATCH] move the check of timeToLiveMillis out of the loop --- .../cache/support/expiring/ExpiringMap.java | 6 ++-- .../expiring/ExpiringCacheFactoryTest.java | 31 +++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/expiring/ExpiringMap.java b/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/expiring/ExpiringMap.java index 895f11408c0..2758bedf155 100644 --- a/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/expiring/ExpiringMap.java +++ b/dubbo-filter/dubbo-filter-cache/src/main/java/org/apache/dubbo/cache/support/expiring/ExpiringMap.java @@ -282,10 +282,10 @@ public void run() { private void processExpires() { long timeNow = System.currentTimeMillis(); + if (timeToLiveMillis <= 0) { + return; + } for (ExpiryObject o : delegateMap.values()) { - if (timeToLiveMillis <= 0) { - continue; - } long timeIdle = timeNow - o.getLastAccessTime(); if (timeIdle >= timeToLiveMillis) { delegateMap.remove(o.getKey()); diff --git a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactoryTest.java b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactoryTest.java index 23484c6c327..877faf4970c 100644 --- a/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactoryTest.java +++ b/dubbo-filter/dubbo-filter-cache/src/test/java/org/apache/dubbo/cache/support/expiring/ExpiringCacheFactoryTest.java @@ -19,18 +19,45 @@ import org.apache.dubbo.cache.Cache; import org.apache.dubbo.cache.support.AbstractCacheFactory; import org.apache.dubbo.cache.support.AbstractCacheFactoryTest; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.RpcInvocation; import org.junit.jupiter.api.Test; -import static org.hamcrest.core.Is.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; public class ExpiringCacheFactoryTest extends AbstractCacheFactoryTest { @Test - public void testLruCacheFactory() throws Exception { + public void testExpiringCacheFactory() throws Exception { Cache cache = super.constructCache(); assertThat(cache instanceof ExpiringCache, is(true)); } + @Test + public void testExpiringCacheGetExpired() throws Exception { + URL url = URL.valueOf("test://test:12/test?cache=expiring&cache.seconds=1&cache.interval=1"); + AbstractCacheFactory cacheFactory = getCacheFactory(); + Invocation invocation = new RpcInvocation(); + Cache cache = cacheFactory.getCache(url, invocation); + cache.put("testKey", "testValue"); + Thread.sleep(2100); + assertNull(cache.get("testKey")); + } + + @Test + public void testExpiringCacheUnExpired() throws Exception { + URL url = URL.valueOf("test://test:12/test?cache=expiring&cache.seconds=0&cache.interval=1"); + AbstractCacheFactory cacheFactory = getCacheFactory(); + Invocation invocation = new RpcInvocation(); + Cache cache = cacheFactory.getCache(url, invocation); + cache.put("testKey", "testValue"); + Thread.sleep(1100); + assertNotNull(cache.get("testKey")); + } + @Override protected AbstractCacheFactory getCacheFactory() { return new ExpiringCacheFactory();