Merge branch 'master' of github.com:SpringSource/spring-data-keyvalue

This commit is contained in:
J. Brisbin
2010-12-08 16:14:13 -06:00
35 changed files with 387 additions and 145 deletions

View File

@@ -56,11 +56,11 @@ public class DefaultSortParameters implements SortParameters {
* @param order
* @param alphabetic
*/
public DefaultSortParameters(byte[] by, Range limit, byte[] get, Order order, Boolean alphabetic) {
public DefaultSortParameters(byte[] byPattern, Range limit, byte[] getPattern, Order order, Boolean alphabetic) {
super();
this.byPattern = by;
this.byPattern = byPattern;
this.limit = limit;
this.getPattern = get;
this.getPattern = getPattern;
this.order = order;
this.alphabetic = alphabetic;
}

View File

@@ -52,5 +52,5 @@ public interface RedisStringCommands {
Long append(byte[] key, byte[] value);
byte[] substr(byte[] key, long start, long end);
byte[] substr(byte[] key, int start, int end);
}

View File

@@ -499,7 +499,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public byte[] substr(byte[] key, long start, long end) {
public byte[] substr(byte[] key, int start, int end) {
try {
if (isQueueing()) {
transaction.substr(key, (int) start, (int) end);

View File

@@ -59,7 +59,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
/**
* Constructs a new <code>JedisConnectionFactory</code> instance.
*
* @param hostname
* @param hostName
*/
public JedisConnectionFactory(String hostName) {
Assert.hasText(hostName);
@@ -69,7 +69,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
/**
* Constructs a new <code>JedisConnectionFactory</code> instance.
*
* @param hostname
* @param hostName
* @param port
*/
public JedisConnectionFactory(String hostName, int port) {

View File

@@ -1,5 +1,5 @@
/**
* <p/>Connection package for <a href="http://github.com/xetorthio/jedis">Jedis</a> library.
* Connection package for <a href="http://github.com/xetorthio/jedis">Jedis</a> library.
*/
package org.springframework.data.keyvalue.redis.connection.jedis;

View File

@@ -330,7 +330,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public byte[] substr(byte[] key, long start, long end) {
public byte[] substr(byte[] key, int start, int end) {
try {
return jredis.substr(JredisUtils.decode(key), start, end);
} catch (RedisException ex) {

View File

@@ -1,5 +1,5 @@
/**
* <p/>Connection package for <a href="http://github.com/alphazero/jredis">JRedis</a> library.
* Connection package for <a href="http://github.com/alphazero/jredis">JRedis</a> library.
*/
package org.springframework.data.keyvalue.redis.connection.jredis;

View File

@@ -1,47 +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.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
/**
* Key operations bound to a certain value.
*
* @author Costin Leau
*/
public interface BoundKeyOperations<K> extends KeyBound<K> {
Boolean exists();
void delete();
DataType type();
void rename(K newKey);
Boolean renameIfAbsent(K newKey);
Boolean expire(long timeout, TimeUnit unit);
Boolean expireAt(Date date);
Long getExpire();
void persist();
}

View File

@@ -46,7 +46,13 @@ public interface BoundSetOperations<K, V> extends KeyBound<K> {
Set<V> members();
Boolean move(K destKey, V value);
V randomMember();
Boolean remove(Object o);
V pop();
Long size();
}

View File

@@ -24,6 +24,8 @@ import java.util.concurrent.TimeUnit;
*/
public interface BoundValueOperations<K, V> extends KeyBound<K> {
RedisOperations<K, V> getOperations();
void set(V value);
void set(V value, long timeout, TimeUnit unit);
@@ -36,4 +38,8 @@ public interface BoundValueOperations<K, V> extends KeyBound<K> {
Long increment(long delta);
Integer append(String value);
String substract(int start, int end);
}

View File

@@ -45,6 +45,8 @@ public interface BoundZSetOperations<K, V> extends KeyBound<K> {
Boolean add(V value, double score);
Double incrementScore(V value, double delta);
Long rank(Object o);
Long reverseRank(Object o);

View File

@@ -36,7 +36,7 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
*/
public DefaultBoundHashOperations(H key, RedisOperations<H, ?> operations) {
super(key);
this.ops = operations.hashOps();
this.ops = operations.getHashOps();
}
@Override

View File

@@ -36,7 +36,7 @@ class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements Bou
*/
public DefaultBoundListOperations(K key, RedisOperations<K, V> operations) {
super(key);
this.ops = operations.listOps();
this.ops = operations.getListOps();
}

View File

@@ -37,7 +37,7 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
*/
DefaultBoundSetOperations(K key, RedisOperations<K, V> operations) {
super(key);
this.ops = operations.setOps();
this.ops = operations.getSetOps();
}
@Override
@@ -47,12 +47,12 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
@Override
public Set<V> diff(Collection<K> keys) {
return ops.diff(getKey(), keys);
return ops.difference(getKey(), keys);
}
@Override
public void diffAndStore(K destKey, Collection<K> keys) {
ops.diffAndStore(getKey(), destKey, keys);
ops.differenceAndStore(getKey(), destKey, keys);
}
@Override
@@ -80,11 +80,26 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
return ops.members(getKey());
}
@Override
public Boolean move(K destKey, V value) {
return ops.move(getKey(), destKey, value);
}
@Override
public V randomMember() {
return ops.randomMember(getKey());
}
@Override
public Boolean remove(Object o) {
return ops.remove(getKey(), o);
}
@Override
public V pop() {
return ops.pop(getKey());
}
@Override
public Long size() {
return ops.size(getKey());

View File

@@ -32,7 +32,7 @@ class DefaultBoundValueOperations<K, V> extends DefaultKeyBound<K> implements Bo
*/
public DefaultBoundValueOperations(K key, RedisOperations<K, V> operations) {
super(key);
this.ops = operations.valueOps();
this.ops = operations.getValueOps();
}
@Override
@@ -50,6 +50,16 @@ class DefaultBoundValueOperations<K, V> extends DefaultKeyBound<K> implements Bo
return ops.increment(getKey(), delta);
}
@Override
public Integer append(String value) {
return ops.append(getKey(), value);
}
@Override
public String substract(int start, int end) {
return ops.substract(getKey(), start, end);
}
@Override
public void set(V value, long timeout, TimeUnit unit) {
ops.set(getKey(), value, timeout, unit);
@@ -64,4 +74,9 @@ class DefaultBoundValueOperations<K, V> extends DefaultKeyBound<K> implements Bo
public Boolean setIfAbsent(V value) {
return ops.setIfAbsent(getKey(), value);
}
@Override
public RedisOperations<K, V> getOperations() {
return ops.getOperations();
}
}

View File

@@ -36,7 +36,7 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
*/
public DefaultBoundZSetOperations(K key, RedisOperations<K, V> oeprations) {
super(key);
this.ops = oeprations.zSetOps();
this.ops = oeprations.getZSetOps();
}
@Override
@@ -44,6 +44,11 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
return ops.add(getKey(), value, score);
}
@Override
public Double incrementScore(V value, double delta) {
return ops.incrementScore(getKey(), value, delta);
}
@Override
public RedisOperations<K, V> getOperations() {
return ops.getOperations();

View File

@@ -49,5 +49,7 @@ public interface ListOperations<K, V> {
V rightPop(K key, long timeout, TimeUnit unit);
V rightPopAndLeftPush(K sourceKey, K destinationKey);
RedisOperations<K, V> getOperations();
}

View File

@@ -50,7 +50,7 @@ public interface RedisOperations<K, V> {
*/
<T> T execute(RedisCallback<T> action);
Boolean exists(K key);
Boolean hasKey(K key);
void delete(Collection<K> key);
@@ -74,31 +74,97 @@ public interface RedisOperations<K, V> {
void watch(Collection<K> keys);
void unwatch();
void multi();
void discard();
Object exec();
List<V> sort(K key, SortParameters params);
Long sort(K key, SortParameters params, K destination);
ValueOperations<K, V> valueOps();
// operation types
/**
* Returns the operations performed on simple values (or Strings in Redis terminology).
*
* @return value operations
*/
ValueOperations<K, V> getValueOps();
BoundValueOperations<K, V> forValue(K key);
/**
* Returns the operations performed on simple values (or Strings in Redis terminology)
* bound to the given key.
*
* @param key Redis key
* @return value operations bound to the given key
*/
BoundValueOperations<K, V> boundValueOps(K key);
ListOperations<K, V> listOps();
/**
* Returns the operations performed on list values.
*
* @return list operations
*/
ListOperations<K, V> getListOps();
BoundListOperations<K, V> forList(K key);
/**
* Returns the operations performed on list values bound to the given key.
*
* @param key Redis key
* @return list operations bound to the given key
*/
BoundListOperations<K, V> boundListOps(K key);
SetOperations<K, V> setOps();
/**
* Returns the operations performed on set values.
*
* @return set operations
*/
SetOperations<K, V> getSetOps();
BoundSetOperations<K, V> forSet(K key);
/**
* Returns the operations performed on set values bound to the given key.
*
* @param key Redis key
* @return set operations bound to the given key
*/
BoundSetOperations<K, V> boundSetOps(K key);
ZSetOperations<K, V> zSetOps();
/**
* Returns the operations performed on zset values (also known as sorted sets).
*
* @return zset operations
*/
ZSetOperations<K, V> getZSetOps();
BoundZSetOperations<K, V> forZSet(K key);
/**
* Returns the operations performed on zset values (also known as sorted sets)
* bound to the given key.
*
* @param key Redis key
* @return zset operations bound to the given key.
*/
BoundZSetOperations<K, V> boundZSetOps(K key);
<HK, HV> HashOperations<K, HK, HV> hashOps();
/**
* Returns the operations performed on hash values.
*
* @param <HK> hash key (or field) type
* @param <HV> hash value type
* @return hash operations
*/
<HK, HV> HashOperations<K, HK, HV> getHashOps();
<HK, HV> BoundHashOperations<K, HK, HV> forHash(K key);
/**
* Returns the operations performed on hash values bound to the given key.
*
* @param <HK> hash key (or field) type
* @param <HV> hash value type
* @param key Redis key
* @return hash operations bound to the given key.
*/
<HK, HV> BoundHashOperations<K, HK, HV> boundHashOps(K key);
}

View File

@@ -71,6 +71,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
private RedisSerializer valueSerializer = new SimpleRedisSerializer();
private RedisSerializer hashKeySerializer = new SimpleRedisSerializer();
private RedisSerializer hashValueSerializer = new SimpleRedisSerializer();
private RedisSerializer stringSerializer = new StringRedisSerializer();
// cache singleton objects (where possible)
private final ValueOperations<K, V> valueOps = new DefaultValueOperations();
private final ListOperations<K, V> listOps = new DefaultListOperations();
private final SetOperations<K, V> setOps = new DefaultSetOperations();
private final ZSetOperations<K, V> zSetOps = new DefaultZSetOperations();
/**
* Constructs a new <code>RedisTemplate</code> instance.
@@ -199,6 +206,16 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
this.hashValueSerializer = hashValueSerializer;
}
/**
* Sets the string value serializer to be used by this template (when the arguments or return types
* are always strings). Defaults to {@link StringRedisSerializer}.
*
* @see ValueOperations#substract(Object, int, int)
* @param stringSerializer The stringValueSerializer to set.
*/
public void setStringSerializer(RedisSerializer<?> stringSerializer) {
this.stringSerializer = stringSerializer;
}
/**
* Invocation handler that suppresses close calls on JDO PersistenceManagers.
@@ -244,6 +261,11 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return (key != null ? keySerializer.serialize(key) : null);
}
@SuppressWarnings("unchecked")
private byte[] rawString(String key) {
return (key != null ? stringSerializer.serialize(key) : null);
}
@SuppressWarnings("unchecked")
private <T> byte[] rawValue(T value) {
return (value != null ? valueSerializer.serialize(value) : null);
@@ -333,6 +355,11 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return (V) deserialize(value, valueSerializer);
}
@SuppressWarnings("unchecked")
private String deserializeString(byte[] value) {
return (String) deserialize(value, stringSerializer);
}
@SuppressWarnings( { "unchecked", "unused" })
private <HK> HK deserializeHashKey(byte[] value) {
return (HK) deserialize(value, hashKeySerializer);
@@ -350,6 +377,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return serializer.deserialize(value);
}
private static boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
@@ -376,6 +404,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
// RedisOperations
//
@Override
public Object exec() {
return execute(new RedisCallback<Object>() {
@@ -401,7 +430,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Boolean exists(K key) {
public Boolean hasKey(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Boolean>() {
@@ -561,13 +590,64 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public BoundValueOperations<K, V> forValue(K key) {
public void multi() {
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.multi();
return null;
}
}, true);
}
@Override
public void discard() {
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.discard();
return null;
}
}, true);
}
@Override
public void watch(Collection<K> keys) {
final byte[][] rawKeys = rawKeys(keys);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.watch(rawKeys);
return null;
}
}, true);
}
@Override
public void unwatch() {
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.unwatch();
return null;
}
}, true);
}
//
// Value Ops
//
@Override
public BoundValueOperations<K, V> boundValueOps(K key) {
return new DefaultBoundValueOperations<K, V>(key, this);
}
@Override
public ValueOperations<K, V> valueOps() {
return new DefaultValueOperations();
public ValueOperations<K, V> getValueOps() {
return valueOps;
}
private class DefaultValueOperations implements ValueOperations<K, V> {
@@ -618,6 +698,33 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public Integer append(K key, String value) {
final byte[] rawKey = rawKey(key);
final byte[] rawString = rawString(value);
return execute(new RedisCallback<Integer>() {
@Override
public Integer doInRedis(RedisConnection connection) {
return connection.append(rawKey, rawString).intValue();
}
}, true);
}
@Override
public String substract(K key, final int start, final int end) {
final byte[] rawKey = rawKey(key);
byte[] rawReturn = execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) {
return connection.substr(rawKey, start, end);
}
}, true);
return deserializeString(rawReturn);
}
@Override
public Collection<V> multiGet(Collection<K> keys) {
if (keys.isEmpty()) {
@@ -722,42 +829,25 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
}, true);
}
@Override
public RedisOperations<K, V> getOperations() {
return RedisTemplate.this;
}
}
@Override
public ListOperations<K, V> listOps() {
return new DefaultListOperations();
public ListOperations<K, V> getListOps() {
return listOps;
}
@Override
public BoundListOperations<K, V> forList(K key) {
public BoundListOperations<K, V> boundListOps(K key) {
return new DefaultBoundListOperations<K, V>(key, this);
}
@Override
public void multi() {
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.multi();
return null;
}
}, true);
}
@Override
public void watch(Collection<K> keys) {
final byte[][] rawKeys = rawKeys(keys);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.watch(rawKeys);
return null;
}
}, true);
}
//
// List operations
@@ -798,7 +888,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Long leftPush(K key, V value) {
final byte[] rawKey = rawKey(key);
@@ -879,6 +968,18 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public V rightPopAndLeftPush(K sourceKey, K destinationKey) {
final byte[] rawDestKey = rawKey(destinationKey);
return execute(new ValueDeserializingRedisCallback(sourceKey) {
@Override
protected byte[] inRedis(byte[] rawSourceKey, RedisConnection connection) {
return connection.rPopLPush(rawSourceKey, rawDestKey);
}
}, true);
}
@Override
public void set(K key, final long index, V value) {
final byte[] rawValue = rawValue(value);
@@ -913,13 +1014,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
//
@Override
public BoundSetOperations<K, V> forSet(K key) {
public BoundSetOperations<K, V> boundSetOps(K key) {
return new DefaultBoundSetOperations<K, V>(key, this);
}
@Override
public SetOperations<K, V> setOps() {
return new DefaultSetOperations();
public SetOperations<K, V> getSetOps() {
return setOps;
}
private class DefaultSetOperations implements SetOperations<K, V> {
@@ -937,7 +1038,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Set<V> diff(final K key, final Collection<K> keys) {
public Set<V> difference(final K key, final Collection<K> keys) {
final byte[][] rawKeys = rawKeys(key, keys);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
@Override
@@ -950,7 +1051,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public void diffAndStore(final K key, K destKey, final Collection<K> keys) {
public void differenceAndStore(final K key, K destKey, final Collection<K> keys) {
final byte[][] rawKeys = rawKeys(key, keys);
final byte[] rawDestKey = rawKey(destKey);
execute(new RedisCallback<Object>() {
@@ -1018,6 +1119,31 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return deserializeValues(rawValues, Set.class);
}
@Override
public Boolean move(K key, K destKey, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawDestKey = rawKey(destKey);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) {
return connection.sMove(rawKey, rawDestKey, rawValue);
}
}, true);
}
@Override
public V randomMember(K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.randomKey();
}
}, true);
}
@Override
public Boolean remove(K key, Object o) {
final byte[] rawKey = rawKey(key);
@@ -1030,6 +1156,16 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public V pop(K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.sPop(rawKey);
}
}, true);
}
@Override
public Long size(K key) {
final byte[] rawKey = rawKey(key);
@@ -1073,13 +1209,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
//
@Override
public BoundZSetOperations<K, V> forZSet(K key) {
public BoundZSetOperations<K, V> boundZSetOps(K key) {
return new DefaultBoundZSetOperations<K, V>(key, this);
}
@Override
public ZSetOperations<K, V> zSetOps() {
return new DefaultZSetOperations();
public ZSetOperations<K, V> getZSetOps() {
return zSetOps;
}
private class DefaultZSetOperations implements ZSetOperations<K, V> {
@@ -1097,6 +1233,19 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public Double incrementScore(K key, V value, final double delta) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Double>() {
@Override
public Double doInRedis(RedisConnection connection) {
return connection.zIncrBy(rawKey, delta, rawValue);
}
}, true);
}
@Override
public RedisOperations<K, V> getOperations() {
return RedisTemplate.this;
@@ -1267,12 +1416,12 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
//
@Override
public <HK, HV> BoundHashOperations<K, HK, HV> forHash(K key) {
public <HK, HV> BoundHashOperations<K, HK, HV> boundHashOps(K key) {
return new DefaultBoundHashOperations<K, HK, HV>(key, this);
}
@Override
public <HK, HV> HashOperations<K, HK, HV> hashOps() {
public <HK, HV> HashOperations<K, HK, HV> getHashOps() {
return new DefaultHashOperations<HK, HV>();
}

View File

@@ -26,11 +26,9 @@ import java.util.Set;
*/
public interface SetOperations<K, V> {
Set<V> diff(K key, Collection<K> keys);
Set<V> difference(K key, Collection<K> keys);
void diffAndStore(K key, K destKey, Collection<K> keys);
RedisOperations<K, V> getOperations();
void differenceAndStore(K key, K destKey, Collection<K> keys);
Set<V> intersect(K key, Collection<K> keys);
@@ -46,8 +44,15 @@ public interface SetOperations<K, V> {
Set<V> members(K key);
Boolean move(K key, K destKey, V value);
V randomMember(K key);
Boolean remove(K key, Object o);
V pop(K key);
Long size(K key);
RedisOperations<K, V> getOperations();
}

View File

@@ -43,4 +43,10 @@ public interface ValueOperations<K, V> {
Collection<V> multiGet(Collection<K> keys);
Long increment(K key, long delta);
Integer append(K key, String value);
String substract(K key, int start, int end);
RedisOperations<K, V> getOperations();
}

View File

@@ -28,20 +28,18 @@ public interface ZSetOperations<K, V> {
void intersectAndStore(K key, K destKey, Collection<K> keys);
void unionAndStore(K key, K destKey, Collection<K> keys);
Set<V> range(K key, long start, long end);
Set<V> rangeByScore(K key, double min, double max);
Set<V> reverseRange(K key, long start, long end);
void removeRange(K key, long start, long end);
void removeRangeByScore(K key, double min, double max);
void unionAndStore(K key, K destKey, Collection<K> keys);
Boolean add(K key, V value, double score);
Double incrementScore(K key, V value, double delta);
Long rank(K key, Object o);
Long reverseRank(K key, Object o);
@@ -50,6 +48,10 @@ public interface ZSetOperations<K, V> {
Boolean remove(K key, Object o);
void removeRange(K key, long start, long end);
void removeRangeByScore(K key, double min, double max);
Long size(K key);
RedisOperations<K, V> getOperations();

View File

@@ -0,0 +1,8 @@
/**
* Root package for integrating <a href="http://code.google.com/p/redis/">Redis</a> with Spring concepts.
* <p/>
* Provides Redis specific exception hierarchy on top of the {@code org.springframework.dao} package.
*
*/
package org.springframework.data.keyvalue.redis;

View File

@@ -54,7 +54,7 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound
*/
public RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer> operations, int initialValue) {
this.key = redisCounter;
this.operations = operations.valueOps();
this.operations = operations.getValueOps();
this.generalOps = operations;
this.operations.set(redisCounter, initialValue);
}

View File

@@ -54,7 +54,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound<St
*/
public RedisAtomicLong(String redisCounter, RedisOperations<String, Long> operations, long initialValue) {
this.key = redisCounter;
this.operations = operations.valueOps();
this.operations = operations.getValueOps();
this.operations.set(redisCounter, initialValue);
}

View File

@@ -67,7 +67,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
* @param operations
*/
public DefaultRedisList(String key, RedisOperations<String, E> operations) {
this(operations.forList(key));
this(operations.boundListOps(key));
}
/**

View File

@@ -67,7 +67,7 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
* @param operations
*/
public DefaultRedisMap(String key, RedisOperations<String, ?> operations) {
this.hashOps = operations.forHash(key);
this.hashOps = operations.boundHashOps(key);
}
/**

View File

@@ -53,7 +53,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
*/
public DefaultRedisSet(String key, RedisOperations<String, E> operations) {
super(key, operations);
boundSetOps = operations.forSet(key);
boundSetOps = operations.boundSetOps(key);
}
/**
@@ -74,7 +74,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
@Override
public RedisSet<E> diffAndStore(String destKey, Collection<? extends RedisSet<?>> sets) {
boundSetOps.diffAndStore(destKey, CollectionUtils.extractKeys(sets));
return new DefaultRedisSet<E>(boundSetOps.getOperations().forSet(destKey));
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
}
@Override
@@ -85,7 +85,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
@Override
public RedisSet<E> intersectAndStore(String destKey, Collection<? extends RedisSet<?>> sets) {
boundSetOps.intersectAndStore(destKey, CollectionUtils.extractKeys(sets));
return new DefaultRedisSet<E>(boundSetOps.getOperations().forSet(destKey));
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
}
@Override
@@ -96,7 +96,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
@Override
public RedisSet<E> unionAndStore(String destKey, Collection<? extends RedisSet<?>> sets) {
boundSetOps.unionAndStore(destKey, CollectionUtils.extractKeys(sets));
return new DefaultRedisSet<E>(boundSetOps.getOperations().forSet(destKey));
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
}
@Override

View File

@@ -64,7 +64,7 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
*/
public DefaultRedisZSet(String key, RedisOperations<String, E> operations, double defaultScore) {
super(key, operations);
boundZSetOps = operations.forZSet(key);
boundZSetOps = operations.boundZSetOps(key);
this.defaultScore = defaultScore;
}
@@ -93,7 +93,7 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
@Override
public RedisZSet<E> intersectAndStore(String destKey, Collection<? extends RedisZSet<?>> sets) {
boundZSetOps.intersectAndStore(destKey, CollectionUtils.extractKeys(sets));
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore());
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
@Override
@@ -126,7 +126,7 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
@Override
public RedisZSet<E> unionAndStore(String destKey, Collection<? extends RedisZSet<?>> sets) {
boundZSetOps.unionAndStore(destKey, CollectionUtils.extractKeys(sets));
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().forZSet(destKey), getDefaultScore());
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
}
@Override

View File

@@ -22,8 +22,9 @@ import java.util.Set;
import java.util.SortedSet;
/**
* Redis ZSet contract. Acts as a {@link SortedSet} based on the given priorities. Since using a {@link Comparator}
* does not apply, a ZSet implements the {@link SortedSet} methods where applicable.
* Redis ZSet (or sorted set (by weight)). Acts as a {@link SortedSet} based on the given priorities or weights associated with each item.
* <p/>
* Since using a {@link Comparator} does not apply, a ZSet implements the {@link SortedSet} methods where applicable.
*
* @author Costin Leau
*/

View File

@@ -2,10 +2,11 @@
* Package providing implementations for most of the {@code java.util} collections on top of Redis.
* <p/>
* For indexed collections, such as {@link java.util.List}, {@link java.util.Queue} or {@link java.util.Deque}
* consider {@link RedisList}.<p/>
* For collections without duplicates the obvious candidate is {@link RedisSet}. Use {@link RedisZSet} if a
* consider {@link org.springframework.data.keyvalue.redis.support.collections.RedisList}.<p/>
* For collections without duplicates the obvious candidate is {@link org.springframework.data.keyvalue.redis.support.collections.RedisSet}. Use
* {@link org.springframework.data.keyvalue.redis.support.collections.RedisZSet} if a
* certain order is required.</p/>
* Lastly, for key/value associations {@link RedisMap} providing a Map-like abstraction on top of a Redis hash.
* Lastly, for key/value associations {@link org.springframework.data.keyvalue.redis.support.collections.RedisMap} providing a Map-like abstraction on top of a Redis hash.
*/
package org.springframework.data.keyvalue.redis.support.collections;

View File

@@ -279,7 +279,7 @@ public abstract class AbstractRedisListTests<T> extends AbstractRedisCollectionT
@Test
public void testCappedCollection() throws Exception {
RedisList<T> cappedList = new DefaultRedisList<T>(template.forList(collection.getKey() + ":capped"), 1);
RedisList<T> cappedList = new DefaultRedisList<T>(template.boundListOps(collection.getKey() + ":capped"), 1);
T first = getT();
cappedList.offer(first);
assertEquals(1, cappedList.size());

View File

@@ -164,7 +164,7 @@ public abstract class AbstractRedisMapTests<K, V> {
@Test
public void testNotEquals() {
RedisOperations<String, ?> ops = map.getOperations();
RedisStore newInstance = new DefaultRedisMap<K, V>(ops.<K, V> forHash(map.getKey() + ":new"));
RedisStore newInstance = new DefaultRedisMap<K, V>(ops.<K, V> boundHashOps(map.getKey() + ":new"));
assertFalse(map.equals(newInstance));
assertFalse(newInstance.equals(map));
}

View File

@@ -59,7 +59,7 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
}
private RedisSet<T> createSetFor(String key) {
return new DefaultRedisSet<T>((BoundSetOperations<String, T>) set.getOperations().forSet(key));
return new DefaultRedisSet<T>((BoundSetOperations<String, T>) set.getOperations().boundSetOps(key));
}
@Test

View File

@@ -183,7 +183,7 @@ public abstract class AbstractRedisZSetTest<T> extends AbstractRedisCollectionTe
}
private RedisZSet<T> createZSetFor(String key) {
return new DefaultRedisZSet<T>((BoundZSetOperations<String, T>) zSet.getOperations().forZSet(key));
return new DefaultRedisZSet<T>((BoundZSetOperations<String, T>) zSet.getOperations().boundZSetOps(key));
}
@Test