+ add putIfAbsent on Hash operations

This commit is contained in:
Costin Leau
2011-01-10 13:22:23 +02:00
parent c3d8c01e65
commit 1966409375
4 changed files with 24 additions and 0 deletions

View File

@@ -36,6 +36,8 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
void put(HK key, HV value);
Boolean putIfAbsent(HK key, HV value);
Collection<HV> multiGet(Collection<HK> keys);
void putAll(Map<? extends HK, ? extends HV> m);

View File

@@ -89,6 +89,11 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
ops.put(getKey(), key, value);
}
@Override
public Boolean putIfAbsent(HK key, HV value) {
return ops.putIfAbsent(getKey(), key, value);
}
@Override
public Collection<HV> values() {
return ops.values(getKey());

View File

@@ -44,6 +44,8 @@ public interface HashOperations<H, HK, HV> {
void put(H key, HK hashKey, HV value);
Boolean putIfAbsent(H key, HK hashKey, HV value);
Collection<HV> values(H key);
Map<HK, HV> entries(H key);

View File

@@ -1606,6 +1606,21 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public Boolean putIfAbsent(K key, HK hashKey, HV value) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawHashKey(hashKey);
final byte[] rawHashValue = rawHashValue(value);
return execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) {
return connection.hSetNX(rawKey, rawHashKey, rawHashValue);
}
}, true);
}
@SuppressWarnings("unchecked")
@Override
public List<HV> values(K key) {