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 131af40ee..0fc28869e 100644
--- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java
+++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java
@@ -16,6 +16,9 @@
package org.springframework.data.redis.cache;
+import static org.springframework.util.Assert.*;
+import static org.springframework.util.ObjectUtils.*;
+
import java.util.Arrays;
import java.util.Set;
@@ -28,7 +31,6 @@ import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
-import org.springframework.util.Assert;
/**
* Cache implementation on top of Redis.
@@ -40,16 +42,10 @@ import org.springframework.util.Assert;
@SuppressWarnings("unchecked")
public class RedisCache implements Cache {
- private static final byte[] REMOVE_KEYS_BY_PATTERN_LUA = "local keys = redis.call('KEYS', ARGV[1]); local keysCount = table.getn(keys); if(keysCount > 0) then for _, key in ipairs(keys) do redis.call('del', key); end; end; return keysCount;"
- .getBytes();
- private static final int PAGE_SIZE = 128;
- private final String name;
- @SuppressWarnings("rawtypes") private final RedisTemplate template;
- private final byte[] prefix;
- private final byte[] setName;
- private final byte[] cacheLockName;
- private long WAIT_FOR_LOCK = 300;
- private final long expiration;
+ @SuppressWarnings("rawtypes")//
+ private final RedisTemplate template;
+ private final RedisCacheMetadata cacheMetadata;
+ private final CacheValueAccessor cacheValueAccessor;
/**
* Constructs a new RedisCache instance.
@@ -62,41 +58,12 @@ public class RedisCache implements Cache {
public RedisCache(String name, byte[] prefix, RedisTemplate extends Object, ? extends Object> template,
long expiration) {
- Assert.hasText(name, "non-empty cache name is required");
- this.name = name;
+ hasText(name, "non-empty cache name is required");
+ this.cacheMetadata = new RedisCacheMetadata(name, prefix);
+ this.cacheMetadata.setDefaultExpiration(expiration);
+
this.template = template;
- this.prefix = prefix;
- this.expiration = expiration;
-
- StringRedisSerializer stringSerializer = new StringRedisSerializer();
-
- // name of the set holding the keys
- this.setName = stringSerializer.serialize(name + "~keys");
- this.cacheLockName = stringSerializer.serialize(name + "~lock");
- }
-
- public String getName() {
- return name;
- }
-
- /**
- * {@inheritDoc} This implementation simply returns the RedisTemplate used for configuring the cache, giving access to
- * the underlying Redis store.
- */
- public Object getNativeCache() {
- return template;
- }
-
- public ValueWrapper get(final Object key) {
- return (ValueWrapper) template.execute(new RedisCallback() {
-
- public ValueWrapper doInRedis(RedisConnection connection) throws DataAccessException {
- waitForLock(connection);
- byte[] bs = connection.get(computeKey(key));
- Object value = template.getValueSerializer() != null ? template.getValueSerializer().deserialize(bs) : bs;
- return (bs == null ? null : new SimpleValueWrapper(value));
- }
- }, true);
+ this.cacheValueAccessor = new CacheValueAccessor(template.getValueSerializer());
}
/**
@@ -114,196 +81,538 @@ public class RedisCache implements Cache {
return wrapper == null ? null : (T) wrapper.get();
}
- public void put(final Object key, final Object value) {
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.cache.Cache#get(java.lang.Object)
+ */
+ @Override
+ public ValueWrapper get(Object key) {
+ return get(new RedisCacheKey(key).usePrefix(this.cacheMetadata.getKeyPrefix()).withKeySerializer(
+ template.getKeySerializer()));
+ }
- final byte[] keyBytes = computeKey(key);
- final byte[] valueBytes = convertToBytesIfNecessary(template.getValueSerializer(), value);
+ /**
+ * Return the value to which this cache maps the specified key.
+ *
+ * @param cacheKey the key whose associated value is to be returned via its binary representation.
+ * @return the {@link RedisCacheElement} stored at given key or {@literal null} if no value found for key.
+ * @since 1.5
+ */
+ public RedisCacheElement get(final RedisCacheKey cacheKey) {
- template.execute(new RedisCallback