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

This commit is contained in:
J. Brisbin
2010-12-02 09:56:43 -06:00
47 changed files with 1177 additions and 465 deletions

View File

@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue-dist</artifactId>
<name>Spring Datastore Key-Value Distribution</name>
<name>Spring Data Key-Value Distribution</name>
<version>1.0.0.BUILD-SNAPSHOT</version>
<packaging>pom</packaging>

View File

@@ -382,6 +382,19 @@
<junitArtifactName>junit:junit</junitArtifactName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
<!--
<useDefaultManifestFile>true</useDefaultManifestFile>
-->
</archive>
</configuration>
</plugin>
<!--
<plugin>
<artifactId>maven-source-plugin</artifactId>
@@ -449,12 +462,18 @@
<executions>
<execution>
<id>bundlor</id>
<phase>compile</phase>
<goals>
<goal>bundlor</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
</plugin>
<plugin>
<groupId>org.spockframework</groupId>
<artifactId>spock-maven</artifactId>

View File

@@ -13,7 +13,7 @@
<properties>
<jredis.ver>02112010</jredis.ver>
<jedis.ver>1.4.0</jedis.ver>
<jedis.ver>1.5.0-RC1</jedis.ver>
</properties>
<dependencies>
@@ -115,12 +115,6 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.commons</groupId>
<artifactId>spring-commons-serializer</artifactId>
<version>1.0.0.M1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>

View File

@@ -28,7 +28,7 @@ public interface RedisCommands extends RedisTxCommands, RedisStringCommands, Red
Boolean exists(byte[] key);
Integer del(byte[]... keys);
Long del(byte[]... keys);
DataType type(byte[] key);
@@ -40,13 +40,15 @@ public interface RedisCommands extends RedisTxCommands, RedisStringCommands, Red
Boolean renameNX(byte[] oldName, byte[] newName);
Integer dbSize();
Long dbSize();
Boolean expire(byte[] key, int seconds);
Boolean expire(byte[] key, long seconds);
Boolean expireAt(byte[] key, long unixTime);
Boolean persist(byte[] key);
Integer ttl(byte[] key);
Long ttl(byte[] key);
void select(int dbIndex);

View File

@@ -37,13 +37,13 @@ public interface RedisHashCommands {
void hMSet(byte[] key, Map<byte[], byte[]> hashes);
Integer hIncrBy(byte[] key, byte[] field, int delta);
Long hIncrBy(byte[] key, byte[] field, long delta);
Boolean hExists(byte[] key, byte[] field);
Boolean hDel(byte[] key, byte[] field);
Integer hLen(byte[] key);
Long hLen(byte[] key);
Set<byte[]> hKeys(byte[] key);

View File

@@ -25,21 +25,21 @@ import java.util.List;
*/
public interface RedisListCommands {
Integer rPush(byte[] key, byte[] value);
Long rPush(byte[] key, byte[] value);
Integer lPush(byte[] key, byte[] value);
Long lPush(byte[] key, byte[] value);
Integer lLen(byte[] key);
Long lLen(byte[] key);
List<byte[]> lRange(byte[] key, int start, int end);
List<byte[]> lRange(byte[] key, long start, long end);
void lTrim(byte[] key, int start, int end);
void lTrim(byte[] key, long start, long end);
byte[] lIndex(byte[] key, int index);
byte[] lIndex(byte[] key, long index);
void lSet(byte[] key, int index, byte[] value);
void lSet(byte[] key, long index, byte[] value);
Integer lRem(byte[] key, int count, byte[] value);
Long lRem(byte[] key, long count, byte[] value);
byte[] lPop(byte[] key);

View File

@@ -33,7 +33,7 @@ public interface RedisSetCommands {
Boolean sMove(byte[] srcKey, byte[] destKey, byte[] value);
Integer sCard(byte[] key);
Long sCard(byte[] key);
Boolean sIsMember(byte[] key, byte[] value);

View File

@@ -36,21 +36,21 @@ public interface RedisStringCommands {
Boolean setNX(byte[] key, byte[] value);
void setEx(byte[] key, int seconds, byte[] value);
void setEx(byte[] key, long seconds, byte[] value);
void mSet(Map<byte[], byte[]> tuple);
void mSetNX(Map<byte[], byte[]> tuple);
Integer incr(byte[] key);
Long incr(byte[] key);
Integer incrBy(byte[] key, int value);
Long incrBy(byte[] key, long value);
Integer decr(byte[] key);
Long decr(byte[] key);
Integer decrBy(byte[] key, int value);
Long decrBy(byte[] key, long value);
Integer append(byte[] key, byte[] value);
Long append(byte[] key, byte[] value);
byte[] substr(byte[] key, int start, int end);
byte[] substr(byte[] key, long start, long end);
}

View File

@@ -42,41 +42,41 @@ public interface RedisZSetCommands {
Double zIncrBy(byte[] key, double increment, byte[] value);
Integer zRank(byte[] key, byte[] value);
Long zRank(byte[] key, byte[] value);
Integer zRevRank(byte[] key, byte[] value);
Long zRevRank(byte[] key, byte[] value);
Set<byte[]> zRange(byte[] key, int start, int end);
Set<byte[]> zRange(byte[] key, long start, long end);
Set<Tuple> zRangeWithScore(byte[] key, int start, int end);
Set<Tuple> zRangeWithScore(byte[] key, long start, long end);
Set<byte[]> zRevRange(byte[] key, int start, int end);
Set<byte[]> zRevRange(byte[] key, long start, long end);
Set<Tuple> zRevRangeWithScore(byte[] key, int start, int end);
Set<Tuple> zRevRangeWithScore(byte[] key, long start, long end);
Set<byte[]> zRangeByScore(byte[] key, double min, double max);
Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max);
Set<byte[]> zRangeByScore(byte[] key, double min, double max, int offset, int count);
Set<byte[]> zRangeByScore(byte[] key, double min, double max, long offset, long count);
Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max, int offset, int count);
Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max, long offset, long count);
Integer zCount(byte[] key, double min, double max);
Long zCount(byte[] key, double min, double max);
Integer zCard(byte[] key);
Long zCard(byte[] key);
Double zScore(byte[] key, byte[] value);
Integer zRemRange(byte[] key, int start, int end);
Long zRemRange(byte[] key, long start, long end);
Integer zRemRangeByScore(byte[] key, double min, double max);
Long zRemRangeByScore(byte[] key, double min, double max);
Integer zUnionStore(byte[] destKey, byte[]... sets);
Long zUnionStore(byte[] destKey, byte[]... sets);
Integer zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets);
Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets);
Integer zInterStore(byte[] destKey, byte[]... sets);
Long zInterStore(byte[] destKey, byte[]... sets);
Integer zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets);
Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets);
}

View File

