From 0b35392b1ab1960dfce7fbc9c04104ae871f47c1 Mon Sep 17 00:00:00 2001 From: johnniang Date: Mon, 29 Apr 2019 17:01:33 +0800 Subject: [PATCH] Fix cache expiry calculation error --- .../halo/app/cache/AbstractCacheStore.java | 13 ++--------- .../halo/app/cache/InMemoryCacheStore.java | 2 +- .../java/run/halo/app/utils/TimeUnitTest.java | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 src/test/java/run/halo/app/utils/TimeUnitTest.java diff --git a/src/main/java/run/halo/app/cache/AbstractCacheStore.java b/src/main/java/run/halo/app/cache/AbstractCacheStore.java index 8aba5a000..5c4939a3c 100644 --- a/src/main/java/run/halo/app/cache/AbstractCacheStore.java +++ b/src/main/java/run/halo/app/cache/AbstractCacheStore.java @@ -1,10 +1,10 @@ package run.halo.app.cache; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.time.DateUtils; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import run.halo.app.utils.DateUtils; import java.util.Date; import java.util.Optional; @@ -50,8 +50,6 @@ public abstract class AbstractCacheStore implements CacheStore { Assert.notNull(key, "Cache key must not be blank"); return getInternal(key).map(cacheWrapper -> { - log.debug("Cache wrapper: [{}]", cacheWrapper); - // Check expiration if (cacheWrapper.getExpireAt() != null && cacheWrapper.getExpireAt().before(run.halo.app.utils.DateUtils.now())) { // Expired then delete it @@ -101,14 +99,7 @@ public abstract class AbstractCacheStore implements CacheStore { Date expireAt = null; if (timeout > 0 && timeUnit != null) { - // Handle expiration - long millis = timeUnit.toMillis(timeout); - if (millis <= 0) { - millis = 1L; - } - - // Calc the expiry time - expireAt = DateUtils.addMilliseconds(now, Long.valueOf(millis).intValue()); + expireAt = DateUtils.add(now, timeout, timeUnit); } // Build cache wrapper diff --git a/src/main/java/run/halo/app/cache/InMemoryCacheStore.java b/src/main/java/run/halo/app/cache/InMemoryCacheStore.java index 3f0984062..1964f930b 100644 --- a/src/main/java/run/halo/app/cache/InMemoryCacheStore.java +++ b/src/main/java/run/halo/app/cache/InMemoryCacheStore.java @@ -57,7 +57,7 @@ public class InMemoryCacheStore extends StringCacheStore { // Put the cache wrapper CacheWrapper putCacheWrapper = cacheContainer.put(key, cacheWrapper); - log.debug("Put cache wrapper: [{}]", putCacheWrapper); + log.debug("Put [{}] cache result: [{}], original cache wrapper: [{}]", key, putCacheWrapper, cacheWrapper); } @Override diff --git a/src/test/java/run/halo/app/utils/TimeUnitTest.java b/src/test/java/run/halo/app/utils/TimeUnitTest.java new file mode 100644 index 000000000..ca60d6fb5 --- /dev/null +++ b/src/test/java/run/halo/app/utils/TimeUnitTest.java @@ -0,0 +1,22 @@ +package run.halo.app.utils; + +import org.junit.Test; + +import java.util.concurrent.TimeUnit; + +/** + * Time unit test + * + * @author johnniang + * @date 19-4-29 + */ +public class TimeUnitTest { + + @Test + public void convertTest() { + Long millis = TimeUnit.DAYS.toMillis(30); + + System.out.println(millis); + System.out.println(millis.intValue()); + } +}