DATAREDIS-542 - Fix key expiration for RedisCache.putIfAbsent.

RedisCache now expires keys using putIfAbsent if the key was set. Previously, the key was only expired if the value was already present and the value matched the value stored inside of Redis.

Original Pull Request: #224
This commit is contained in:
Mark Paluch
2016-10-06 13:39:08 +02:00
committed by Christoph Strobl
parent 0b9b1804ff
commit ea598c2dad
2 changed files with 72 additions and 10 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.data.redis.cache;
import static org.springframework.util.Assert.*;
import static org.springframework.util.ObjectUtils.*;
import java.lang.reflect.Constructor;
import java.util.Arrays;
@@ -37,6 +36,7 @@ import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
/**
* Cache implementation on top of Redis.
@@ -720,20 +720,27 @@ public class RedisCache implements Cache {
public byte[] doInRedis(BinaryRedisCacheElement element, RedisConnection connection) throws DataAccessException {
waitForLock(connection);
byte[] resultValue = put(element, connection);
if (nullSafeEquals(element.get(), resultValue)) {
byte existingValue[] = null;
boolean keyMaintenance;
byte[] keyBytes = element.getKeyBytes();
byte[] value = element.get();
if (connection.setNX(keyBytes, value)) {
keyMaintenance = true;
} else {
existingValue = connection.get(keyBytes);
keyMaintenance = ObjectUtils.nullSafeEquals(value, existingValue);
}
if (keyMaintenance) {
processKeyExpiration(element, connection);
maintainKnownKeys(element, connection);
}
return resultValue;
}
private byte[] put(BinaryRedisCacheElement element, RedisConnection connection) {
boolean valueWasSet = connection.setNX(element.getKeyBytes(), element.get());
return valueWasSet ? null : connection.get(element.getKeyBytes());
return existingValue;
}
}