@@ -108,7 +108,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer dbSize() {
public Long dbSize() {
try {
if (isQueueing()) {
transaction.dbSize();
@@ -134,7 +134,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer del(byte[]... keys) {
public Long del(byte[]... keys) {
try {
if (isQueueing()) {
transaction.del(keys);
@@ -178,10 +178,10 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Boolean expire(byte[] key, int seconds) {
public Boolean expire(byte[] key, long seconds) {
try {
if (isQueueing()) {
transaction.expire(key, seconds);
transaction.expire(key, (int) seconds);
return null;
}
return (jedis.expire(key, (int) seconds) == 1);
@@ -190,6 +190,19 @@ public class JedisConnection implements RedisConnection {
}
}
@Override
public Boolean expireAt(byte[] key, long unixTime) {
try {
if (isQueueing()) {
transaction.expireAt(key, unixTime);
return null;
}
return (jedis.expireAt(key, unixTime) == 1);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Collection<byte[]> keys(byte[] pattern) {
try {
@@ -276,7 +289,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer ttl(byte[] key) {
public Long ttl(byte[] key) {
try {
if (isQueueing()) {
transaction.ttl(key);
@@ -368,7 +381,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer append(byte[] key, byte[] value) {
public Long append(byte[] key, byte[] value) {
try {
if (isQueueing()) {
transaction.append(key, value);
@@ -418,12 +431,12 @@ public class JedisConnection implements RedisConnection {
}
@Override
public void setEx(byte[] key, int time, byte[] value) {
public void setEx(byte[] key, long time, byte[] value) {
try {
if (isQueueing()) {
transaction.setex(key, time, value);
transaction.setex(key, (int) time, value);
}
jedis.setex(key, time, value);
jedis.setex(key, (int) time, value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
@@ -442,20 +455,20 @@ public class JedisConnection implements RedisConnection {
}
@Override
public byte[] substr(byte[] key, int start, int end) {
public byte[] substr(byte[] key, long start, long end) {
try {
if (isQueueing()) {
transaction.substr(key, start, end);
transaction.substr(key, (int) start, (int) end);
return null;
}
return jedis.substr(key, start, end);
return jedis.substr(key, (int) start, (int) end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer decr(byte[] key) {
public Long decr(byte[] key) {
try {
if (isQueueing()) {
transaction.decr(key);
@@ -468,20 +481,20 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer decrBy(byte[] key, int value) {
public Long decrBy(byte[] key, long value) {
try {
if (isQueueing()) {
transaction.decrBy(key, value);
transaction.decrBy(key, (int) value);
return null;
}
return jedis.decrBy(key, value);
return jedis.decrBy(key, (int) value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer incr(byte[] key) {
public Long incr(byte[] key) {
try {
if (isQueueing()) {
transaction.incr(key);
@@ -494,13 +507,13 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer incrBy(byte[] key, int value) {
public Long incrBy(byte[] key, long value) {
try {
if (isQueueing()) {
transaction.incrBy(key, value);
transaction.incrBy(key, (int) value);
return null;
}
return jedis.incrBy(key, value);
return jedis.incrBy(key, (int) value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
@@ -512,7 +525,7 @@ public class JedisConnection implements RedisConnection {
@Override
public Integer lPush(byte[] key, byte[] value) {
public Long lPush(byte[] key, byte[] value) {
try {
if (isQueueing()) {
transaction.lpush(key, value);
@@ -525,7 +538,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer rPush(byte[] key, byte[] value) {
public Long rPush(byte[] key, byte[] value) {
try {
if (isQueueing()) {
transaction.rpush(key, value);
@@ -562,20 +575,20 @@ public class JedisConnection implements RedisConnection {
}
@Override
public byte[] lIndex(byte[] key, int index) {
public byte[] lIndex(byte[] key, long index) {
try {
if (isQueueing()) {
transaction.lindex(key, index);
transaction.lindex(key, (int) index);
return null;
}
return jedis.lindex(key, index);
return jedis.lindex(key, (int) index);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer lLen(byte[] key) {
public Long lLen(byte[] key) {
try {
if (isQueueing()) {
transaction.llen(key);
@@ -601,50 +614,50 @@ public class JedisConnection implements RedisConnection {
}
@Override
public List<byte[]> lRange(byte[] key, int start, int end) {
public List<byte[]> lRange(byte[] key, long start, long end) {
try {
if (isQueueing()) {
transaction.lrange(key, start, end);
transaction.lrange(key, (int) start, (int) end);
return null;
}
return jedis.lrange(key, start, end);
return jedis.lrange(key, (int) start, (int) end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer lRem(byte[] key, int count, byte[] value) {
public Long lRem(byte[] key, long count, byte[] value) {
try {
if (isQueueing()) {
transaction.lrem(key, count, value);
transaction.lrem(key, (int) count, value);
return null;
}
return jedis.lrem(key, count, value);
return jedis.lrem(key, (int) count, value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public void lSet(byte[] key, int index, byte[] value) {
public void lSet(byte[] key, long index, byte[] value) {
try {
if (isQueueing()) {
transaction.lset(key, index, value);
transaction.lset(key, (int) index, value);
}
jedis.lset(key, index, value);
jedis.lset(key, (int) index, value);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public void lTrim(byte[] key, int start, int end) {
public void lTrim(byte[] key, long start, long end) {
try {
if (isQueueing()) {
transaction.ltrim(key, start, end);
transaction.ltrim(key, (int) start, (int) end);
}
jedis.ltrim(key, start, end);
jedis.ltrim(key, (int) start, (int) end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
@@ -695,7 +708,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer sCard(byte[] key) {
public Long sCard(byte[] key) {
try {
if (isQueueing()) {
transaction.scard(key);
@@ -878,7 +891,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer zCard(byte[] key) {
public Long zCard(byte[] key) {
try {
if (isQueueing()) {
transaction.zcard(key);
@@ -891,7 +904,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer zCount(byte[] key, double min, double max) {
public Long zCount(byte[] key, double min, double max) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
@@ -916,7 +929,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
@@ -930,7 +943,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer zInterStore(byte[] destKey, byte[]... sets) {
public Long zInterStore(byte[] destKey, byte[]... sets) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
@@ -942,26 +955,26 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Set<byte[]> zRange(byte[] key, int start, int end) {
public Set<byte[]> zRange(byte[] key, long start, long end) {
try {
if (isQueueing()) {
transaction.zrange(key, start, end);
transaction.zrange(key, (int) start, (int) end);
return null;
}
return jedis.zrange(key, start, end);
return jedis.zrange(key, (int) start, (int) end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Set<Tuple> zRangeWithScore(byte[] key, int start, int end) {
public Set<Tuple> zRangeWithScore(byte[] key, long start, long end) {
try {
if (isQueueing()) {
transaction.zrangeWithScores(key, start, end);
transaction.zrangeWithScores(key, (int) start, (int) end);
return null;
}
return JedisUtils.convertJedisTuple(jedis.zrangeWithScores(key, start, end));
return JedisUtils.convertJedisTuple(jedis.zrangeWithScores(key, (int) start, (int) end));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
@@ -992,44 +1005,44 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Set<Tuple> zRevRangeWithScore(byte[] key, int start, int end) {
public Set<Tuple> zRevRangeWithScore(byte[] key, long start, long end) {
try {
if (isQueueing()) {
transaction.zrangeWithScores(key, start, end);
transaction.zrangeWithScores(key, (int) start, (int) end);
return null;
}
return JedisUtils.convertJedisTuple(jedis.zrangeByScoreWithScores(key, start, end));
return JedisUtils.convertJedisTuple(jedis.zrangeByScoreWithScores(key, (int) start, (int) end));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Set<byte[]> zRangeByScore(byte[] key, double min, double max, int offset, int count) {
public Set<byte[]> zRangeByScore(byte[] key, double min, double max, long offset, long count) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
}
return jedis.zrangeByScore(key, min, max, offset, count);
return jedis.zrangeByScore(key, min, max, (int) offset, (int) count);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max, int offset, int count) {
public Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max, long offset, long count) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
}
return JedisUtils.convertJedisTuple(jedis.zrangeByScoreWithScores(key, min, max, offset, count));
return JedisUtils.convertJedisTuple(jedis.zrangeByScoreWithScores(key, min, max, (int) offset, (int) count));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer zRank(byte[] key, byte[] value) {
public Long zRank(byte[] key, byte[] value) {
try {
if (isQueueing()) {
transaction.zrank(key, value);
@@ -1055,19 +1068,19 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer zRemRange(byte[] key, int start, int end) {
public Long zRemRange(byte[] key, long start, long end) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
}
return jedis.zremrangeByRank(key, start, end);
return jedis.zremrangeByRank(key, (int) start, (int) end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer zRemRangeByScore(byte[] key, double min, double max) {
public Long zRemRangeByScore(byte[] key, double min, double max) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
@@ -1079,20 +1092,20 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Set<byte[]> zRevRange(byte[] key, int start, int end) {
public Set<byte[]> zRevRange(byte[] key, long start, long end) {
try {
if (isQueueing()) {
transaction.zrevrange(key, start, end);
transaction.zrevrange(key, (int) start, (int) end);
return null;
}
return jedis.zrevrange(key, start, end);
return jedis.zrevrange(key, (int) start, (int) end);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public Integer zRevRank(byte[] key, byte[] value) {
public Long zRevRank(byte[] key, byte[] value) {
try {
if (isQueueing()) {
transaction.zrevrank(key, value);
@@ -1118,7 +1131,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
@@ -1132,7 +1145,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer zUnionStore(byte[] destKey, byte[]... sets) {
public Long zUnionStore(byte[] destKey, byte[]... sets) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
@@ -1226,13 +1239,13 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer hIncrBy(byte[] key, byte[] field, int delta) {
public Long hIncrBy(byte[] key, byte[] field, long delta) {
try {
if (isQueueing()) {
transaction.hincrBy(key, field, delta);
transaction.hincrBy(key, field, (int) delta);
return null;
}
return jedis.hincrBy(key, field, delta);
return jedis.hincrBy(key, field, (int) delta);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
@@ -1252,7 +1265,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer hLen(byte[] key) {
public Long hLen(byte[] key) {
try {
if (isQueueing()) {
transaction.hlen(key);

View File

@@ -16,13 +16,13 @@
package org.springframework.data.keyvalue.redis.connection.jedis;
import java.util.concurrent.TimeoutException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
import org.springframework.util.Assert;
@@ -99,8 +99,8 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
return pool.getResource();
}
return new Jedis(getShardInfo());
} catch (TimeoutException ex) {
throw JedisUtils.convertJedisAccessException(ex);
} catch (Exception ex) {
throw new DataAccessResourceFailureException("Cannot get Jedis connection", ex);
}
}
@@ -115,15 +115,18 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
if (usePool) {
int size = getPoolSize();
pool = new JedisPool(shardInfo);
pool.setResourcesNumber(size);
pool.init();
pool = new JedisPool(new GenericObjectPool.Config(), shardInfo.getHost(), shardInfo.getPort(),
shardInfo.getTimeout(), shardInfo.getPassword());
}
}
public void destroy() {
if (usePool && pool != null) {
pool.destroy();
try {
pool.destroy();
} catch (Exception ex) {
log.warn("Cannot properly close Jedis pool", ex);
}
pool = null;
}
}

View File

@@ -70,8 +70,8 @@ public abstract class JedisUtils {
return status != null && (OK_CODE.equals(status) || OK_MULTI_CODE.equals(status));
}
static Boolean convertCodeReply(Integer code) {
return (code != null ? code == 1 : null);
static Boolean convertCodeReply(Number code) {
return (code != null ? code.intValue() == 1 : null);
}
static Set<Tuple> convertJedisTuple(Set<redis.clients.jedis.Tuple> tuples) {

View File

@@ -76,9 +76,9 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer dbSize() {
public Long dbSize() {
try {
return Integer.valueOf((int) jredis.dbsize());
return jredis.dbsize();
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -94,9 +94,9 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer del(byte[]... keys) {
public Long del(byte[]... keys) {
try {
return Integer.valueOf((int) jredis.del(JredisUtils.convertMultiple(charset, keys)));
return jredis.del(JredisUtils.convertMultiple(charset, keys));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -126,9 +126,18 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Boolean expire(byte[] key, int seconds) {
public Boolean expire(byte[] key, long seconds) {
try {
return jredis.expire(JredisUtils.convert(charset, key), seconds);
return jredis.expire(JredisUtils.convert(charset, key), (int) seconds);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Boolean expireAt(byte[] key, long unixTime) {
try {
return jredis.expireat(JredisUtils.convert(charset, key), unixTime);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -186,9 +195,9 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer ttl(byte[] key) {
public Long ttl(byte[] key) {
try {
return Integer.valueOf((int) jredis.ttl(JredisUtils.convert(charset, key)));
return jredis.ttl(JredisUtils.convert(charset, key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -245,9 +254,9 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer append(byte[] key, byte[] value) {
public Long append(byte[] key, byte[] value) {
try {
return Integer.valueOf((int) jredis.append(JredisUtils.convert(charset, key), value));
return jredis.append(JredisUtils.convert(charset, key), value);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -281,7 +290,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public void setEx(byte[] key, int seconds, byte[] value) {
public void setEx(byte[] key, long seconds, byte[] value) {
throw new UnsupportedOperationException();
}
@@ -295,7 +304,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public byte[] substr(byte[] key, int start, int end) {
public byte[] substr(byte[] key, long start, long end) {
try {
return jredis.substr(JredisUtils.convert(charset, key), (long) start, (long) end);
} catch (RedisException ex) {
@@ -304,36 +313,36 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer decr(byte[] key) {
public Long decr(byte[] key) {
try {
return (int) jredis.decr(JredisUtils.convert(charset, key));
return jredis.decr(JredisUtils.convert(charset, key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Integer decrBy(byte[] key, int value) {
public Long decrBy(byte[] key, long value) {
try {
return (int) jredis.decrby(JredisUtils.convert(charset, key), value);
return jredis.decrby(JredisUtils.convert(charset, key), (int) value);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Integer incr(byte[] key) {
public Long incr(byte[] key) {
try {
return (int) jredis.incr(JredisUtils.convert(charset, key));
return jredis.incr(JredisUtils.convert(charset, key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Integer incrBy(byte[] key, int value) {
public Long incrBy(byte[] key, long value) {
try {
return (int) jredis.incrby(JredisUtils.convert(charset, key), value);
return jredis.incrby(JredisUtils.convert(charset, key), (int) value);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -354,7 +363,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public byte[] lIndex(byte[] key, int index) {
public byte[] lIndex(byte[] key, long index) {
try {
return jredis.lindex(JredisUtils.convert(charset, key), (long) index);
} catch (RedisException ex) {
@@ -363,9 +372,9 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer lLen(byte[] key) {
public Long lLen(byte[] key) {
try {
return Integer.valueOf((int) jredis.llen(JredisUtils.convert(charset, key)));
return jredis.llen(JredisUtils.convert(charset, key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -381,7 +390,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer lPush(byte[] key, byte[] value) {
public Long lPush(byte[] key, byte[] value) {
try {
jredis.lpush(JredisUtils.convert(charset, key), value);
return null;
@@ -391,7 +400,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public List<byte[]> lRange(byte[] key, int start, int end) {
public List<byte[]> lRange(byte[] key, long start, long end) {
try {
List<byte[]> lrange = jredis.lrange(JredisUtils.convert(charset, key), start, end);
@@ -402,16 +411,16 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer lRem(byte[] key, int count, byte[] value) {
public Long lRem(byte[] key, long count, byte[] value) {
try {
return Integer.valueOf((int) jredis.lrem(JredisUtils.convert(charset, key), value, count));
return jredis.lrem(JredisUtils.convert(charset, key), value, (int) count);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public void lSet(byte[] key, int index, byte[] value) {
public void lSet(byte[] key, long index, byte[] value) {
try {
jredis.lset(JredisUtils.convert(charset, key), index, value);
} catch (RedisException ex) {
@@ -420,7 +429,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public void lTrim(byte[] key, int start, int end) {
public void lTrim(byte[] key, long start, long end) {
try {
jredis.ltrim(JredisUtils.convert(charset, key), start, end);
} catch (RedisException ex) {
@@ -447,7 +456,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer rPush(byte[] key, byte[] value) {
public Long rPush(byte[] key, byte[] value) {
try {
jredis.rpush(JredisUtils.convert(charset, key), value);
return null;
@@ -470,9 +479,9 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer sCard(byte[] key) {
public Long sCard(byte[] key) {
try {
return Integer.valueOf((int) jredis.scard(JredisUtils.convert(charset, key)));
return jredis.scard(JredisUtils.convert(charset, key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -621,18 +630,18 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer zCard(byte[] key) {
public Long zCard(byte[] key) {
try {
return Integer.valueOf((int) jredis.zcard(JredisUtils.convert(charset, key)));
return jredis.zcard(JredisUtils.convert(charset, key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Integer zCount(byte[] key, double min, double max) {
public Long zCount(byte[] key, double min, double max) {
try {
return Integer.valueOf((int) jredis.zcount(JredisUtils.convert(charset, key), min, max));
return jredis.zcount(JredisUtils.convert(charset, key), min, max);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -648,17 +657,17 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
throw new UnsupportedOperationException();
}
@Override
public Integer zInterStore(byte[] destKey, byte[]... sets) {
public Long zInterStore(byte[] destKey, byte[]... sets) {
throw new UnsupportedOperationException();
}
@Override
public Set<byte[]> zRange(byte[] key, int start, int end) {
public Set<byte[]> zRange(byte[] key, long start, long end) {
try {
return new LinkedHashSet<byte[]>(jredis.zrange(JredisUtils.convert(charset, key), (long) start, (long) end));
} catch (RedisException ex) {
@@ -667,7 +676,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Set<Tuple> zRangeWithScore(byte[] key, int start, int end) {
public Set<Tuple> zRangeWithScore(byte[] key, long start, long end) {
throw new UnsupportedOperationException();
}
@@ -687,19 +696,19 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Set<byte[]> zRangeByScore(byte[] key, double min, double max, int offset, int count) {
public Set<byte[]> zRangeByScore(byte[] key, double min, double max, long offset, long count) {
throw new UnsupportedOperationException();
}
@Override
public Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max, int offset, int count) {
public Set<Tuple> zRangeByScoreWithScore(byte[] key, double min, double max, long offset, long count) {
throw new UnsupportedOperationException();
}
@Override
public Integer zRank(byte[] key, byte[] value) {
public Long zRank(byte[] key, byte[] value) {
try {
return Integer.valueOf((int) jredis.zrank(JredisUtils.convert(charset, key), value));
return jredis.zrank(JredisUtils.convert(charset, key), value);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -715,25 +724,25 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer zRemRange(byte[] key, int start, int end) {
public Long zRemRange(byte[] key, long start, long end) {
try {
return Integer.valueOf((int) jredis.zremrangebyrank(JredisUtils.convert(charset, key), start, end));
return jredis.zremrangebyrank(JredisUtils.convert(charset, key), start, end);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Integer zRemRangeByScore(byte[] key, double min, double max) {
public Long zRemRangeByScore(byte[] key, double min, double max) {
try {
return Integer.valueOf((int) jredis.zremrangebyscore(JredisUtils.convert(charset, key), min, max));
return jredis.zremrangebyscore(JredisUtils.convert(charset, key), min, max);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Set<byte[]> zRevRange(byte[] key, int start, int end) {
public Set<byte[]> zRevRange(byte[] key, long start, long end) {
try {
return new LinkedHashSet<byte[]>(jredis.zrevrange(JredisUtils.convert(charset, key), start, end));
} catch (RedisException ex) {
@@ -742,14 +751,14 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Set<Tuple> zRevRangeWithScore(byte[] key, int start, int end) {
public Set<Tuple> zRevRangeWithScore(byte[] key, long start, long end) {
throw new UnsupportedOperationException();
}
@Override
public Integer zRevRank(byte[] key, byte[] value) {
public Long zRevRank(byte[] key, byte[] value) {
try {
return Integer.valueOf((int) jredis.zrevrank(JredisUtils.convert(charset, key), value));
return jredis.zrevrank(JredisUtils.convert(charset, key), value);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
@@ -770,12 +779,12 @@ public class JredisConnection implements RedisConnection {
//
@Override
public Integer zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
throw new UnsupportedOperationException();
}
@Override
public Integer zUnionStore(byte[] destKey, byte[]... sets) {
public Long zUnionStore(byte[] destKey, byte[]... sets) {
throw new UnsupportedOperationException();
}
@@ -816,7 +825,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer hIncrBy(byte[] key, byte[] field, int delta) {
public Long hIncrBy(byte[] key, byte[] field, long delta) {
throw new UnsupportedOperationException();
}
@@ -831,9 +840,9 @@ public class JredisConnection implements RedisConnection {
}
@Override
public Integer hLen(byte[] key) {
public Long hLen(byte[] key) {
try {
return Integer.valueOf((int) jredis.hlen(JredisUtils.convert(charset, key)));
return jredis.hlen(JredisUtils.convert(charset, key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}

View File

@@ -20,6 +20,8 @@ import java.util.Map;
import java.util.Set;
/**
* Hash operations bound to a certain key.
*
* @author Costin Leau
*/
public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
@@ -28,7 +30,7 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
boolean hasKey(Object key);
Integer increment(HK key, int delta);
Long increment(HK key, long delta);
HV get(Object key);
@@ -42,7 +44,7 @@ public interface BoundHashOperations<H, HK, HV> extends KeyBound<H> {
Collection<HV> values();
Integer length();
Long size();
void delete(Object key);

View File

@@ -0,0 +1,47 @@
/*
* 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

@@ -26,23 +26,23 @@ public interface BoundListOperations<K, V> extends KeyBound<K> {
RedisOperations<K, V> getOperations();
List<V> range(int start, int end);
List<V> range(long start, long end);
void trim(int start, int end);
void trim(long start, long end);
Integer length();
Long size();
Integer leftPush(V value);
Long leftPush(V value);
Integer rightPush(V value);
Long rightPush(V value);
V leftPop();
V rightPop();
Integer remove(int i, Object value);
Long remove(long i, Object value);
V index(int index);
V index(long index);
void set(int index, V value);
void set(long index, V value);
}

View File

@@ -41,11 +41,11 @@ public interface BoundSetOperations<K, V> extends KeyBound<K> {
Boolean add(V value);
boolean isMember(Object o);
Boolean isMember(Object o);
Set<V> members();
boolean remove(Object o);
Boolean remove(Object o);
int size();
Long size();
}

View File

@@ -0,0 +1,37 @@
/*
* 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.concurrent.TimeUnit;
/**
* @author Costin Leau
*/
public interface BoundValueOperations<K, V> extends KeyBound<K> {
void set(V value);
void set(V value, long timeout, TimeUnit unit);
Boolean setIfAbsent(V value);
V get();
V getAndSet(V value);
V increment(long delta);
}

View File

@@ -30,27 +30,27 @@ public interface BoundZSetOperations<K, V> extends KeyBound<K> {
void intersectAndStore(K destKey, K... keys);
Set<V> range(int start, int end);
Set<V> range(long start, long end);
Set<V> rangeByScore(double min, double max);
Set<V> reverseRange(int start, int end);
Set<V> reverseRange(long start, long end);
void removeRange(int start, int end);
void removeRange(long start, long end);
void removeRangeByScore(double min, double max);
void unionAndStore(K destKey, K... keys);
boolean add(V value, double score);
Boolean add(V value, double score);
Integer rank(Object o);
Long rank(Object o);
Integer reverseRank(Object o);
Long reverseRank(Object o);
boolean remove(Object o);
Boolean remove(Object o);
int size();
Long size();
Double score(Object o);
}

View File

@@ -65,7 +65,7 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
}
@Override
public Integer increment(HK key, int delta) {
public Long increment(HK key, long delta) {
return ops.increment(getKey(), key, delta);
}
@@ -75,8 +75,8 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
}
@Override
public Integer length() {
return ops.length(getKey());
public Long size() {
return ops.size(getKey());
}
@Override

View File

@@ -23,10 +23,16 @@ import java.util.List;
*
* @author Costin Leau
*/
public class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements BoundListOperations<K, V> {
class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> implements BoundListOperations<K, V> {
private final ListOperations<K, V> ops;
/**
* Constructs a new <code>DefaultBoundListOperations</code> instance.
*
* @param key
* @param template
*/
public DefaultBoundListOperations(K key, RedisTemplate<K, V> template) {
super(key);
this.ops = template.listOps();
@@ -39,7 +45,7 @@ public class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> impleme
}
@Override
public V index(int index) {
public V index(long index) {
return ops.index(getKey(), index);
}
@@ -49,22 +55,22 @@ public class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> impleme
}
@Override
public Integer leftPush(V value) {
public Long leftPush(V value) {
return ops.leftPush(getKey(), value);
}
@Override
public Integer length() {
return ops.length(getKey());
public Long size() {
return ops.size(getKey());
}
@Override
public List<V> range(int start, int end) {
public List<V> range(long start, long end) {
return ops.range(getKey(), start, end);
}
@Override
public Integer remove(int i, Object value) {
public Long remove(long i, Object value) {
return ops.remove(getKey(), i, value);
}
@@ -74,17 +80,17 @@ public class DefaultBoundListOperations<K, V> extends DefaultKeyBound<K> impleme
}
@Override
public Integer rightPush(V value) {
public Long rightPush(V value) {
return ops.rightPush(getKey(), value);
}
@Override
public void trim(int start, int end) {
public void trim(long start, long end) {
ops.trim(getKey(), start, end);
}
@Override
public void set(int index, V value) {
public void set(long index, V value) {
ops.set(getKey(), index, value);
}
}

View File

@@ -28,6 +28,12 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
private final SetOperations<K, V> ops;
/**
* Constructs a new <code>DefaultBoundSetOperations</code> instance.
*
* @param key
* @param template
*/
DefaultBoundSetOperations(K key, RedisTemplate<K, V> template) {
super(key);
this.ops = template.setOps();
@@ -64,7 +70,7 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
}
@Override
public boolean isMember(Object o) {
public Boolean isMember(Object o) {
return ops.isMember(getKey(), o);
}
@@ -74,12 +80,12 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
}
@Override
public boolean remove(Object o) {
public Boolean remove(Object o) {
return ops.remove(getKey(), o);
}
@Override
public int size() {
public Long size() {
return ops.size(getKey());
}

View File

@@ -0,0 +1,67 @@
/*
* 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.concurrent.TimeUnit;
/**
* @author Costin Leau
*/
class DefaultBoundValueOperations<K, V> extends DefaultKeyBound<K> implements BoundValueOperations<K, V> {
private final ValueOperations<K, V> ops;
/**
* Constructs a new <code>DefaultBoundValueOperations</code> instance.
*
* @param key
* @param template
*/
public DefaultBoundValueOperations(K key, RedisTemplate<K, V> template) {
super(key);
this.ops = template.valueOps();
}
@Override
public V get() {
return ops.get(getKey());
}
@Override
public V getAndSet(V value) {
return ops.getAndSet(getKey(), value);
}
@Override
public V increment(long delta) {
return ops.increment(getKey(), delta);
}
@Override
public void set(V value, long timeout, TimeUnit unit) {
ops.set(getKey(), value, timeout, unit);
}
@Override
public void set(V value) {
ops.set(getKey(), value);
}
@Override
public Boolean setIfAbsent(V value) {
return ops.setIfAbsent(getKey(), value);
}
}

View File

@@ -27,13 +27,19 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
private final ZSetOperations<K, V> ops;
/**
* Constructs a new <code>DefaultBoundZSetOperations</code> instance.
*
* @param key
* @param template
*/
public DefaultBoundZSetOperations(K key, RedisTemplate<K, V> template) {
super(key);
this.ops = template.zSetOps();
}
@Override
public boolean add(V value, double score) {
public Boolean add(V value, double score) {
return ops.add(getKey(), value, score);
}
@@ -48,7 +54,7 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
}
@Override
public Set<V> range(int start, int end) {
public Set<V> range(long start, long end) {
return ops.range(getKey(), start, end);
}
@@ -58,12 +64,12 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
}
@Override
public Integer rank(Object o) {
public Long rank(Object o) {
return ops.rank(getKey(), o);
}
@Override
public Integer reverseRank(Object o) {
public Long reverseRank(Object o) {
return ops.reverseRank(getKey(), o);
}
@@ -73,12 +79,12 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
}
@Override
public boolean remove(Object o) {
public Boolean remove(Object o) {
return ops.remove(getKey(), o);
}
@Override
public void removeRange(int start, int end) {
public void removeRange(long start, long end) {
ops.removeRange(getKey(), start, end);
}
@@ -88,12 +94,12 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
}
@Override
public Set<V> reverseRange(int start, int end) {
public Set<V> reverseRange(long start, long end) {
return ops.reverseRange(getKey(), start, end);
}
@Override
public int size() {
public Long size() {
return ops.size(getKey());
}

View File

@@ -21,16 +21,20 @@ package org.springframework.data.keyvalue.redis.core;
*
* @author Costin Leau
*/
public class DefaultKeyBound<K> implements KeyBound<K> {
class DefaultKeyBound<K> implements KeyBound<K> {
private final K key;
private K key;
public DefaultKeyBound(K key) {
this.key = key;
setKey(key);
}
@Override
public K getKey() {
return key;
}
protected void setKey(K key) {
this.key = key;
}
}

View File

@@ -34,11 +34,11 @@ public interface HashOperations<H, HK, HV> {
Collection<HV> multiGet(H key, Set<HK> hashKeys);
Integer increment(H key, HK hashKey, int delta);
Long increment(H key, HK hashKey, long delta);
Set<HK> keys(H key);
Integer length(H key);
Long size(H key);
void multiSet(H key, Map<? extends HK, ? extends HV> m);

View File

@@ -24,21 +24,21 @@ import java.util.List;
*/
public interface ListOperations<K, V> {
List<V> range(K key, int start, int end);
List<V> range(K key, long start, long end);
void trim(K key, int start, int end);
void trim(K key, long start, long end);
Integer length(K key);
Long size(K key);
Integer leftPush(K key, V value);
Long leftPush(K key, V value);
Integer rightPush(K key, V value);
Long rightPush(K key, V value);
void set(K key, int index, V value);
void set(K key, long index, V value);
Integer remove(K key, int i, Object value);
Long remove(K key, long i, Object value);
V index(K key, int index);
V index(K key, long index);
V leftPop(K key);

View File

@@ -15,6 +15,13 @@
*/
package org.springframework.data.keyvalue.redis.core;
import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.keyvalue.redis.connection.DataType;
/**
* Basic set of Redis operations, implemented by {@link RedisTemplate}.
@@ -23,21 +30,37 @@ package org.springframework.data.keyvalue.redis.core;
*/
public interface RedisOperations<K, V> {
void set(K key, V value);
Boolean exists(K key);
V get(K key);
void delete(Collection<K> key);
V getAndSet(K key, V newValue);
DataType type(K key);
void watch(K... keys);
Set<K> keys(K pattern);
K randomKey();
void rename(K oldKey, K newKey);
Boolean renameIfAbsent(K oldKey, K newKey);
Boolean expire(K key, long timeout, TimeUnit unit);
Boolean expireAt(K key, Date date);
void persist(K key);
long getExpire(K key);
void watch(Collection<K> keys);
void multi();
Object exec();
Integer increment(K key, int delta);
ValueOperations<K, V> valueOps();
void delete(K... keys);
BoundValueOperations<K, V> forValue(K key);
ListOperations<K, V> listOps();

View File

@@ -22,12 +22,16 @@ import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.dao.DataAccessException;
import org.springframework.data.keyvalue.redis.connection.DataType;
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
import org.springframework.data.keyvalue.redis.serializer.RedisSerializer;
@@ -211,7 +215,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
}
private byte[] rawKey(K key) {
private byte[] rawKey(Object key) {
return (key != null ? keySerializer.serialize(key) : null);
}
@@ -229,6 +233,17 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return rawKeys;
}
private byte[][] rawKeys(Collection<K> keys) {
final byte[][] rawKeys = new byte[keys.size()][];
int i = 0;
for (K key : keys) {
rawKeys[i++] = rawKey(key);
}
return rawKeys;
}
private <HK> byte[] rawHashKey(HK value) {
return (value != null ? hashKeySerializer.serialize(value) : null);
}
@@ -264,6 +279,19 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return values;
}
@SuppressWarnings("unchecked")
private Collection<K> deserializeKeys(Collection<byte[]> rawKeys, Class<? extends Collection> type) {
Collection<K> values = (List.class.isAssignableFrom(type) ? new ArrayList<K>(rawKeys.size())
: new LinkedHashSet<K>(rawKeys.size()));
for (byte[] bs : rawKeys) {
if (bs != null) {
values.add((K) hashValueSerializer.deserialize(bs));
}
}
return values;
}
@SuppressWarnings("unchecked")
private K deserializeKey(byte[] value) {
return (K) deserialize(value, keySerializer);
@@ -297,9 +325,9 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
// utility methods for the template internal methods
private abstract class ValueDeserializingRedisCallback implements RedisCallback<V> {
private K key;
private Object key;
public ValueDeserializingRedisCallback(K key) {
public ValueDeserializingRedisCallback(Object key) {
this.key = key;
}
@@ -320,82 +348,348 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
@Override
public Object exec() {
throw new UnsupportedOperationException();
}
return execute(new RedisCallback<Object>() {
@Override
public BoundListOperations<K, V> forList(K key) {
return new DefaultBoundListOperations<K, V>(key, this);
}
@Override
public V get(final K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.get(rawKey);
public Object doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exec();
}
});
}
@Override
public void delete(Collection<K> keys) {
final byte[][] rawKeys = rawKeys(keys);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.del(rawKeys);
return null;
}
}, true);
}
@Override
public V getAndSet(K key, V newValue) {
final byte[] rawValue = rawValue(newValue);
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.getSet(rawKey, rawValue);
}
}, true);
}
@Override
public Integer increment(K key, final int delta) {
public Boolean exists(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Boolean>() {
@Override
public Integer doInRedis(RedisConnection connection) {
if (delta == 1) {
return connection.incr(rawKey);
}
if (delta == -1) {
return connection.decr(rawKey);
}
if (delta < 0) {
return connection.decrBy(rawKey, delta);
}
return connection.incrBy(rawKey, delta);
public Boolean doInRedis(RedisConnection connection) {
return connection.exists(rawKey);
}
}, true);
}
@Override
public Boolean expire(K key, long timeout, TimeUnit unit) {
final byte[] rawKey = rawKey(key);
final int rawTimeout = (int) unit.toSeconds(timeout);
return execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) {
return connection.expire(rawKey, rawTimeout);
}
}, true);
}
@Override
public Boolean expireAt(K key, Date date) {
final byte[] rawKey = rawKey(key);
final long rawTimeout = date.getTime();
return execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) {
return connection.expireAt(rawKey, rawTimeout);
}
}, true);
}
//
// Value operations
//
@Override
public long getExpire(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) {
return Long.valueOf(connection.ttl(rawKey));
}
}, true);
}
@Override
public Set<K> keys(K pattern) {
final byte[] rawKey = rawKey(pattern);
Collection<byte[]> rawKeys = execute(new RedisCallback<Collection<byte[]>>() {
@Override
public Collection<byte[]> doInRedis(RedisConnection connection) {
return connection.keys(rawKey);
}
}, true);
return (Set<K>) deserializeKeys(rawKeys, Set.class);
}
@Override
public void persist(K key) {
final byte[] rawKey = rawKey(key);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.persist(rawKey);
return null;
}
}, true);
}
@Override
public K randomKey() {
byte[] rawKey = execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) {
return connection.randomKey();
}
}, true);
return deserializeKey(rawKey);
}
@Override
public void rename(K oldKey, K newKey) {
final byte[] rawOldKey = rawKey(oldKey);
final byte[] rawNewKey = rawKey(newKey);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.rename(rawOldKey, rawNewKey);
return null;
}
}, true);
}
@Override
public Boolean renameIfAbsent(K oldKey, K newKey) {
final byte[] rawOldKey = rawKey(oldKey);
final byte[] rawNewKey = rawKey(newKey);
return execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) {
return connection.renameNX(rawOldKey, rawNewKey);
}
}, true);
}
@Override
public DataType type(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<DataType>() {
@Override
public DataType doInRedis(RedisConnection connection) {
return connection.type(rawKey);
}
}, true);
}
@Override
public BoundValueOperations<K, V> forValue(K key) {
return new DefaultBoundValueOperations<K, V>(key, this);
}
@Override
public ValueOperations<K, V> valueOps() {
return new DefaultValueOperations();
}
private class DefaultValueOperations implements ValueOperations<K, V> {
@Override
public V get(final Object key) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.get(rawKey);
}
}, true);
}
@Override
public V getAndSet(K key, V newValue) {
final byte[] rawValue = rawValue(newValue);
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
return connection.getSet(rawKey, rawValue);
}
}, true);
}
@Override
public V increment(K key, final long delta) {
final byte[] rawKey = rawKey(key);
// TODO add conversion service in here ?
return (V) execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) {
if (delta == 1) {
return connection.incr(rawKey);
}
if (delta == -1) {
return connection.decr(rawKey);
}
if (delta < 0) {
return connection.decrBy(rawKey, delta);
}
return connection.incrBy(rawKey, delta);
}
}, true);
}
@Override
public Collection<V> multiGet(Set<K> keys) {
if (keys.isEmpty()) {
return Collections.emptyList();
}
final byte[][] rawKeys = new byte[keys.size()][];
int counter = 0;
for (K hashKey : keys) {
rawKeys[counter++] = rawKey(hashKey);
}
List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) {
return connection.mGet(rawKeys);
}
}, true);
return (List<V>) values(rawValues, List.class);
}
@Override
public void multiSet(Map<? extends K, ? extends V> m) {
if (m.isEmpty()) {
return;
}
final Map<byte[], byte[]> rawKeys = new LinkedHashMap<byte[], byte[]>(m.size());
for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
rawKeys.put(rawKey(entry.getKey()), rawValue(entry.getValue()));
}
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.mSet(rawKeys);
return null;
}
}, true);
}
@Override
public void multiSetIfAbsent(Map<? extends K, ? extends V> m) {
if (m.isEmpty()) {
return;
}
final Map<byte[], byte[]> rawKeys = new LinkedHashMap<byte[], byte[]>(m.size());
for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
rawKeys.put(rawKey(entry.getKey()), rawValue(entry.getValue()));
}
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.mSetNX(rawKeys);
return null;
}
}, true);
}
@Override
public void set(K key, V value) {
final byte[] rawValue = rawValue(value);
execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
connection.set(rawKey, rawValue);
return null;
}
}, true);
}
@Override
public void set(K key, V value, long timeout, TimeUnit unit) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
final long rawTimeout = unit.toSeconds(timeout);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.setEx(rawKey, (int) rawTimeout, rawValue);
return null;
}
}, true);
}
@Override
public Boolean setIfAbsent(K key, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.setNX(rawKey, rawValue);
}
}, true);
}
}
@Override
public ListOperations<K, V> listOps() {
return new DefaultListOperations();
}
@Override
public void multi() {
throw new UnsupportedOperationException();
public BoundListOperations<K, V> forList(K key) {
return new DefaultBoundListOperations<K, V>(key, this);
}
@Override
public void set(K key, V value) {
final byte[] rawValue = rawValue(value);
execute(new ValueDeserializingRedisCallback(key) {
public void multi() {
execute(new RedisCallback<Object>() {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
connection.set(rawKey, rawValue);
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.multi();
return null;
}
}, true);
}
@Override
public void watch(K... keys) {
public void watch(Collection<K> keys) {
final byte[][] rawKeys = rawKeys(keys);
execute(new RedisCallback<Object>() {
@@ -407,19 +701,6 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}, true);
}
@Override
public void delete(K... keys) {
final byte[][] rawKeys = rawKeys(keys);
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.del(rawKeys);
return null;
}
}, true);
}
//
// List operations
//
@@ -450,7 +731,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public V index(K key, final int index) {
public V index(K key, final long index) {
return execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
@@ -470,30 +751,30 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Integer leftPush(K key, V value) {
public Long leftPush(K key, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.lPush(rawKey, rawValue);
}
}, true);
}
@Override
public Integer length(K key) {
public Long size(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.lLen(rawKey);
}
}, true);
}
@Override
public List<V> range(K key, final int start, final int end) {
public List<V> range(K key, final long start, final long end) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<List<V>>() {
@Override
@@ -504,12 +785,12 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Integer remove(K key, final int count, Object value) {
public Long remove(K key, final long count, Object value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.lRem(rawKey, count, rawValue);
}
}, true);
@@ -526,19 +807,19 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Integer rightPush(K key, V value) {
public Long rightPush(K key, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.rPush(rawKey, rawValue);
}
}, true);
}
@Override
public void set(K key, final int index, V value) {
public void set(K key, final long index, V value) {
final byte[] rawValue = rawValue(value);
execute(new ValueDeserializingRedisCallback(key) {
@Override
@@ -550,7 +831,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public void trim(K key, final int start, final int end) {
public void trim(K key, final long start, final long end) {
execute(new ValueDeserializingRedisCallback(key) {
@Override
protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
@@ -662,7 +943,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public boolean isMember(K key, Object o) {
public Boolean isMember(K key, Object o) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(o);
return execute(new RedisCallback<Boolean>() {
@@ -687,7 +968,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public boolean remove(K key, Object o) {
public Boolean remove(K key, Object o) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(o);
return execute(new RedisCallback<Boolean>() {
@@ -699,11 +980,11 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public int size(K key) {
public Long size(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.sCard(rawKey);
}
}, true);
@@ -753,7 +1034,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
private class DefaultZSetOperations implements ZSetOperations<K, V> {
@Override
public boolean add(final K key, final V value, final double score) {
public Boolean add(final K key, final V value, final double score) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
@@ -784,7 +1065,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Set<V> range(K key, final int start, final int end) {
public Set<V> range(K key, final long start, final long end) {
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
@@ -812,33 +1093,33 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Integer rank(K key, Object o) {
public Long rank(K key, Object o) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(o);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.zRank(rawKey, rawValue);
}
}, true);
}
@Override
public Integer reverseRank(K key, Object o) {
public Long reverseRank(K key, Object o) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(o);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.zRevRank(rawKey, rawValue);
}
}, true);
}
@Override
public boolean remove(K key, Object o) {
public Boolean remove(K key, Object o) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(o);
@@ -851,7 +1132,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public void removeRange(K key, final int start, final int end) {
public void removeRange(K key, final long start, final long end) {
final byte[] rawKey = rawKey(key);
execute(new RedisCallback<Object>() {
@Override
@@ -875,7 +1156,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Set<V> reverseRange(K key, final int start, final int end) {
public Set<V> reverseRange(K key, final long start, final long end) {
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
@@ -902,12 +1183,12 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public int size(K key) {
public Long size(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.zCard(rawKey);
}
}, true);
@@ -978,13 +1259,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Integer increment(K key, HK hashKey, final int delta) {
public Long increment(K key, HK hashKey, final long delta) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawHashKey(hashKey);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.hIncrBy(rawKey, rawHashKey, delta);
}
}, true);
@@ -1006,12 +1287,12 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
@Override
public Integer length(K key) {
public Long size(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Integer>() {
return execute(new RedisCallback<Long>() {
@Override
public Integer doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
return connection.hLen(rawKey);
}
}, true);

View File

@@ -41,12 +41,12 @@ public interface SetOperations<K, V> {
Boolean add(K key, V value);
boolean isMember(K key, Object o);
Boolean isMember(K key, Object o);
Set<V> members(K key);
boolean remove(K key, Object o);
Boolean remove(K key, Object o);
int size(K key);
Long size(K key);
}

View File

@@ -0,0 +1,47 @@
/*
* 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.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Redis operations for simple (or in Redis terminology 'string') values.
*
* @author Costin Leau
*/
public interface ValueOperations<K, V> {
void set(K key, V value);
void set(K key, V value, long timeout, TimeUnit unit);
Boolean setIfAbsent(K key, V value);
void multiSet(Map<? extends K, ? extends V> m);
void multiSetIfAbsent(Map<? extends K, ? extends V> m);
V get(Object key);
V getAndSet(K key, V value);
Collection<V> multiGet(Set<K> keys);
V increment(K key, long delta);
}

View File

@@ -27,29 +27,29 @@ public interface ZSetOperations<K, V> {
void intersectAndStore(K key, K destKey, K... keys);
Set<V> range(K key, int start, int end);
Set<V> range(K key, long start, long end);
Set<V> rangeByScore(K key, double min, double max);
Set<V> reverseRange(K key, int start, int end);
Set<V> reverseRange(K key, long start, long end);
void removeRange(K key, int start, int end);
void removeRange(K key, long start, long end);
void removeRangeByScore(K key, double min, double max);
void unionAndStore(K key, K destKey, K... keys);
boolean add(K key, V value, double score);
Boolean add(K key, V value, double score);
Integer rank(K key, Object o);
Long rank(K key, Object o);
Integer reverseRank(K key, Object o);
Long reverseRank(K key, Object o);
Double score(K key, Object o);
boolean remove(K key, Object o);
Boolean remove(K key, Object o);
int size(K key);
Long size(K key);
RedisOperations<K, V> getOperations();
}

View File

@@ -62,7 +62,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
}
@Override
public List<E> range(int start, int end) {
public List<E> range(long start, long end) {
return listOps.range(start, end);
}
@@ -83,7 +83,7 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
@Override
public int size() {
return listOps.length();
return listOps.size().intValue();
}
@@ -100,8 +100,8 @@ public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements R
@Override
public boolean remove(Object o) {
Integer result = listOps.remove(0, o);
return (result != null && result.intValue() > 0);
Long result = listOps.remove(0, o);
return (result != null && result.longValue() > 0);
}
@Override

View File

@@ -16,6 +16,7 @@
package org.springframework.data.keyvalue.redis.util;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
@@ -79,19 +80,10 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
}
@Override
public Integer increment(K key, int delta) {
public Long increment(K key, long delta) {
return hashOps.increment(key, delta);
}
@Override
public boolean putIfAbsent(K key, V value) {
if (!hashOps.hasKey(key)) {
put(key, value);
return true;
}
return false;
}
@Override
public String getKey() {
return hashOps.getKey();
@@ -104,7 +96,7 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
@Override
public void clear() {
getOperations().delete(getKey());
getOperations().delete(Collections.singleton(getKey()));
}
@Override
@@ -169,7 +161,7 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
@Override
public int size() {
return hashOps.length();
return hashOps.size().intValue();
}
@Override
@@ -203,4 +195,104 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
sb.append(getKey());
return sb.toString();
}
@Override
public V putIfAbsent(K key, V value) {
throw new UnsupportedOperationException();
// RedisOperations<String, ?> ops = hashOps.getOperations();
//
// for (;;) {
// ops.watch(getKey());
// V v = get(key);
// if (v == null) {
// ops.multi();
// put(key, value);
// if (ops.exec() != null) {
// return null;
// }
// }
// else {
// return v;
// }
// }
}
@Override
public boolean remove(Object key, Object value) {
throw new UnsupportedOperationException();
// if (value == null){
// throw new NullPointerException();
// }
//
// RedisOperations<String, ?> ops = hashOps.getOperations();
//
// for (;;) {
// ops.watch(getKey());
// V v = get(key);
// if (value.equals(v)) {
// ops.multi();
// remove(key);
// if (ops.exec() != null) {
// return true;
// }
// }
// else {
// return false;
// }
// }
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
throw new UnsupportedOperationException();
// if (newValue == null || oldValue == null) {
// throw new NullPointerException();
// }
//
// RedisOperations<String, ?> ops = hashOps.getOperations();
//
// for (;;) {
// ops.watch(getKey());
// V v = get(key);
// if (oldValue.equals(v)) {
// ops.multi();
// put(key, newValue);
// if (ops.exec() != null) {
// return true;
// }
// }
// else {
// return false;
// }
// }
}
@Override
public V replace(K key, V value) {
throw new UnsupportedOperationException();
// if (value == null) {
// throw new NullPointerException();
// }
//
// RedisOperations<String, ?> ops = hashOps.getOperations();
//
// for (;;) {
// ops.watch(getKey());
// if (containsKey(key)) {
// ops.multi();
// V oldValue = put(key, value);
// if (ops.exec() != null) {
// return oldValue;
// }
// }
// else {
// return null;
// }
// }
}
}

View File

@@ -125,7 +125,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
@Override
public int size() {
return boundSetOps.size();
return boundSetOps.size().intValue();
}
private String[] extractKeys(RedisSet<?>... sets) {

View File

@@ -96,12 +96,12 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
}
@Override
public Set<E> range(int start, int end) {
public Set<E> range(long start, long end) {
return boundZSetOps.range(start, end);
}
@Override
public Set<E> reverseRange(int start, int end) {
public Set<E> reverseRange(long start, long end) {
return boundZSetOps.reverseRange(start, end);
}
@@ -111,7 +111,7 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
}
@Override
public RedisZSet<E> remove(int start, int end) {
public RedisZSet<E> remove(long start, long end) {
boundZSetOps.removeRange(start, end);
return this;
}
@@ -160,7 +160,7 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
@Override
public int size() {
return boundZSetOps.size();
return boundZSetOps.size().intValue();
}
@Override
@@ -185,12 +185,12 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
}
@Override
public Integer rank(Object o) {
public Long rank(Object o) {
return boundZSetOps.rank(o);
}
@Override
public Integer reverseRank(Object o) {
public Long reverseRank(Object o) {
return boundZSetOps.reverseRank(o);
}

View File

@@ -16,8 +16,10 @@
package org.springframework.data.keyvalue.redis.util;
import java.io.Serializable;
import java.util.Collections;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
import org.springframework.data.keyvalue.redis.core.ValueOperations;
/**
* Atomic integer backed by Redis.
@@ -29,7 +31,8 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
public class RedisAtomicInteger extends Number implements Serializable {
private final String key;
private RedisOperations<String, Integer> operations;
private ValueOperations<String, Integer> operations;
private RedisOperations<String, Integer> generalOps;
/**
* Constructs a new <code>RedisAtomicInteger</code> instance with an initial value of zero.
@@ -50,8 +53,9 @@ public class RedisAtomicInteger extends Number implements Serializable {
*/
public RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer> operations, int initialValue) {
this.key = redisCounter;
this.operations = operations;
operations.set(redisCounter, initialValue);
this.operations = operations.valueOps();
this.generalOps = operations;
this.operations.set(redisCounter, initialValue);
}
/**
@@ -92,15 +96,17 @@ public class RedisAtomicInteger extends Number implements Serializable {
*/
public boolean compareAndSet(int expect, int update) {
for (;;) {
operations.watch(key);
generalOps.watch(Collections.singleton(key));
if (expect == get()) {
operations.multi();
generalOps.multi();
set(update);
if (operations.exec() != null) {
if (generalOps.exec() != null) {
return true;
}
}
return false;
else {
return false;
}
}
}
@@ -110,11 +116,11 @@ public class RedisAtomicInteger extends Number implements Serializable {
*/
public int getAndIncrement() {
for (;;) {
operations.watch(key);
generalOps.watch(Collections.singleton(key));
int value = get();
operations.multi();
generalOps.multi();
operations.increment(key, 1);
if (operations.exec() != null) {
if (generalOps.exec() != null) {
return value;
}
}
@@ -127,11 +133,11 @@ public class RedisAtomicInteger extends Number implements Serializable {
*/
public int getAndDecrement() {
for (;;) {
operations.watch(key);
generalOps.watch(Collections.singleton(key));
int value = get();
operations.multi();
generalOps.multi();
operations.increment(key, -1);
if (operations.exec() != null) {
if (generalOps.exec() != null) {
return value;
}
}
@@ -145,11 +151,11 @@ public class RedisAtomicInteger extends Number implements Serializable {
*/
public int getAndAdd(int delta) {
for (;;) {
operations.watch(key);
generalOps.watch(Collections.singleton(key));
int value = get();
operations.multi();
generalOps.multi();
set(value + delta);
if (operations.exec() != null) {
if (generalOps.exec() != null) {
return value;
}
}

View File

@@ -16,8 +16,10 @@
package org.springframework.data.keyvalue.redis.util;
import java.io.Serializable;
import java.util.Collections;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
import org.springframework.data.keyvalue.redis.core.ValueOperations;
/**
* Atomic long backed by Redis.
@@ -29,7 +31,8 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
public class RedisAtomicLong extends Number implements Serializable {
private final String key;
private RedisOperations<String, Long> operations;
private ValueOperations<String, Long> operations;
private RedisOperations<String, Long> generalOps;
/**
* Constructs a new <code>RedisAtomicLong</code> instance with an initial value of zero.
@@ -50,8 +53,8 @@ public class RedisAtomicLong extends Number implements Serializable {
*/
public RedisAtomicLong(String redisCounter, RedisOperations<String, Long> operations, long initialValue) {
this.key = redisCounter;
this.operations = operations;
operations.set(redisCounter, initialValue);
this.operations = operations.valueOps();
this.operations.set(redisCounter, initialValue);
}
/**
@@ -93,15 +96,17 @@ public class RedisAtomicLong extends Number implements Serializable {
*/
public boolean compareAndSet(long expect, long update) {
for (;;) {
operations.watch(key);
generalOps.watch(Collections.singleton(key));
if (expect == get()) {
operations.multi();
generalOps.multi();
set(update);
if (operations.exec() != null) {
if (generalOps.exec() != null) {
return true;
}
}
return false;
else {
return false;
}
}
}
@@ -112,11 +117,11 @@ public class RedisAtomicLong extends Number implements Serializable {
*/
public long getAndIncrement() {
for (;;) {
operations.watch(key);
generalOps.watch(Collections.singleton(key));
long value = get();
operations.multi();
generalOps.multi();
operations.increment(key, 1);
if (operations.exec() != null) {
if (generalOps.exec() != null) {
return value;
}
}
@@ -129,11 +134,11 @@ public class RedisAtomicLong extends Number implements Serializable {
*/
public long getAndDecrement() {
for (;;) {
operations.watch(key);
generalOps.watch(Collections.singleton(key));
long value = get();
operations.multi();
generalOps.multi();
operations.increment(key, -1);
if (operations.exec() != null) {
if (generalOps.exec() != null) {
return value;
}
}
@@ -147,11 +152,11 @@ public class RedisAtomicLong extends Number implements Serializable {
*/
public long getAndAdd(long delta) {
for (;;) {
operations.watch(key);
generalOps.watch(Collections.singleton(key));
long value = get();
operations.multi();
generalOps.multi();
set(value + delta);
if (operations.exec() != null) {
if (generalOps.exec() != null) {
return value;
}
}

View File

@@ -26,7 +26,7 @@ import java.util.Queue;
*/
public interface RedisList<E> extends RedisStore<String>, List<E>, Queue<E> {
List<E> range(int start, int end);
List<E> range(long start, long end);
RedisList<E> trim(int start, int end);
}

View File

@@ -15,16 +15,15 @@
*/
package org.springframework.data.keyvalue.redis.util;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
/**
* Map view of a Redis hash.
*
* @author Costin Leau
*/
public interface RedisMap<K, V> extends RedisStore<String>, Map<K, V> {
public interface RedisMap<K, V> extends RedisStore<String>, ConcurrentMap<K, V> {
boolean putIfAbsent(K key, V value);
Integer increment(K key, int delta);
Long increment(K key, long delta);
}

View File

@@ -32,13 +32,13 @@ public interface RedisZSet<E> extends RedisStore<String>, Set<E> {
RedisZSet<E> unionAndStore(String destKey, RedisZSet<E>... sets);
Set<E> range(int start, int end);
Set<E> range(long start, long end);
Set<E> reverseRange(int start, int end);
Set<E> reverseRange(long start, long end);
Set<E> rangeByScore(double min, double max);
RedisZSet<E> remove(int start, int end);
RedisZSet<E> remove(long start, long end);
RedisZSet<E> removeByScore(double min, double max);
@@ -78,7 +78,7 @@ public interface RedisZSet<E> extends RedisStore<String>, Set<E> {
* @param o object
* @return rank of the given object
*/
Integer rank(Object o);
Long rank(Object o);
/**
* Returns the rank (position) of the given element in the set, in descending order.
@@ -87,7 +87,7 @@ public interface RedisZSet<E> extends RedisStore<String>, Set<E> {
* @param o object
* @return reverse rank of the given object
*/
Integer reverseRank(Object o);
Long reverseRank(Object o);
/**
* Returns the default score used by this set.

View File

@@ -23,8 +23,6 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.keyvalue.redis.Person;
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
public abstract class AbstractConnectionIntegrationTests {
@@ -46,9 +44,9 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
public void testLPush() throws Exception {
Integer index = connection.lPush(listName.getBytes(), "bar".getBytes());
Long index = connection.lPush(listName.getBytes(), "bar".getBytes());
if (index != null) {
assertEquals((Integer) (index + 1), connection.lPush(listName.getBytes(), "bar".getBytes()));
assertEquals((Long) (index + 1), connection.lPush(listName.getBytes(), "bar".getBytes()));
}
}

View File

@@ -22,6 +22,7 @@ import static org.junit.matchers.JUnitMatchers.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
@@ -101,7 +102,7 @@ public abstract class AbstractRedisCollectionTests<T> {
@After
public void tearDown() throws Exception {
// remove the collection entirely since clear() doesn't always work
collection.getOperations().delete(collection.getKey());
collection.getOperations().delete(Collections.singleton(collection.getKey()));
template.execute(new RedisCallback<Object>() {
@Override

View File

@@ -21,6 +21,7 @@ import static org.junit.matchers.JUnitMatchers.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@@ -101,7 +102,7 @@ public abstract class AbstractRedisMapTests<K, V> {
@After
public void tearDown() throws Exception {
// remove the collection entirely since clear() doesn't always work
map.getOperations().delete(map.getKey());
map.getOperations().delete(Collections.singleton(map.getKey()));
template.execute(new RedisCallback<Object>() {
@Override
@@ -201,7 +202,7 @@ public abstract class AbstractRedisMapTests<K, V> {
V v1 = getValue();
map.put(k1, v1);
Integer value = map.increment(k1, 1);
Long value = map.increment(k1, 1);
System.out.println("Value is " + value);
}
@@ -269,25 +270,6 @@ public abstract class AbstractRedisMapTests<K, V> {
assertEquals(v2, map.get(k2));
}
@Test
public void testPutIfAbsent() {
K k1 = getKey();
K k2 = getKey();
V v1 = getValue();
V v2 = getValue();
assertNull(map.get(k1));
assertTrue(map.putIfAbsent(k1, v1));
assertFalse(map.putIfAbsent(k1, v2));
assertEquals(v1, map.get(k1));
assertTrue(map.putIfAbsent(k2, v2));
assertFalse(map.putIfAbsent(k2, v1));
assertEquals(v2, map.get(k2));
}
@Test
public void testRemove() {
K k1 = getKey();
@@ -375,4 +357,65 @@ public abstract class AbstractRedisMapTests<K, V> {
assertThat(values, hasItem(v1));
assertThat(values, not(hasItem(v2)));
}
@Test(expected = UnsupportedOperationException.class)
public void testConcurrentPutIfAbsent() {
K k1 = getKey();
K k2 = getKey();
V v1 = getValue();
V v2 = getValue();
assertNull(map.get(k1));
assertNull(map.putIfAbsent(k1, v1));
assertEquals(v1, map.putIfAbsent(k1, v2));
assertEquals(v1, map.get(k1));
assertNull(map.putIfAbsent(k2, v2));
assertEquals(v2, map.putIfAbsent(k2, v1));
assertEquals(v2, map.get(k2));
}
@Test(expected = UnsupportedOperationException.class)
public void testConcurrentRemove() {
K k1 = getKey();
V v1 = getValue();
V v2 = getValue();
map.put(k1, v1);
assertFalse(map.remove(k1, v1));
assertEquals(v1, map.get(k1));
assertTrue(map.remove(k1, v1));
assertNull(map.get(k1));
}
@Test(expected = UnsupportedOperationException.class)
public void testConcurrentReplaceTwoArgs() {
K k1 = getKey();
V v1 = getValue();
V v2 = getValue();
map.put(k1, v1);
assertFalse(map.replace(k1, v2, v1));
assertEquals(v1, map.get(k1));
assertTrue(map.replace(k1, v1, v2));
assertEquals(v2, map.get(k1));
}
@Test(expected = UnsupportedOperationException.class)
public void testConcurrentReplaceOneArg() {
K k1 = getKey();
V v1 = getValue();
V v2 = getValue();
assertNull(map.replace(k1, v1));
map.put(k1, v1);
assertNull(map.replace(getKey(), v1));
assertEquals(v1, map.replace(k1, v2));
assertEquals(v2, map.get(k1));
}
}

View File

@@ -136,9 +136,9 @@ public abstract class AbstractRedisZSetTest<T> extends AbstractRedisCollectionTe
zSet.add(t2, 4);
zSet.add(t3, 5);
assertEquals(Integer.valueOf(0), zSet.rank(t1));
assertEquals(Integer.valueOf(1), zSet.rank(t2));
assertEquals(Integer.valueOf(2), zSet.rank(t3));
assertEquals(Long.valueOf(0), zSet.rank(t1));
assertEquals(Long.valueOf(1), zSet.rank(t2));
assertEquals(Long.valueOf(2), zSet.rank(t3));
assertNull(zSet.rank(getT()));
}
@@ -152,9 +152,9 @@ public abstract class AbstractRedisZSetTest<T> extends AbstractRedisCollectionTe
zSet.add(t2, 4);
zSet.add(t3, 5);
assertEquals(Integer.valueOf(0), zSet.reverseRank(t3));
assertEquals(Integer.valueOf(1), zSet.reverseRank(t2));
assertEquals(Integer.valueOf(2), zSet.reverseRank(t1));
assertEquals(Long.valueOf(0), zSet.reverseRank(t3));
assertEquals(Long.valueOf(1), zSet.reverseRank(t2));
assertEquals(Long.valueOf(2), zSet.reverseRank(t1));
assertNull(zSet.rank(getT()));
}

View File

@@ -50,8 +50,8 @@ public class RedisMapTests extends AbstractRedisMapTests<Object, Object> {
jedisConnFactory.setPooling(false);
jedisConnFactory.afterPropertiesSet();
RedisTemplate<String, String> stringTemplate = new RedisTemplate<String, String>(jedisConnFactory);
RedisTemplate<String, Person> personTemplate = new RedisTemplate<String, Person>(jedisConnFactory);
RedisTemplate<String, String> genericTemplate = new RedisTemplate<String, String>(jedisConnFactory);
// JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
// jredisConnFactory.setPooling(false);
@@ -60,7 +60,8 @@ public class RedisMapTests extends AbstractRedisMapTests<Object, Object> {
// RedisTemplate<String, String> stringTemplateJR = new RedisTemplate<String, String>(jredisConnFactory);
// RedisTemplate<String, Person> personTemplateJR = new RedisTemplate<String, Person>(jredisConnFactory);
return Arrays.asList(new Object[][] { { stringFactory, stringFactory, stringTemplate },
{ personFactory, personFactory, personTemplate } });
return Arrays.asList(new Object[][] { { stringFactory, stringFactory, genericTemplate },
{ personFactory, personFactory, genericTemplate }, { stringFactory, personFactory, genericTemplate },
{ personFactory, stringFactory, genericTemplate } });
}
}

View File

@@ -1,5 +1,5 @@
Bundle-SymbolicName: org.springframework.data.redis
Bundle-Name: Spring Datastore Redis Support
Bundle-Name: Spring Data Redis Support
Bundle-Vendor: SpringSource
Bundle-ManifestVersion: 2
Import-Package:
@@ -22,4 +22,5 @@ Import-Template:
org.springframework.transaction.support.*;version="[3.0.0, 4.0.0)",
redis.clients.jedis.*;version="[1.0.0, 2.0.0)",
redis.clients.util.*;version="[1.0.0, 2.0.0)",
org.apache.commons.pool.impl.*;version="[1.0.0, 3.0.0)"