+ several API improvements
- eliminated use of varags with generified types - eliminated primitives as return types
This commit is contained in:
@@ -36,7 +36,7 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
|
||||
|
||||
void set(HK key, HV value);
|
||||
|
||||
Collection<HV> multiGet(Set<HK> keys);
|
||||
Collection<HV> multiGet(Collection<HK> keys);
|
||||
|
||||
void multiSet(Map<? extends HK, ? extends HV> m);
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ public interface BoundKeyOperations<K> extends KeyBound<K> {
|
||||
|
||||
Boolean expireAt(Date date);
|
||||
|
||||
long getExpire();
|
||||
Long getExpire();
|
||||
|
||||
void persist();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.data.keyvalue.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -27,17 +28,17 @@ public interface BoundSetOperations<K, V> extends KeyBound<K> {
|
||||
|
||||
RedisOperations<K, V> getOperations();
|
||||
|
||||
Set<V> diff(K... keys);
|
||||
Set<V> diff(Collection<K> keys);
|
||||
|
||||
void diffAndStore(K destKey, K... keys);
|
||||
void diffAndStore(K destKey, Collection<K> keys);
|
||||
|
||||
Set<V> intersect(K... keys);
|
||||
Set<V> intersect(Collection<K> keys);
|
||||
|
||||
void intersectAndStore(K destKey, K... keys);
|
||||
void intersectAndStore(K destKey, Collection<K> keys);
|
||||
|
||||
Set<V> union(K... keys);
|
||||
Set<V> union(Collection<K> keys);
|
||||
|
||||
void unionAndStore(K destKey, K... keys);
|
||||
void unionAndStore(K destKey, Collection<K> keys);
|
||||
|
||||
Boolean add(V value);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.data.keyvalue.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
@@ -28,7 +29,7 @@ public interface BoundZSetOperations<K, V> extends KeyBound<K> {
|
||||
|
||||
RedisOperations<K, V> getOperations();
|
||||
|
||||
void intersectAndStore(K destKey, K... keys);
|
||||
void intersectAndStore(K destKey, Collection<K> keys);
|
||||
|
||||
Set<V> range(long start, long end);
|
||||
|
||||
@@ -40,7 +41,7 @@ public interface BoundZSetOperations<K, V> extends KeyBound<K> {
|
||||
|
||||
void removeRangeByScore(double min, double max);
|
||||
|
||||
void unionAndStore(K destKey, K... keys);
|
||||
void unionAndStore(K destKey, Collection<K> keys);
|
||||
|
||||
Boolean add(V value, double score);
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<HV> multiGet(Set<HK> hashKeys) {
|
||||
public Collection<HV> multiGet(Collection<HK> hashKeys) {
|
||||
return ops.multiGet(getKey(), hashKeys);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.data.keyvalue.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -45,12 +46,12 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> diff(K... keys) {
|
||||
public Set<V> diff(Collection<K> keys) {
|
||||
return ops.diff(getKey(), keys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void diffAndStore(K destKey, K... keys) {
|
||||
public void diffAndStore(K destKey, Collection<K> keys) {
|
||||
ops.diffAndStore(getKey(), destKey, keys);
|
||||
}
|
||||
|
||||
@@ -60,12 +61,12 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> intersect(K... keys) {
|
||||
public Set<V> intersect(Collection<K> keys) {
|
||||
return ops.intersect(getKey(), keys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K destKey, K... keys) {
|
||||
public void intersectAndStore(K destKey, Collection<K> keys) {
|
||||
ops.intersectAndStore(getKey(), destKey, keys);
|
||||
}
|
||||
|
||||
@@ -90,12 +91,12 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> union(K... keys) {
|
||||
public Set<V> union(Collection<K> keys) {
|
||||
return ops.union(getKey(), keys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K destKey, K... keys) {
|
||||
public void unionAndStore(K destKey, Collection<K> keys) {
|
||||
ops.unionAndStore(getKey(), destKey, keys);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.data.keyvalue.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -49,7 +50,7 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K destKey, K... keys) {
|
||||
public void intersectAndStore(K destKey, Collection<K> keys) {
|
||||
ops.intersectAndStore(getKey(), destKey, keys);
|
||||
}
|
||||
|
||||
@@ -104,7 +105,7 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K destKey, K... keys) {
|
||||
public void unionAndStore(K destKey, Collection<K> keys) {
|
||||
ops.unionAndStore(getKey(), destKey, keys);
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public interface HashOperations<H, HK, HV> {
|
||||
|
||||
HV get(H key, Object hashKey);
|
||||
|
||||
Collection<HV> multiGet(H key, Set<HK> hashKeys);
|
||||
Collection<HV> multiGet(H key, Collection<HK> hashKeys);
|
||||
|
||||
Long increment(H key, HK hashKey, long delta);
|
||||
|
||||
|
||||
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.keyvalue.redis.core;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Key value operations with 'friendly' names instead of using command names for methods.
|
||||
* Additional helper methods for working with keys and values
|
||||
*
|
||||
* @author Mark Pollack
|
||||
*
|
||||
*/
|
||||
public interface KeyValueOperations {
|
||||
|
||||
// Set and Set with expiry operations
|
||||
|
||||
void set(String key, String value);
|
||||
|
||||
void set(String key, String value, long expiryInMillis);
|
||||
|
||||
void setAsBytes(String key, byte[] value);
|
||||
|
||||
void setAsBytes(String key, byte[] value, long expiryInMillis);
|
||||
|
||||
void convertAndSet(String key, Object value);
|
||||
|
||||
void convertAndSet(String key, Object value, long expiryInMillis);
|
||||
|
||||
// Get operations
|
||||
|
||||
String get(String key);
|
||||
|
||||
byte[] getAsBytes(String key);
|
||||
|
||||
<T> T getAndConvert(String key, Class<T> requiredType);
|
||||
|
||||
// Get and Set operations
|
||||
|
||||
String getAndSet(String key, String value);
|
||||
|
||||
byte[] getAndSetBytes(String key, byte[] value);
|
||||
|
||||
<T> T getAndSetObject(String key, T value, Class<T> requiredType);
|
||||
|
||||
// Multi-get operations
|
||||
|
||||
List<String> getValues(List<String> keys);
|
||||
|
||||
<T> List<T> getAndConvertValues(List<String> keys, Class<T> requiredType);
|
||||
|
||||
|
||||
// Set if non-existent operations
|
||||
|
||||
void setIfKeyNonExistent(String key, String value);
|
||||
|
||||
void setIfKeyNonExistent(String key, byte[] value);
|
||||
|
||||
void convertAndSetIfKeyNonExistent(String key, Object value);
|
||||
|
||||
// Multiple key-value set
|
||||
|
||||
void setMultiple(Map<String, String> keysAndValues);
|
||||
|
||||
void setMultipleAsBytes(Map<String, byte[]> keysAndValues);
|
||||
|
||||
<T> void convertAndSetMultiple(Map<String, T> keysAndValues);
|
||||
|
||||
// Multiple key-value set if non-existent
|
||||
|
||||
void setMultipleIfKeysNonExistent(Map<String, String> keysAndValues);
|
||||
|
||||
void setMultipleAsBytesIfKeysNonExistent(Map<String, byte[]> keysAndValues);
|
||||
|
||||
<T> void convertAndSetMultipleIfKeysNonExistent(Map<String, T> keysAndValues);
|
||||
|
||||
|
||||
|
||||
// Append
|
||||
|
||||
int append(String key, String value);
|
||||
|
||||
|
||||
|
||||
// Increment
|
||||
|
||||
int increment(String key);
|
||||
|
||||
int incrementBy(String key, int value);
|
||||
|
||||
// Decrement
|
||||
|
||||
int decrement(String key);
|
||||
|
||||
int decrementBy(String key, int value);
|
||||
|
||||
|
||||
// Substring
|
||||
|
||||
String getSubString(String key, int fromIndex, int toIndex);
|
||||
|
||||
boolean containsKey(String key);
|
||||
|
||||
boolean deleteKeys(String... keys);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public interface RedisOperations<K, V> {
|
||||
|
||||
void persist(K key);
|
||||
|
||||
long getExpire(K key);
|
||||
Long getExpire(K key);
|
||||
|
||||
void watch(Collection<K> keys);
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.keyvalue.redis.connection.DataType;
|
||||
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
|
||||
@@ -73,17 +74,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void del(final String redisKey) {
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
public Object doInRedis(RedisConnection connection) {
|
||||
connection.del(keySerializer.serialize(redisKey));
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public <T> T execute(RedisCallback<T> action) {
|
||||
return execute(action, isExposeConnection());
|
||||
}
|
||||
@@ -92,7 +82,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
return execute(action, exposeConnection, valueSerializer);
|
||||
}
|
||||
|
||||
public <T> T execute(RedisCallback<T> action, boolean exposeConnection, RedisSerializer returnSerializer) {
|
||||
public <T> T execute(RedisCallback<T> action, boolean exposeConnection, RedisSerializer<?> returnSerializer) {
|
||||
Assert.notNull(action, "Callback object must not be null");
|
||||
|
||||
RedisConnectionFactory factory = getConnectionFactory();
|
||||
@@ -145,7 +135,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
*
|
||||
* @param serializer
|
||||
*/
|
||||
public void setKeySerializer(RedisSerializer serializer) {
|
||||
public void setKeySerializer(RedisSerializer<?> serializer) {
|
||||
this.keySerializer = serializer;
|
||||
}
|
||||
|
||||
@@ -154,7 +144,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
*
|
||||
* @param serializer
|
||||
*/
|
||||
public void setValueSerializer(RedisSerializer serializer) {
|
||||
public void setValueSerializer(RedisSerializer<?> serializer) {
|
||||
this.valueSerializer = serializer;
|
||||
}
|
||||
|
||||
@@ -163,7 +153,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
*
|
||||
* @param hashKeySerializer The hashKeySerializer to set.
|
||||
*/
|
||||
public void setHashKeySerializer(RedisSerializer hashKeySerializer) {
|
||||
public void setHashKeySerializer(RedisSerializer<?> hashKeySerializer) {
|
||||
this.hashKeySerializer = hashKeySerializer;
|
||||
}
|
||||
|
||||
@@ -172,7 +162,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
*
|
||||
* @param hashValueSerializer The hashValueSerializer to set.
|
||||
*/
|
||||
public void setHashValueSerializer(RedisSerializer hashValueSerializer) {
|
||||
public void setHashValueSerializer(RedisSerializer<?> hashValueSerializer) {
|
||||
this.hashValueSerializer = hashValueSerializer;
|
||||
}
|
||||
|
||||
@@ -216,24 +206,16 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private byte[] rawKey(Object key) {
|
||||
return (key != null ? keySerializer.serialize(key) : null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> byte[] rawValue(T value) {
|
||||
return (value != null ? valueSerializer.serialize(value) : null);
|
||||
}
|
||||
|
||||
private byte[][] rawKeys(K... keys) {
|
||||
final byte[][] rawKeys = new byte[keys.length][];
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
rawKeys[i] = rawKey(keys[i]);
|
||||
}
|
||||
|
||||
return rawKeys;
|
||||
}
|
||||
|
||||
private byte[][] rawKeys(Collection<K> keys) {
|
||||
final byte[][] rawKeys = new byte[keys.size()][];
|
||||
|
||||
@@ -245,10 +227,25 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
return rawKeys;
|
||||
}
|
||||
|
||||
private byte[][] rawKeys(K key, Collection<K> keys) {
|
||||
final byte[][] rawKeys = new byte[keys.size() + 1][];
|
||||
|
||||
|
||||
rawKeys[0] = rawKey(key);
|
||||
int i = 1;
|
||||
for (K k : keys) {
|
||||
rawKeys[i++] = rawKey(k);
|
||||
}
|
||||
|
||||
return rawKeys;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <HK> byte[] rawHashKey(HK value) {
|
||||
return (value != null ? hashKeySerializer.serialize(value) : null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <HV> byte[] rawHashValue(HV value) {
|
||||
return (value != null ? hashValueSerializer.serialize(value) : null);
|
||||
}
|
||||
@@ -303,7 +300,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
return (V) deserialize(value, valueSerializer);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings( { "unchecked", "unused" })
|
||||
private <HK> HK deserializeHashKey(byte[] value) {
|
||||
return (HK) deserialize(value, hashKeySerializer);
|
||||
}
|
||||
@@ -332,7 +329,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final V doInRedis(RedisConnection connection) {
|
||||
byte[] result = inRedis(rawKey(key), connection);
|
||||
@@ -442,7 +438,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
//
|
||||
|
||||
@Override
|
||||
public long getExpire(K key) {
|
||||
public Long getExpire(K key) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
|
||||
return execute(new RedisCallback<Long>() {
|
||||
@@ -569,6 +565,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
public V increment(K key, final long delta) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
// TODO add conversion service in here ?
|
||||
ConversionService cs;
|
||||
return (V) execute(new RedisCallback<Long>() {
|
||||
@Override
|
||||
public Long doInRedis(RedisConnection connection) {
|
||||
@@ -590,7 +587,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> multiGet(Set<K> keys) {
|
||||
public Collection<V> multiGet(Collection<K> keys) {
|
||||
if (keys.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@@ -883,16 +880,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
// Set operations
|
||||
//
|
||||
|
||||
private K[] aggregateKeys(K key, K... keys) {
|
||||
Object[] aggregate = new Object[keys.length + 1];
|
||||
aggregate[0] = key;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
aggregate[i + 1] = keys[i];
|
||||
}
|
||||
|
||||
return (K[]) aggregate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BoundSetOperations<K, V> forSet(K key) {
|
||||
return new DefaultBoundSetOperations<K, V>(key, this);
|
||||
@@ -918,8 +905,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> diff(final K key, final K... keys) {
|
||||
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
|
||||
public Set<V> diff(final K key, final Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
|
||||
@Override
|
||||
public Set<byte[]> doInRedis(RedisConnection connection) {
|
||||
@@ -931,8 +918,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void diffAndStore(final K key, K destKey, final K... keys) {
|
||||
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
|
||||
public void diffAndStore(final K key, K destKey, final Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
@@ -949,8 +936,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> intersect(K key, K... keys) {
|
||||
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
|
||||
public Set<V> intersect(K key, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
|
||||
@Override
|
||||
public Set<byte[]> doInRedis(RedisConnection connection) {
|
||||
@@ -962,8 +949,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K key, K destKey, K... keys) {
|
||||
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
|
||||
public void intersectAndStore(K key, K destKey, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
@@ -1023,8 +1010,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> union(K key, K... keys) {
|
||||
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
|
||||
public Set<V> union(K key, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
|
||||
@Override
|
||||
public Set<byte[]> doInRedis(RedisConnection connection) {
|
||||
@@ -1036,8 +1023,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K key, K destKey, K... keys) {
|
||||
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
|
||||
public void unionAndStore(K key, K destKey, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
@@ -1084,8 +1071,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K key, K destKey, K... keys) {
|
||||
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
|
||||
public void intersectAndStore(K key, K destKey, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
@@ -1229,8 +1216,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K key, K destKey, K... keys) {
|
||||
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
|
||||
public void unionAndStore(K key, K destKey, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
@@ -1306,6 +1293,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Set<HK> keys(K key) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
@@ -1356,8 +1344,9 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<HV> multiGet(K key, Set<HK> fields) {
|
||||
public Collection<HV> multiGet(K key, Collection<HK> fields) {
|
||||
if (fields.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@@ -1396,6 +1385,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}, true);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<HV> values(K key) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.data.keyvalue.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -25,19 +26,19 @@ import java.util.Set;
|
||||
*/
|
||||
public interface SetOperations<K, V> {
|
||||
|
||||
Set<V> diff(K key, K... keys);
|
||||
Set<V> diff(K key, Collection<K> keys);
|
||||
|
||||
void diffAndStore(K key, K destKey, K... keys);
|
||||
void diffAndStore(K key, K destKey, Collection<K> keys);
|
||||
|
||||
RedisOperations<K, V> getOperations();
|
||||
|
||||
Set<V> intersect(K key, K... keys);
|
||||
Set<V> intersect(K key, Collection<K> keys);
|
||||
|
||||
void intersectAndStore(K key, K destKey, K... keys);
|
||||
void intersectAndStore(K key, K destKey, Collection<K> keys);
|
||||
|
||||
Set<V> union(K key, K... keys);
|
||||
Set<V> union(K key, Collection<K> keys);
|
||||
|
||||
void unionAndStore(K key, K destKey, K... keys);
|
||||
void unionAndStore(K key, K destKey, Collection<K> keys);
|
||||
|
||||
Boolean add(K key, V value);
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -41,7 +40,7 @@ public interface ValueOperations<K, V> {
|
||||
|
||||
V getAndSet(K key, V value);
|
||||
|
||||
Collection<V> multiGet(Set<K> keys);
|
||||
Collection<V> multiGet(Collection<K> keys);
|
||||
|
||||
V increment(K key, long delta);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.data.keyvalue.redis.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -25,7 +26,7 @@ import java.util.Set;
|
||||
*/
|
||||
public interface ZSetOperations<K, V> {
|
||||
|
||||
void intersectAndStore(K key, K destKey, K... keys);
|
||||
void intersectAndStore(K key, K destKey, Collection<K> keys);
|
||||
|
||||
Set<V> range(K key, long start, long end);
|
||||
|
||||
@@ -37,7 +38,7 @@ public interface ZSetOperations<K, V> {
|
||||
|
||||
void removeRangeByScore(K key, double min, double max);
|
||||
|
||||
void unionAndStore(K key, K destKey, K... keys);
|
||||
void unionAndStore(K key, K destKey, Collection<K> keys);
|
||||
|
||||
Boolean add(K key, V value, double score);
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.keyvalue.redis.support.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -36,4 +37,14 @@ abstract class CollectionUtils {
|
||||
|
||||
return (List<E>) Arrays.asList(reverse);
|
||||
}
|
||||
|
||||
static Collection<String> extractKeys(Collection<? extends RedisStore<String>> stores) {
|
||||
Collection<String> keys = new ArrayList<String>(stores.size());
|
||||
|
||||
for (RedisStore<String> store : stores) {
|
||||
keys.add(store.getKey());
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.keyvalue.redis.support.collections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.keyvalue.redis.core.BoundSetOperations;
|
||||
import org.springframework.data.keyvalue.redis.core.RedisOperations;
|
||||
@@ -64,36 +67,36 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> diff(RedisSet<? extends E>... sets) {
|
||||
return boundSetOps.diff(extractKeys(sets));
|
||||
public Set<E> diff(Collection<RedisSet<? extends E>> sets) {
|
||||
return boundSetOps.diff(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSet<E> diffAndStore(String destKey, RedisSet<? extends E>... sets) {
|
||||
boundSetOps.diffAndStore(destKey, extractKeys(sets));
|
||||
return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey));
|
||||
public RedisSet<E> diffAndStore(String destKey, Collection<RedisSet<? extends E>> sets) {
|
||||
boundSetOps.diffAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().forSet(destKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> intersect(RedisSet<? extends E>... sets) {
|
||||
return boundSetOps.intersect(extractKeys(sets));
|
||||
public Set<E> intersect(Collection<RedisSet<? extends E>> sets) {
|
||||
return boundSetOps.intersect(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSet<E> intersectAndStore(String destKey, RedisSet<? extends E>... sets) {
|
||||
boundSetOps.intersectAndStore(destKey, extractKeys(sets));
|
||||
return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey));
|
||||
public RedisSet<E> intersectAndStore(String destKey, Collection<RedisSet<? extends E>> sets) {
|
||||
boundSetOps.intersectAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().forSet(destKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> union(RedisSet<? extends E>... sets) {
|
||||
return boundSetOps.union(extractKeys(sets));
|
||||
public Set<E> union(Collection<RedisSet<? extends E>> sets) {
|
||||
return boundSetOps.union(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSet<E> unionAndStore(String destKey, RedisSet<? extends E>... sets) {
|
||||
boundSetOps.unionAndStore(destKey, extractKeys(sets));
|
||||
return new DefaultRedisSet(boundSetOps.getOperations().forSet(destKey));
|
||||
public RedisSet<E> unionAndStore(String destKey, Collection<RedisSet<? extends E>> sets) {
|
||||
boundSetOps.unionAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().forSet(destKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -105,7 +108,8 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
|
||||
public void clear() {
|
||||
// intersect the set with a non existing one
|
||||
// TODO: find a safer way to clean the set
|
||||
boundSetOps.intersectAndStore(key, "NON-EXISTING");
|
||||
String randomKey = UUID.randomUUID().toString();
|
||||
boundSetOps.intersectAndStore(key, Collections.singleton(randomKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -127,13 +131,4 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
|
||||
public int size() {
|
||||
return boundSetOps.size().intValue();
|
||||
}
|
||||
|
||||
private String[] extractKeys(RedisSet<?>... sets) {
|
||||
String[] keys = new String[sets.length];
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = sets[i].getKey();
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.keyvalue.redis.support.collections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
@@ -90,8 +91,8 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisZSet<E> intersectAndStore(String destKey, RedisZSet<E>... sets) {
|
||||
boundZSetOps.intersectAndStore(destKey, extractKeys(sets));
|
||||
public RedisZSet<E> intersectAndStore(String destKey, Collection<RedisZSet<? extends E>> sets) {
|
||||
boundZSetOps.intersectAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore());
|
||||
}
|
||||
|
||||
@@ -123,8 +124,8 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisZSet<E> unionAndStore(String destKey, RedisZSet<E>... sets) {
|
||||
boundZSetOps.unionAndStore(destKey, extractKeys(sets));
|
||||
public RedisZSet<E> unionAndStore(String destKey, Collection<RedisZSet<? extends E>> sets) {
|
||||
boundZSetOps.unionAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore());
|
||||
}
|
||||
|
||||
@@ -198,14 +199,4 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
|
||||
public Double score(Object o) {
|
||||
return boundZSetOps.score(o);
|
||||
}
|
||||
|
||||
private String[] extractKeys(RedisZSet<E>... sets) {
|
||||
String[] keys = new String[sets.length];
|
||||
keys[0] = key;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = sets[i].getKey();
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.keyvalue.redis.support.collections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -25,15 +26,15 @@ import java.util.Set;
|
||||
*/
|
||||
public interface RedisSet<E> extends RedisStore<String>, Set<E> {
|
||||
|
||||
Set<E> intersect(RedisSet<? extends E>... sets);
|
||||
Set<E> intersect(Collection<RedisSet<? extends E>> sets);
|
||||
|
||||
Set<E> union(RedisSet<? extends E>... sets);
|
||||
Set<E> union(Collection<RedisSet<? extends E>> sets);
|
||||
|
||||
Set<E> diff(RedisSet<? extends E>... sets);
|
||||
Set<E> diff(Collection<RedisSet<? extends E>> sets);
|
||||
|
||||
RedisSet<E> intersectAndStore(String destKey, RedisSet<? extends E>... sets);
|
||||
RedisSet<E> intersectAndStore(String destKey, Collection<RedisSet<? extends E>> sets);
|
||||
|
||||
RedisSet<E> unionAndStore(String destKey, RedisSet<? extends E>... sets);
|
||||
RedisSet<E> unionAndStore(String destKey, Collection<RedisSet<? extends E>> sets);
|
||||
|
||||
RedisSet<E> diffAndStore(String destKey, RedisSet<? extends E>... sets);
|
||||
RedisSet<E> diffAndStore(String destKey, Collection<RedisSet<? extends E>> sets);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.keyvalue.redis.support.collections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
@@ -28,9 +29,9 @@ import java.util.SortedSet;
|
||||
*/
|
||||
public interface RedisZSet<E> extends RedisStore<String>, Set<E> {
|
||||
|
||||
RedisZSet<E> intersectAndStore(String destKey, RedisZSet<E>... sets);
|
||||
RedisZSet<E> intersectAndStore(String destKey, Collection<RedisZSet<? extends E>> sets);
|
||||
|
||||
RedisZSet<E> unionAndStore(String destKey, RedisZSet<E>... sets);
|
||||
RedisZSet<E> unionAndStore(String destKey, Collection<RedisZSet<? extends E>> sets);
|
||||
|
||||
Set<E> range(long start, long end);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user