Skip to content

Commit 67106bc

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-510 - Fix caching of null values.
We now no longer add empty byte[] as cache value but return null instead. Original pull request: #201.
1 parent 28b5d61 commit 67106bc

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,14 @@ public Void doInRedis(BinaryRedisCacheElement element, RedisConnection connectio
686686
connection.multi();
687687
}
688688

689-
connection.set(element.getKeyBytes(), element.get());
689+
if (element.get().length == 0) {
690+
connection.del(element.getKeyBytes());
691+
} else {
692+
connection.set(element.getKeyBytes(), element.get());
690693

691-
processKeyExpiration(element, connection);
692-
maintainKnownKeys(element, connection);
694+
processKeyExpiration(element, connection);
695+
maintainKnownKeys(element, connection);
696+
}
693697

694698
if (!isClusterConnection(connection)) {
695699
connection.exec();

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.data.redis.cache;
1818

1919
import static edu.umd.cs.mtc.TestFramework.*;
20+
import static org.hamcrest.core.Is.*;
2021
import static org.hamcrest.core.IsEqual.*;
2122
import static org.hamcrest.core.IsInstanceOf.*;
2223
import static org.hamcrest.core.IsNot.*;
@@ -281,6 +282,38 @@ public void putIfAbsentShouldSetValueOnlyIfNotPresent() {
281282
assertThat(wrapper.get(), equalTo(value));
282283
}
283284

285+
/**
286+
* @see DATAREDIS-510
287+
*/
288+
@Test
289+
public void cachePutWithNullShouldNotAddStuffToRedis() {
290+
291+
Object key = getKey();
292+
Object value = getValue();
293+
294+
cache.put(key, null);
295+
296+
assertThat(cache.get(key), is(nullValue()));
297+
}
298+
299+
/**
300+
* @see DATAREDIS-510
301+
*/
302+
@Test
303+
public void cachePutWithNullShouldRemoveKeyIfExists() {
304+
305+
Object key = getKey();
306+
Object value = getValue();
307+
308+
cache.put(key, value);
309+
310+
assertThat(cache.get(key).get(), is(equalTo(value)));
311+
312+
cache.put(key, null);
313+
314+
assertThat(cache.get(key), is(nullValue()));
315+
}
316+
284317
/**
285318
* @see DATAREDIS-443
286319
* @see DATAREDIS-452

0 commit comments

Comments
 (0)