+ add support for Redis 2.2 String commands (get/set bit, get/set range and strlen)
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
//
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user