support for new 'set' command (redis 2.6.12+)

This commit is contained in:
amitm
2013-05-13 12:40:24 +03:00
parent 62c30dfe8b
commit 24c6b7c867
8 changed files with 152 additions and 56 deletions

View File

@@ -1,26 +1,22 @@
package redis.clients.jedis;
import static redis.clients.jedis.Protocol.toByteArray;
import static redis.clients.jedis.Protocol.Command.*;
import static redis.clients.jedis.Protocol.Keyword.ENCODING;
import static redis.clients.jedis.Protocol.Keyword.IDLETIME;
import static redis.clients.jedis.Protocol.Keyword.LEN;
import static redis.clients.jedis.Protocol.Keyword.LIMIT;
import static redis.clients.jedis.Protocol.Keyword.NO;
import static redis.clients.jedis.Protocol.Keyword.ONE;
import static redis.clients.jedis.Protocol.Keyword.REFCOUNT;
import static redis.clients.jedis.Protocol.Keyword.RESET;
import static redis.clients.jedis.Protocol.Keyword.STORE;
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;
import redis.clients.jedis.Protocol.Command;
import redis.clients.jedis.Protocol.Keyword;
import redis.clients.util.SafeEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import redis.clients.jedis.Protocol.Command;
import redis.clients.jedis.Protocol.Keyword;
import redis.clients.util.SafeEncoder;
import static redis.clients.jedis.Protocol.Command.*;
import static redis.clients.jedis.Protocol.Command.EXISTS;
import static redis.clients.jedis.Protocol.Command.PSUBSCRIBE;
import static redis.clients.jedis.Protocol.Command.PUNSUBSCRIBE;
import static redis.clients.jedis.Protocol.Command.SUBSCRIBE;
import static redis.clients.jedis.Protocol.Command.UNSUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.*;
import static redis.clients.jedis.Protocol.toByteArray;
public class BinaryClient extends Connection {
public enum LIST_POSITION {
@@ -86,6 +82,11 @@ public class BinaryClient extends Connection {
sendCommand(Command.SET, key, value);
}
public void set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx, final long time) {
sendCommand(Command.SET, key, value, nxxx, expx, toByteArray(time));
}
public void get(final byte[] key) {
sendCommand(Command.GET, key);
}

View File

@@ -1,22 +1,16 @@
package redis.clients.jedis;
import static redis.clients.jedis.Protocol.toByteArray;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.JedisByteHashMap;
import redis.clients.util.SafeEncoder;
import java.net.URI;
import java.util.*;
import static redis.clients.jedis.Protocol.toByteArray;
public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands, AdvancedBinaryJedisCommands, BinaryScriptingCommands {
protected Client client = null;
@@ -78,6 +72,23 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKey
return client.getStatusCodeReply();
}
/**
* Set the string value as value of the key. The string can't be longer than
* 1073741824 bytes (1 GB).
* @param key
* @param value
* @param nxxx NX|XX, NX -- Only set the key if it does not already exist.
* XX -- Only set the key if it already exist.
* @param expx EX|PX, expire time units: EX = seconds; PX = milliseconds
* @param time expire time in the units of {@param #expx}
* @return Status code reply
*/
public String set(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx, final long time) {
checkIsInMulti();
client.set(key, value, nxxx, expx, time);
return client.getStatusCodeReply();
}
/**
* Get the value of the specified key. If the key does not exist the special
* value 'nil' is returned. If the value stored at key is not a string an

View File

@@ -1,12 +1,12 @@
package redis.clients.jedis;
import static redis.clients.jedis.Protocol.toByteArray;
import redis.clients.util.SafeEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import redis.clients.util.SafeEncoder;
import static redis.clients.jedis.Protocol.toByteArray;
public class Client extends BinaryClient implements Commands {
public Client(final String host) {
@@ -21,6 +21,10 @@ public class Client extends BinaryClient implements Commands {
set(SafeEncoder.encode(key), SafeEncoder.encode(value));
}
public void set(final String key, final String value, final String nxxx, final String expx, final long time) {
set(SafeEncoder.encode(key), SafeEncoder.encode(value), SafeEncoder.encode(nxxx), SafeEncoder.encode(expx), time);
}
public void get(final String key) {
get(SafeEncoder.encode(key));
}

View File

@@ -1,13 +1,15 @@
package redis.clients.jedis;
import java.util.Map;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import java.util.Map;
public interface Commands {
public void set(final String key, final String value);
public void set(final String key, final String value, final String nxxx, final String expx, final long time);
public void get(final String key);
public void exists(final String key);

View File

@@ -1,19 +1,12 @@
package redis.clients.jedis;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.SafeEncoder;
import redis.clients.util.Slowlog;
import java.net.URI;
import java.util.*;
public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands {
public Jedis(final String host) {
super(host);
@@ -51,6 +44,23 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand
return client.getStatusCodeReply();
}
/**
* Set the string value as value of the key. The string can't be longer than
* 1073741824 bytes (1 GB).
* @param key
* @param value
* @param nxxx NX|XX, NX -- Only set the key if it does not already exist.
* XX -- Only set the key if it already exist.
* @param expx EX|PX, expire time units: EX = seconds; PX = milliseconds
* @param time expire time in the units of {@param #expx}
* @return Status code reply
*/
public String set(final String key, final String value, final String nxxx, final String expx, final long time) {
checkIsInMulti();
client.set(key, value, nxxx, expx, time);
return client.getStatusCodeReply();
}
/**
* Get the value of the specified key. If the key does not exist the special
* value 'nil' is returned. If the value stored at key is not a string an

View File

@@ -7,7 +7,8 @@ import java.util.Set;
/**
* Common interface for sharded and non-sharded Jedis
*/
public interface JedisCommands {
public interface
JedisCommands {
String set(String key, String value);
String get(String key);

View File

@@ -1,18 +1,23 @@
package redis.clients.jedis.tests.commands;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Protocol.Keyword;
import redis.clients.jedis.exceptions.JedisDataException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Protocol.Keyword;
import redis.clients.jedis.exceptions.JedisDataException;
public class BinaryValuesCommandsTest extends JedisCommandTestBase {
byte[] bfoo = { 0x01, 0x02, 0x03, 0x04 };
byte[] bbar = { 0x05, 0x06, 0x07, 0x08 };
byte[] bxx = { 0x78, 0x78 };
byte[] bnx = { 0x6E, 0x78 };
byte[] bex = { 0x65, 0x78 };
byte[] bpx = { 0x70, 0x78 };
long expireSeconds = 2;
long expireMillis = expireSeconds * 1000;
byte[] binaryValue;
@Before
@@ -37,6 +42,69 @@ public class BinaryValuesCommandsTest extends JedisCommandTestBase {
assertNull(jedis.get(bbar));
}
@Test
public void setNxExAndGet() {
String status = jedis.set(bfoo, binaryValue, bnx, bex, expireSeconds);
assertTrue(Keyword.OK.name().equalsIgnoreCase(status));
byte[] value = jedis.get(bfoo);
assertTrue(Arrays.equals(binaryValue, value));
assertNull(jedis.get(bbar));
}
@Test
public void setIfNotExistAndGet() {
String status= jedis.set(bfoo, binaryValue);
assertTrue(Keyword.OK.name().equalsIgnoreCase(status));
// nx should fail if value exists
String statusFail = jedis.set(bfoo, binaryValue, bnx, bex, expireSeconds);
assertNull(statusFail);
byte[] value = jedis.get(bfoo);
assertTrue(Arrays.equals(binaryValue, value));
assertNull(jedis.get(bbar));
}
@Test
public void setIfExistAndGet() {
String status= jedis.set(bfoo, binaryValue);
assertTrue(Keyword.OK.name().equalsIgnoreCase(status));
// nx should fail if value exists
String statusSuccess = jedis.set(bfoo, binaryValue, bxx, bex, expireSeconds);
assertTrue(Keyword.OK.name().equalsIgnoreCase(statusSuccess));
byte[] value = jedis.get(bfoo);
assertTrue(Arrays.equals(binaryValue, value));
assertNull(jedis.get(bbar));
}
@Test
public void setFailIfNotExistAndGet() {
// xx should fail if value does NOT exists
String statusFail = jedis.set(bfoo, binaryValue, bxx, bex, expireSeconds);
assertNull(statusFail);
}
@Test
public void setAndExpireMillis() {
String status = jedis.set(bfoo, binaryValue, bnx, bpx, expireMillis);
assertTrue(Keyword.OK.name().equalsIgnoreCase(status));
long ttl = jedis.ttl(bfoo);
assertTrue(ttl > 0 && ttl <= expireSeconds);
}
@Test
public void setAndExpire() {
String status = jedis.set(bfoo, binaryValue, bnx, bex, expireSeconds);
assertTrue(Keyword.OK.name().equalsIgnoreCase(status));
long ttl = jedis.ttl(bfoo);
assertTrue(ttl > 0 && ttl <= expireSeconds);
}
@Test
public void getSet() {
byte[] value = jedis.getSet(bfoo, binaryValue);

View File

@@ -1,19 +1,18 @@
package redis.clients.jedis.tests.commands;
import org.junit.After;
import org.junit.Before;
import org.junit.ComparisonFailure;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import redis.clients.jedis.tests.JedisTestBase;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.ComparisonFailure;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.JedisTestBase;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public abstract class JedisCommandTestBase extends JedisTestBase {
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);