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:
committed by
Christoph Strobl
parent
d97117b0a2
commit
cbbfad481c
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user