Skip to content

Commit 72d6df0

Browse files
DATAREDIS-542 - Polishing.
Prevent putIfAbsent from prolonging expiration time of existing keys having same value. Remove some trailing white spaces. Original Pull Request: spring-projects#224
1 parent ea598c2 commit 72d6df0

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

src/main/java/org/springframework/data/redis/cache/RedisCache.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public RedisCache(String name, byte[] prefix, RedisOperations<? extends Object,
7676
/**
7777
* Return the value to which this cache maps the specified key, generically specifying a type that return value will
7878
* be cast to.
79-
*
79+
*
8080
* @param key
8181
* @param type
8282
* @return
@@ -126,7 +126,7 @@ public <T> T get(final Object key, final Callable<T> valueLoader) {
126126

127127
/**
128128
* Return the value to which this cache maps the specified key.
129-
*
129+
*
130130
* @param cacheKey the key whose associated value is to be returned via its binary representation.
131131
* @return the {@link RedisCacheElement} stored at given key or {@literal null} if no value found for key.
132132
* @since 1.5
@@ -162,7 +162,7 @@ public void put(final Object key, final Object value) {
162162
* Add the element by adding {@link RedisCacheElement#get()} at {@link RedisCacheElement#getKeyBytes()}. If the cache
163163
* previously contained a mapping for this {@link RedisCacheElement#getKeyBytes()}, the old value is replaced by
164164
* {@link RedisCacheElement#get()}.
165-
*
165+
*
166166
* @param element must not be {@literal null}.
167167
* @since 1.5
168168
*/
@@ -188,7 +188,7 @@ public ValueWrapper putIfAbsent(Object key, final Object value) {
188188
/**
189189
* Add the element as long as no element exists at {@link RedisCacheElement#getKeyBytes()}. If a value is present for
190190
* {@link RedisCacheElement#getKeyBytes()} this one is returned.
191-
*
191+
*
192192
* @param element must not be {@literal null}.
193193
* @return
194194
* @since 1.5
@@ -256,7 +256,7 @@ private ValueWrapper toWrapper(Object value) {
256256
/**
257257
* Metadata required to maintain {@link RedisCache}. Keeps track of additional data structures required for processing
258258
* cache operations.
259-
*
259+
*
260260
* @author Christoph Strobl
261261
* @since 1.5
262262
*/
@@ -294,7 +294,7 @@ public boolean usesKeyPrefix() {
294294

295295
/**
296296
* Get the binary representation of the key prefix.
297-
*
297+
*
298298
* @return never {@literal null}.
299299
*/
300300
public byte[] getKeyPrefix() {
@@ -303,7 +303,7 @@ public byte[] getKeyPrefix() {
303303

304304
/**
305305
* Get the binary representation of the key identifying the data structure used to maintain known keys.
306-
*
306+
*
307307
* @return never {@literal null}.
308308
*/
309309
public byte[] getSetOfKnownKeysKey() {
@@ -312,7 +312,7 @@ public byte[] getSetOfKnownKeysKey() {
312312

313313
/**
314314
* Get the binary representation of the key identifying the data structure used to lock the cache.
315-
*
315+
*
316316
* @return never {@literal null}.
317317
*/
318318
public byte[] getCacheLockKey() {
@@ -321,7 +321,7 @@ public byte[] getCacheLockKey() {
321321

322322
/**
323323
* Get the name of the cache.
324-
*
324+
*
325325
* @return
326326
*/
327327
public String getCacheName() {
@@ -330,7 +330,7 @@ public String getCacheName() {
330330

331331
/**
332332
* Set the default expiration time in seconds
333-
*
333+
*
334334
* @param defaultExpiration
335335
*/
336336
public void setDefaultExpiration(long seconds) {
@@ -339,7 +339,7 @@ public void setDefaultExpiration(long seconds) {
339339

340340
/**
341341
* Get the default expiration time in seconds.
342-
*
342+
*
343343
* @return
344344
*/
345345
public long getDefaultExpiration() {
@@ -721,26 +721,17 @@ public byte[] doInRedis(BinaryRedisCacheElement element, RedisConnection connect
721721

722722
waitForLock(connection);
723723

724-
byte existingValue[] = null;
725-
726-
boolean keyMaintenance;
727-
728724
byte[] keyBytes = element.getKeyBytes();
729725
byte[] value = element.get();
730726

731-
if (connection.setNX(keyBytes, value)) {
732-
keyMaintenance = true;
733-
} else {
734-
existingValue = connection.get(keyBytes);
735-
keyMaintenance = ObjectUtils.nullSafeEquals(value, existingValue);
727+
if (!connection.setNX(keyBytes, value)) {
728+
return connection.get(keyBytes);
736729
}
737730

738-
if (keyMaintenance) {
739-
processKeyExpiration(element, connection);
740-
maintainKnownKeys(element, connection);
741-
}
731+
maintainKnownKeys(element, connection);
732+
processKeyExpiration(element, connection);
742733

743-
return existingValue;
734+
return null;
744735
}
745736
}
746737

src/test/java/org/springframework/data/redis/cache/RedisCacheUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void putIfAbsentShouldNotExpireWhenValueWasNotSetAndRedisContainsOtherDat
197197
* @see DATAREDIS-542
198198
*/
199199
@Test
200-
public void putIfAbsentShouldExpireWhenValueWasNotSetAndRedisContainsSameData() {
200+
public void putIfAbsentShouldNotSetExpireWhenValueWasNotSetAndRedisContainsSameData() {
201201

202202
when(connectionMock.setNX(KEY_BYTES, VALUE_BYTES)).thenReturn(false);
203203
when(connectionMock.get(KEY_BYTES)).thenReturn(VALUE_BYTES);
@@ -206,7 +206,7 @@ public void putIfAbsentShouldExpireWhenValueWasNotSetAndRedisContainsSameData()
206206
Cache.ValueWrapper valueWrapper = cache.putIfAbsent(KEY, VALUE);
207207

208208
assertThat(valueWrapper, is(notNullValue()));
209-
verify(connectionMock).expire(eq(KEY_BYTES), anyLong());
209+
verify(connectionMock, never()).expire(eq(KEY_BYTES), anyLong());
210210
}
211211

212212
/**

0 commit comments

Comments
 (0)