Fix cache expiry calculation error

pull/146/head
johnniang 2019-04-29 17:01:33 +08:00
parent 3193ed9f77
commit 0b35392b1a
3 changed files with 25 additions and 12 deletions

View File

@ -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<K, V> implements CacheStore<K, V> {
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<K, V> implements CacheStore<K, V> {
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

View File

@ -57,7 +57,7 @@ public class InMemoryCacheStore extends StringCacheStore {
// Put the cache wrapper
CacheWrapper<String> putCacheWrapper = cacheContainer.put(key, cacheWrapper);
log.debug("Put cache wrapper: [{}]", putCacheWrapper);
log.debug("Put [{}] cache result: [{}], original cache wrapper: [{}]", key, putCacheWrapper, cacheWrapper);
}
@Override

View File

@ -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());
}
}