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 888cd8b1e..4a34ba4d6 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -28,7 +28,7 @@ import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.ReturnType; import org.springframework.data.redis.core.RedisCallback; -import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @@ -43,7 +43,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; public class RedisCache implements Cache { @SuppressWarnings("rawtypes")// - private final RedisTemplate template; + private final RedisOperations redisOperations; private final RedisCacheMetadata cacheMetadata; private final CacheValueAccessor cacheValueAccessor; @@ -52,18 +52,18 @@ public class RedisCache implements Cache { * * @param name cache name * @param prefix - * @param template + * @param redisOperations * @param expiration */ - public RedisCache(String name, byte[] prefix, RedisTemplate template, + public RedisCache(String name, byte[] prefix, RedisOperations redisOperations, long expiration) { hasText(name, "non-empty cache name is required"); this.cacheMetadata = new RedisCacheMetadata(name, prefix); this.cacheMetadata.setDefaultExpiration(expiration); - this.template = template; - this.cacheValueAccessor = new CacheValueAccessor(template.getValueSerializer()); + this.redisOperations = redisOperations; + this.cacheValueAccessor = new CacheValueAccessor(redisOperations.getValueSerializer()); } /** @@ -88,7 +88,7 @@ public class RedisCache implements Cache { @Override public ValueWrapper get(Object key) { return get(new RedisCacheKey(key).usePrefix(this.cacheMetadata.getKeyPrefix()).withKeySerializer( - template.getKeySerializer())); + redisOperations.getKeySerializer())); } /** @@ -101,7 +101,7 @@ public class RedisCache implements Cache { public RedisCacheElement get(final RedisCacheKey cacheKey) { notNull(cacheKey, "CacheKey must not be null!"); - return (RedisCacheElement) template.execute(new AbstractRedisCacheCallback( + return (RedisCacheElement) redisOperations.execute(new AbstractRedisCacheCallback( new RedisCacheElement(cacheKey, null), cacheMetadata) { @Override @@ -109,10 +109,10 @@ public class RedisCache implements Cache { throws DataAccessException { byte[] bs = connection.get(element.getKeyBytes()); - Object value = template.getValueSerializer() != null ? template.getValueSerializer().deserialize(bs) : bs; + Object value = redisOperations.getValueSerializer() != null ? redisOperations.getValueSerializer().deserialize(bs) : bs; return (bs == null ? null : new RedisCacheElement(element.getKey(), value)); } - }, true); + }); } /* @@ -123,7 +123,7 @@ public class RedisCache implements Cache { public void put(final Object key, final Object value) { put(new RedisCacheElement(new RedisCacheKey(key).usePrefix(cacheMetadata.getKeyPrefix()).withKeySerializer( - template.getKeySerializer()), value).expireAfter(cacheMetadata.getDefaultExpiration())); + redisOperations.getKeySerializer()), value).expireAfter(cacheMetadata.getDefaultExpiration())); } /** @@ -137,7 +137,7 @@ public class RedisCache implements Cache { public void put(RedisCacheElement element) { notNull(element, "Element must not be null!"); - template.execute(new RedisCachePutCallback(element, cacheValueAccessor, cacheMetadata), true); + redisOperations.execute(new RedisCachePutCallback(element, cacheValueAccessor, cacheMetadata)); } /* @@ -147,7 +147,7 @@ public class RedisCache implements Cache { public ValueWrapper putIfAbsent(Object key, final Object value) { return putIfAbsent(new RedisCacheElement(new RedisCacheKey(key).usePrefix(cacheMetadata.getKeyPrefix()) - .withKeySerializer(template.getKeySerializer()), value).expireAfter(cacheMetadata.getDefaultExpiration())); + .withKeySerializer(redisOperations.getKeySerializer()), value).expireAfter(cacheMetadata.getDefaultExpiration())); } /** @@ -161,8 +161,7 @@ public class RedisCache implements Cache { public ValueWrapper putIfAbsent(RedisCacheElement element) { notNull(element, "Element must not be null!"); - return toWrapper(template.execute(new RedisCachePutIfAbsentCallback(element, cacheValueAccessor, cacheMetadata), - true)); + return toWrapper(redisOperations.execute(new RedisCachePutIfAbsentCallback(element, cacheValueAccessor, cacheMetadata))); } /* @@ -171,7 +170,7 @@ public class RedisCache implements Cache { */ public void evict(Object key) { evict(new RedisCacheElement(new RedisCacheKey(key).usePrefix(cacheMetadata.getKeyPrefix()).withKeySerializer( - template.getKeySerializer()), null)); + redisOperations.getKeySerializer()), null)); } /** @@ -181,7 +180,7 @@ public class RedisCache implements Cache { public void evict(final RedisCacheElement element) { notNull(element, "Element must not be null!"); - template.execute(new RedisCacheEvictCallback(element, cacheMetadata)); + redisOperations.execute(new RedisCacheEvictCallback(element, cacheMetadata)); } /* @@ -189,8 +188,8 @@ public class RedisCache implements Cache { * @see org.springframework.cache.Cache#clear() */ public void clear() { - template.execute(cacheMetadata.usesKeyPrefix() ? new RedisCacheCleanByPrefixCallback(cacheMetadata) - : new RedisCacheCleanByKeysCallback(cacheMetadata), true); + redisOperations.execute(cacheMetadata.usesKeyPrefix() ? new RedisCacheCleanByPrefixCallback(cacheMetadata) + : new RedisCacheCleanByKeysCallback(cacheMetadata)); } /* @@ -206,7 +205,7 @@ public class RedisCache implements Cache { * the underlying Redis store. */ public Object getNativeCache() { - return template; + return redisOperations; } private ValueWrapper toWrapper(Object value) { diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java index ea0630b3b..ee48d8d26 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCacheManager.java @@ -34,7 +34,7 @@ import org.springframework.cache.transaction.TransactionAwareCacheDecorator; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; -import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.RedisOperations; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -57,7 +57,7 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager private final Log logger = LogFactory.getLog(RedisCacheManager.class); @SuppressWarnings("rawtypes")// - private final RedisTemplate template; + private final RedisOperations redisOperations; private boolean usePrefix = false; private RedisCachePrefix cachePrefix = new DefaultRedisCachePrefix(); @@ -73,23 +73,23 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager /** * Construct a {@link RedisCacheManager}. * - * @param template + * @param redisOperations */ @SuppressWarnings("rawtypes") - public RedisCacheManager(RedisTemplate template) { - this(template, Collections. emptyList()); + public RedisCacheManager(RedisOperations redisOperations) { + this(redisOperations, Collections. emptyList()); } /** * Construct a static {@link RedisCacheManager}, managing caches for the specified cache names only. * - * @param template + * @param redisOperations * @param cacheNames * @since 1.2 */ @SuppressWarnings("rawtypes") - public RedisCacheManager(RedisTemplate template, Collection cacheNames) { - this.template = template; + public RedisCacheManager(RedisOperations redisOperations, Collection cacheNames) { + this.redisOperations = redisOperations; setCacheNames(cacheNames); } @@ -168,7 +168,7 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager @Override protected Collection loadCaches() { - Assert.notNull(this.template, "A redis template is required in order to interact with data store"); + Assert.notNull(this.redisOperations, "A redis template is required in order to interact with data store"); return addConfiguredCachesIfNecessary(loadRemoteCachesOnStartup ? loadAndInitRemoteCaches() : Collections . emptyList()); } @@ -214,7 +214,7 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager @SuppressWarnings("unchecked") protected RedisCache createCache(String cacheName) { long expiration = computeExpiration(cacheName); - return new RedisCache(cacheName, (usePrefix ? cachePrefix.prefix(cacheName) : null), template, expiration); + return new RedisCache(cacheName, (usePrefix ? cachePrefix.prefix(cacheName) : null), redisOperations, expiration); } protected long computeExpiration(String name) { @@ -249,18 +249,18 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager @SuppressWarnings("unchecked") protected Set loadRemoteCacheKeys() { - return (Set) template.execute(new RedisCallback>() { + return (Set) redisOperations.execute(new RedisCallback>() { @Override public Set doInRedis(RedisConnection connection) throws DataAccessException { // we are using the ~keys postfix as defined in RedisCache#setName - Set keys = connection.keys(template.getKeySerializer().serialize("*~keys")); + Set keys = connection.keys(redisOperations.getKeySerializer().serialize("*~keys")); Set cacheKeys = new LinkedHashSet(); if (!CollectionUtils.isEmpty(keys)) { for (byte[] key : keys) { - cacheKeys.add(template.getKeySerializer().deserialize(key).toString().replace("~keys", "")); + cacheKeys.add(redisOperations.getKeySerializer().deserialize(key).toString().replace("~keys", "")); } } @@ -270,8 +270,8 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager } @SuppressWarnings("rawtypes") - protected RedisTemplate getTemplate() { - return template; + protected RedisOperations getRedisOperations() { + return redisOperations; } protected RedisCachePrefix getCachePrefix() {