diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCache.java b/src/main/java/org/springframework/data/redis/cache/RedisCache.java index 58f438fcc..0a5bce9ab 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -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 get(final Object key, final Callable 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) { diff --git a/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java b/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java index d5b9b3940..83cc1a014 100644 --- a/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java @@ -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() { + @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() {