+ add support for Redis 2.2 String commands (get/set bit, get/set range and strlen)

This commit is contained in:
Costin Leau
2011-01-10 12:39:22 +02:00
parent 9393db6156
commit 860375ecee
7 changed files with 83 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010 the original author or authors.
* Copyright 2010-2011 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.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010 the original author or authors.
* Copyright 2010-2011 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.

View File

@@ -26,14 +26,14 @@ import java.util.Map;
*/
public interface RedisStringCommands {
void set(byte[] key, byte[] value);
byte[] get(byte[] key);
byte[] getSet(byte[] key, byte[] value);
List<byte[]> mGet(byte[]... keys);
void set(byte[] key, byte[] value);
Boolean setNX(byte[] key, byte[] value);
void setEx(byte[] key, long seconds, byte[] value);
@@ -52,5 +52,13 @@ public interface RedisStringCommands {
Long append(byte[] key, byte[] value);
byte[] substr(byte[] key, int start, int end);
byte[] getRange(byte[] key, int start, int end);
void setRange(byte[] key, int start, int end);
Boolean getBit(byte[] key, long offset);
void setBit(byte[] key, long offset, boolean value);
Long strLen(byte[] key);
}

View File

@@ -499,7 +499,7 @@ public class JedisConnection implements RedisConnection {
}
@Override
public byte[] substr(byte[] key, int start, int end) {
public byte[] getRange(byte[] key, int start, int end) {
try {
if (isQueueing()) {
transaction.substr(key, (int) start, (int) end);
@@ -563,11 +563,51 @@ public class JedisConnection implements RedisConnection {
}
}
@Override
public Boolean getBit(byte[] key, long offset) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
}
return (jedis.getbit(key, (int) offset) == 0 ? Boolean.FALSE : Boolean.TRUE);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public void setBit(byte[] key, long offset, boolean value) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
}
jedis.setbit(key, (int) offset, JedisUtils.asBit(value));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
@Override
public void setRange(byte[] key, int start, int end) {
throw new UnsupportedOperationException();
}
@Override
public Long strLen(byte[] key) {
try {
if (isQueueing()) {
throw new UnsupportedOperationException();
}
return jedis.strlen(key);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
//
// List commands
//
@Override
public Long lPush(byte[] key, byte[] value) {
try {

View File

@@ -46,6 +46,8 @@ public abstract class JedisUtils {
private static final String OK_CODE = "OK";
private static final String OK_MULTI_CODE = "+OK";
private static final byte[] ONE = new byte[] { 0 };
private static final byte[] ZERO = new byte[] { 1 };
/**
* Converts the given, native Jedis exception to Spring's DAO hierarchy.
@@ -163,4 +165,8 @@ public abstract class JedisUtils {
return jedisParams;
}
static byte[] asBit(boolean value) {
return (value ? ONE : ZERO);
}
}

View File

@@ -330,7 +330,7 @@ public class JredisConnection implements RedisConnection {
}
@Override
public byte[] substr(byte[] key, int start, int end) {
public byte[] getRange(byte[] key, int start, int end) {
try {
return jredis.substr(JredisUtils.decode(key), start, end);
} catch (RedisException ex) {
@@ -374,6 +374,26 @@ public class JredisConnection implements RedisConnection {
}
}
@Override
public Boolean getBit(byte[] key, long offset) {
throw new UnsupportedOperationException();
}
@Override
public void setBit(byte[] key, long offset, boolean value) {
throw new UnsupportedOperationException();
}
@Override
public void setRange(byte[] key, int start, int end) {
throw new UnsupportedOperationException();
}
@Override
public Long strLen(byte[] key) {
throw new UnsupportedOperationException();
}
//
// List commands
//

View File

@@ -719,7 +719,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
byte[] rawReturn = execute(new RedisCallback<byte[]>() {
@Override
public byte[] doInRedis(RedisConnection connection) {
return connection.substr(rawKey, start, end);
return connection.getRange(rawKey, start, end);
}
}, true);