Skip to content

Commit

Permalink
DATAREDIS-344 - Assert RedisCache works with Spring 4.1.
Browse files Browse the repository at this point in the history
Added `putIfAbsent` to `RedisCache` to satisfy compatibility with newly introduced method.
  • Loading branch information
christophstrobl authored and Thomas Darimont committed Sep 4, 2014
1 parent f94df9f commit 1548b88
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
42 changes: 42 additions & 0 deletions src/main/java/org/springframework/data/redis/cache/RedisCache.java
Expand Up @@ -137,6 +137,38 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
}, true);
}

public ValueWrapper putIfAbsent(Object key, final Object value) {

final byte[] keyBytes = computeKey(key);
final byte[] valueBytes = convertToBytesIfNecessary(template.getValueSerializer(), value);

return toWrapper(template.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {

waitForLock(connection);

Object resultValue = value;
boolean valueWasSet = connection.setNX(keyBytes, valueBytes);
if (valueWasSet) {
connection.zAdd(setName, 0, keyBytes);
if (expiration > 0) {
connection.expire(keyBytes, expiration);
// update the expiration of the set of keys as well
connection.expire(setName, expiration);
}
} else {
resultValue = deserializeIfNecessary(template.getValueSerializer(), connection.get(keyBytes));
}

return resultValue;
}
}, true));
}

private ValueWrapper toWrapper(Object value) {
return (value != null ? new SimpleValueWrapper(value) : null);
}

public void evict(Object key) {
final byte[] k = computeKey(key);

Expand Down Expand Up @@ -227,4 +259,14 @@ private byte[] convertToBytesIfNecessary(RedisSerializer<Object> serializer, Obj

return serializer.serialize(value);
}

private Object deserializeIfNecessary(RedisSerializer<byte[]> serializer, byte[] value) {

if (serializer != null) {
return serializer.deserialize(value);
}

return value;
}

}
Expand Up @@ -16,14 +16,17 @@

package org.springframework.data.redis.cache;

import static org.hamcrest.core.IsInstanceOf.*;
import static org.hamcrest.core.IsNull.*;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.nullValue;
import static org.hamcrest.core.IsSame.sameInstance;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.*;
import static org.junit.Assume.assumeThat;
import static org.springframework.data.redis.matcher.RedisTestMatchers.isEqual;

import java.util.Collection;
Expand Down Expand Up @@ -71,7 +74,7 @@ public static Collection<Object[]> testParams() {
}

@SuppressWarnings("unchecked")
protected Cache createCache(RedisTemplate nativeCache) {
protected RedisCache createCache(RedisTemplate nativeCache) {
return new RedisCache(CACHE_NAME, CACHE_NAME.concat(":").getBytes(), nativeCache, TimeUnit.MINUTES.toSeconds(10));
}

Expand Down Expand Up @@ -246,4 +249,31 @@ public void testCacheGetShouldReturnNullIfNoCachedValueFound() {
Object invalidKey = template.getKeySerializer() == null ? "spring-data-redis".getBytes() : "spring-data-redis";
assertThat(redisCache.get(invalidKey, value.getClass()), nullValue());
}

/**
* @see DATAREDIS-344
*/
@Test
public void putIfAbsentShouldSetValueOnlyIfNotPresent() {

assumeThat(cache, instanceOf(RedisCache.class));

RedisCache redisCache = (RedisCache)cache;

Object key = getKey();
template.delete(key);

Object value = getValue();
ValueWrapper wrapper = redisCache.putIfAbsent(key, value);

assertThat(wrapper.get(), sameInstance(value));

ValueWrapper wrapper2 = redisCache.putIfAbsent(key, value);

if (!(value instanceof Number)) {
assertThat(wrapper2.get(), not(sameInstance(value)));
}

assertThat(wrapper2.get(), equalTo(value));
}
}

0 comments on commit 1548b88

Please sign in to comment.