DATAREDIS-401 - RedisCacheManager should operate on RedisOperations.
We now no longer require RedisCache and RedisCacheManager to be initialized with a RedisTemplate but use RedisOperations instead. Original pull request: #142.
This commit is contained in:
committed by
Oliver Gierke
parent
f694be772d
commit
1ad3392a72
@@ -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<? extends Object, ? extends Object> template,
|
||||
public RedisCache(String name, byte[] prefix, RedisOperations<? extends Object, ? extends Object> 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<RedisCacheElement>(
|
||||
return (RedisCacheElement) redisOperations.execute(new AbstractRedisCacheCallback<RedisCacheElement>(
|
||||
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) {
|
||||
|
||||
@@ -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.<String> emptyList());
|
||||
public RedisCacheManager(RedisOperations redisOperations) {
|
||||
this(redisOperations, Collections.<String> 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<String> cacheNames) {
|
||||
this.template = template;
|
||||
public RedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames) {
|
||||
this.redisOperations = redisOperations;
|
||||
setCacheNames(cacheNames);
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ public class RedisCacheManager extends AbstractTransactionSupportingCacheManager
|
||||
@Override
|
||||
protected Collection<? extends Cache> 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
|
||||
.<Cache> 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<String> loadRemoteCacheKeys() {
|
||||
return (Set<String>) template.execute(new RedisCallback<Set<String>>() {
|
||||
return (Set<String>) redisOperations.execute(new RedisCallback<Set<String>>() {
|
||||
|
||||
@Override
|
||||
public Set<String> doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
|
||||
// we are using the ~keys postfix as defined in RedisCache#setName
|
||||
Set<byte[]> keys = connection.keys(template.getKeySerializer().serialize("*~keys"));
|
||||
Set<byte[]> keys = connection.keys(redisOperations.getKeySerializer().serialize("*~keys"));
|
||||
Set<String> cacheKeys = new LinkedHashSet<String>();
|
||||
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user