DATAREDIS-592 - Consider expiry timeout in synchronized RedisCache mode.

We now consider the element expiry when retrieving cache elements with a value loader. Previously, all cache elements that were cached using `@Cacheable(sync=true)` or used `RedisCache.get(Object, Callable)` directly were considered eternal without setting a TTL.

Original Pull Request: #234
This commit is contained in:
Mark Paluch
2017-01-17 12:04:59 +01:00
committed by Christoph Strobl
parent d97117b0a2
commit cbbfad481c
2 changed files with 25 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,8 +15,6 @@
*/
package org.springframework.data.redis.cache;
import static org.springframework.util.Assert.*;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Set;
@@ -41,7 +39,6 @@ import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
/**
* Cache implementation on top of Redis.
@@ -142,8 +139,9 @@ public class RedisCache extends AbstractValueAdaptingCache {
*/
public <T> T get(final Object key, final Callable<T> valueLoader) {
BinaryRedisCacheElement rce = new BinaryRedisCacheElement(
new RedisCacheElement(getRedisCacheKey(key), new StoreTranslatingCallable(valueLoader)), cacheValueAccessor);
RedisCacheElement cacheElement = new RedisCacheElement(getRedisCacheKey(key),
new StoreTranslatingCallable(valueLoader)).expireAfter(cacheMetadata.getDefaultExpiration());
BinaryRedisCacheElement rce = new BinaryRedisCacheElement(cacheElement, cacheValueAccessor);
ValueWrapper val = get(key);
if (val != null) {

View File

@@ -248,7 +248,7 @@ public class RedisCacheUnitTests {
verify(connectionMock, times(1)).exec();
}
@Test // DATAREDIS-443
@Test // DATAREDIS-443, DATAREDIS-592
public void getWithCallableShouldReadValueFromCallableAddToCache() {
cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, 0L);
@@ -263,9 +263,29 @@ public class RedisCacheUnitTests {
verify(connectionMock, times(1)).get(eq(KEY_BYTES));
verify(connectionMock, times(1)).multi();
verify(connectionMock, times(1)).set(eq(KEY_BYTES), eq(VALUE_BYTES));
verify(connectionMock, never()).expire(any(byte[].class), anyLong());
verify(connectionMock, times(1)).exec();
}
@Test // DATAREDIS-592
public void getWithCallableShouldReadValueFromCallableAddToCacheWithTtl() {
cache = new RedisCache(CACHE_NAME, NO_PREFIX_BYTES, templateSpy, 100L);
cache.get(KEY, new Callable<Object>() {
@Override
public Object call() throws Exception {
return VALUE;
}
});
verify(connectionMock).get(eq(KEY_BYTES));
verify(connectionMock).multi();
verify(connectionMock).set(eq(KEY_BYTES), eq(VALUE_BYTES));
verify(connectionMock).expire(eq(KEY_BYTES), eq(100L));
verify(connectionMock).exec();
}
@Test // DATAREDIS-443
@SuppressWarnings("unchecked")
public void getWithCallableShouldNotReadValueFromCallableWhenAlreadyPresent() {