+ finish up String operations
+ add Jedis and JRedis implementations
This commit is contained in:
@@ -16,13 +16,13 @@
|
||||
|
||||
package org.springframework.datastore.redis.connection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* String specific commands supported by Redis.
|
||||
*
|
||||
* @author Costin Leau
|
||||
*/
|
||||
// TODO should the strings be byte[] instead
|
||||
// at least for values ?
|
||||
public interface RedisStringCommands {
|
||||
|
||||
void set(String key, String value);
|
||||
@@ -31,6 +31,16 @@ public interface RedisStringCommands {
|
||||
|
||||
String getSet(String key, String value);
|
||||
|
||||
List<String> mGet(String... keys);
|
||||
|
||||
Boolean setNX(String key, String value);
|
||||
|
||||
void setEx(String key, int seconds, String value);
|
||||
|
||||
void mSet(String[] keys, String[] values);
|
||||
|
||||
void mSetNX(String[] keys, String[] values);
|
||||
|
||||
Integer incr(String key);
|
||||
|
||||
Integer incrBy(String key, int value);
|
||||
@@ -38,4 +48,8 @@ public interface RedisStringCommands {
|
||||
Integer decr(String key);
|
||||
|
||||
Integer decrBy(String key, int value);
|
||||
|
||||
Integer append(String key, String value);
|
||||
|
||||
String substr(String key, int start, int end);
|
||||
}
|
||||
|
||||
@@ -316,31 +316,9 @@ public class JedisConnection implements RedisConnection {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean hSet(String key, String field, String value) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.hset(key, field, value);
|
||||
return null;
|
||||
}
|
||||
return JedisUtils.convertCodeReply(jedis.hset(key, field, value));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean hSetNX(String key, String field, String value) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.hsetnx(key, field, value);
|
||||
return null;
|
||||
}
|
||||
return JedisUtils.convertCodeReply(jedis.hsetnx(key, field, value));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
//
|
||||
// String commands
|
||||
//
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
@@ -379,6 +357,93 @@ public class JedisConnection implements RedisConnection {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer append(String key, String value) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.append(key, value);
|
||||
return null;
|
||||
}
|
||||
return jedis.append(key, value);
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> mGet(String... keys) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.mget(keys);
|
||||
return null;
|
||||
}
|
||||
return jedis.mget(keys);
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mSet(String[] keys, String[] values) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.mset(JedisUtils.arrange(keys, values));
|
||||
}
|
||||
jedis.mset(JedisUtils.arrange(keys, values));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mSetNX(String[] keys, String[] values) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.msetnx(JedisUtils.arrange(keys, values));
|
||||
}
|
||||
jedis.msetnx(JedisUtils.arrange(keys, values));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEx(String key, int time, String value) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.setex(key, time, value);
|
||||
}
|
||||
jedis.setex(key, time, value);
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean setNX(String key, String value) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.setnx(key, value);
|
||||
}
|
||||
return JedisUtils.convertCodeReply(jedis.setnx(key, value));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String substr(String key, int start, int end) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.substr(key, start, end);
|
||||
return null;
|
||||
}
|
||||
return jedis.substr(key, start, end);
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer decr(String key) {
|
||||
try {
|
||||
@@ -1072,6 +1137,32 @@ public class JedisConnection implements RedisConnection {
|
||||
// Hash commands
|
||||
//
|
||||
|
||||
@Override
|
||||
public Boolean hSet(String key, String field, String value) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.hset(key, field, value);
|
||||
return null;
|
||||
}
|
||||
return JedisUtils.convertCodeReply(jedis.hset(key, field, value));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean hSetNX(String key, String field, String value) {
|
||||
try {
|
||||
if (isQueueing()) {
|
||||
transaction.hsetnx(key, field, value);
|
||||
return null;
|
||||
}
|
||||
return JedisUtils.convertCodeReply(jedis.hsetnx(key, field, value));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean hDel(String key, String field) {
|
||||
try {
|
||||
|
||||
@@ -102,4 +102,16 @@ public abstract class JedisUtils {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static String[] arrange(String[] keys, String[] values) {
|
||||
String[] result = new String[keys.length * 2];
|
||||
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
int index = i << 1;
|
||||
result[index] = keys[i];
|
||||
result[index + 1] = values[i];
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -204,6 +204,10 @@ public class JredisConnection implements RedisConnection {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
//
|
||||
// String operations
|
||||
//
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
try {
|
||||
@@ -231,6 +235,66 @@ public class JredisConnection implements RedisConnection {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Integer append(String key, String value) {
|
||||
try {
|
||||
return Integer.valueOf((int) jredis.append(key, value));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> mGet(String... keys) {
|
||||
try {
|
||||
return JredisUtils.convertToStringCollection(jredis.mget(keys), encoding, List.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mSet(String[] keys, String[] values) {
|
||||
try {
|
||||
jredis.mset(JredisUtils.convert(keys, values));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mSetNX(String[] keys, String[] values) {
|
||||
try {
|
||||
jredis.msetnx(JredisUtils.convert(keys, values));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEx(String key, int seconds, String value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean setNX(String key, String value) {
|
||||
try {
|
||||
return jredis.setnx(key, value);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String substr(String key, int start, int end) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.substr(key, (long) start, (long) end), encoding);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer decr(String key) {
|
||||
try {
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.datastore.redis.connection.jredis;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -44,7 +45,7 @@ public abstract class JredisUtils {
|
||||
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
public static String convertToString(byte[] bytes, String encoding) {
|
||||
static String convertToString(byte[] bytes, String encoding) {
|
||||
try {
|
||||
return new String(bytes, encoding);
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
@@ -97,4 +98,13 @@ public abstract class JredisUtils {
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
static Map<String, byte[]> convert(String[] keys, String[] values) {
|
||||
Map<String, byte[]> result = new LinkedHashMap<String, byte[]>(keys.length);
|
||||
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
result.put(keys[i], values[i].getBytes());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user