+ add multiGet to hash operations + template

This commit is contained in:
Costin Leau
2010-11-29 21:50:01 +02:00
parent 1700061473
commit 6d35c3f1d9
4 changed files with 37 additions and 0 deletions

View File

@@ -34,6 +34,8 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
void set(HK key, HV value);
Collection<HV> multiGet(Set<HK> keys);
void multiSet(Map<? extends HK, ? extends HV> m);
Set<HK> keys();
@@ -43,4 +45,5 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
Integer length();
void delete(Object key);
}

View File

@@ -49,6 +49,11 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
return ops.get(getKey(), key);
}
@Override
public Collection<HV> multiGet(Set<HK> hashKeys) {
return ops.multiGet(getKey(), hashKeys);
}
@Override
public RedisOperations<H, ?> getOperations() {
return ops.getOperations();

View File

@@ -32,6 +32,8 @@ public interface HashOperations<H, HK, HV> {
HV get(H key, Object hashKey);
Collection<HV> multiGet(H key, Set<HK> hashKeys);
Integer increment(H key, HK hashKey, int delta);
Set<HK> keys(H key);

View File

@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -1039,6 +1040,32 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public Collection<HV> multiGet(K key, Set<HK> fields) {
if (fields.isEmpty()) {
return Collections.emptyList();
}
final byte[] rawKey = rawKey(key);
final byte[][] rawHashKeys = new byte[fields.size()][];
int counter = 0;
for (HK hashKey : fields) {
rawHashKeys[counter++] = rawHashKey(hashKey);
}
List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) {
return connection.hMGet(rawKey, rawHashKeys);
}
}, true);
return (List<HV>) hashValues(rawValues, List.class);
}
@Override
public void set(K key, HK hashKey, HV value) {
final byte[] rawKey = rawKey(key);