Added commands operating on strings
This commit is contained in:
@@ -7,7 +7,6 @@ Jedis was conceived to be EASY to use and FULLY COMPATIBLE with the latest versi
|
||||
Jedis is a WORK IN PROGRESS.
|
||||
|
||||
## What's still missing?
|
||||
- Commands operating on string values
|
||||
- Commands operating on lists
|
||||
- Commands operating on sets
|
||||
- Commands operating on sorted sets
|
||||
@@ -24,7 +23,7 @@ But stay close because things are going fast and all this will be implemented so
|
||||
## Ok.. so what's already done?
|
||||
- Connection handling (not AUTH)
|
||||
- Commands operating on all the kind of values
|
||||
- Commands operating on string values (just GET and SET)
|
||||
- Commands operating on string values
|
||||
|
||||
## How do I use it?
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public class Jedis extends Client {
|
||||
}
|
||||
|
||||
public String randomKey() throws JedisException {
|
||||
return sendCommand("RANDOMKEY").getStatusCodeReply();
|
||||
return sendCommand("RANDOMKEY").getBulkReply();
|
||||
}
|
||||
|
||||
public String rename(String oldkey, String newkey) throws JedisException {
|
||||
@@ -86,4 +86,57 @@ public class Jedis extends Client {
|
||||
public String flushAll() throws JedisException {
|
||||
return sendCommand("FLUSHALL").getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String getSet(String key, String value) throws JedisException {
|
||||
return sendCommand("GETSET", key, value).getBulkReply();
|
||||
}
|
||||
|
||||
public List<String> mget(String... keys) throws JedisException {
|
||||
return sendCommand("MGET", keys).getMultiBulkReply();
|
||||
}
|
||||
|
||||
public int setnx(String key, String value) throws JedisException {
|
||||
return sendCommand("SETNX", key, value).getIntegerReply();
|
||||
}
|
||||
|
||||
public String setex(String key, int seconds, String value)
|
||||
throws JedisException {
|
||||
return sendCommand("SETEX", key, String.valueOf(seconds), value)
|
||||
.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String mset(String... keysvalues) throws JedisException {
|
||||
return sendCommand("MSET", keysvalues).getStatusCodeReply();
|
||||
}
|
||||
|
||||
public int msetnx(String... keysvalues) throws JedisException {
|
||||
return sendCommand("MSETNX", keysvalues).getIntegerReply();
|
||||
}
|
||||
|
||||
public int decrBy(String key, int integer) throws JedisException {
|
||||
return sendCommand("DECRBY", key, String.valueOf(integer))
|
||||
.getIntegerReply();
|
||||
}
|
||||
|
||||
public int decr(String key) throws JedisException {
|
||||
return sendCommand("DECR", key).getIntegerReply();
|
||||
}
|
||||
|
||||
public int incrBy(String key, int integer) throws JedisException {
|
||||
return sendCommand("INCRBY", key, String.valueOf(integer))
|
||||
.getIntegerReply();
|
||||
}
|
||||
|
||||
public int incr(String key) throws JedisException {
|
||||
return sendCommand("INCR", key).getIntegerReply();
|
||||
}
|
||||
|
||||
public int append(String key, String value) throws JedisException {
|
||||
return sendCommand("APPEND", key, value).getIntegerReply();
|
||||
}
|
||||
|
||||
public String substr(String key, int start, int end) throws JedisException {
|
||||
return sendCommand("SUBSTR", key, String.valueOf(start),
|
||||
String.valueOf(end)).getBulkReply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ public class AllKindOfValuesCommandsTest extends Assert {
|
||||
|
||||
@Test
|
||||
public void randomKey() throws JedisException {
|
||||
assertEquals("", jedis.randomKey());
|
||||
assertEquals(null, jedis.randomKey());
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.After;
|
||||
@@ -31,6 +34,158 @@ public class StringValuesCommandsTest extends Assert {
|
||||
|
||||
String value = jedis.get("foo");
|
||||
assertEquals("bar", value);
|
||||
|
||||
assertEquals(null, jedis.get("bar"));
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void getSet() throws JedisException {
|
||||
String value = jedis.getSet("foo", "bar");
|
||||
assertEquals(null, value);
|
||||
value = jedis.get("foo");
|
||||
assertEquals("bar", value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mget() throws JedisException {
|
||||
List<String> values = jedis.mget("foo", "bar");
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add(null);
|
||||
expected.add(null);
|
||||
|
||||
assertEquals(expected, values);
|
||||
|
||||
jedis.set("foo", "bar");
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
expected.add("bar");
|
||||
expected.add(null);
|
||||
values = jedis.mget("foo", "bar");
|
||||
|
||||
assertEquals(expected, values);
|
||||
|
||||
jedis.set("bar", "foo");
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
expected.add("bar");
|
||||
expected.add("foo");
|
||||
values = jedis.mget("foo", "bar");
|
||||
|
||||
assertEquals(expected, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setnx() throws JedisException {
|
||||
int status = jedis.setnx("foo", "bar");
|
||||
assertEquals(1, status);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
|
||||
status = jedis.setnx("foo", "bar2");
|
||||
assertEquals(0, status);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setex() throws JedisException {
|
||||
String status = jedis.setex("foo", 20, "bar");
|
||||
assertEquals("OK", status);
|
||||
int ttl = jedis.ttl("foo");
|
||||
assertTrue(ttl > 0 && ttl <= 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mset() throws JedisException {
|
||||
String status = jedis.mset("foo", "bar", "bar", "foo");
|
||||
assertEquals("OK", status);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
assertEquals("foo", jedis.get("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void msetnx() throws JedisException {
|
||||
int status = jedis.msetnx("foo", "bar", "bar", "foo");
|
||||
assertEquals(1, status);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
assertEquals("foo", jedis.get("bar"));
|
||||
|
||||
status = jedis.msetnx("foo", "bar1", "bar2", "foo2");
|
||||
assertEquals(0, status);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
assertEquals("foo", jedis.get("bar"));
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void incrWrongValue() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.incr("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incr() throws JedisException {
|
||||
int value = jedis.incr("foo");
|
||||
assertEquals(1, value);
|
||||
value = jedis.incr("foo");
|
||||
assertEquals(2, value);
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void incrByWrongValue() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.incrBy("foo", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incrBy() throws JedisException {
|
||||
int value = jedis.incrBy("foo", 2);
|
||||
assertEquals(2, value);
|
||||
value = jedis.incrBy("foo", 2);
|
||||
assertEquals(4, value);
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void decrWrongValue() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.decr("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decr() throws JedisException {
|
||||
int value = jedis.decr("foo");
|
||||
assertEquals(-1, value);
|
||||
value = jedis.decr("foo");
|
||||
assertEquals(-2, value);
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void decrByWrongValue() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.decrBy("foo", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decrBy() throws JedisException {
|
||||
int value = jedis.decrBy("foo", 2);
|
||||
assertEquals(-2, value);
|
||||
value = jedis.decrBy("foo", 2);
|
||||
assertEquals(-4, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void append() throws JedisException {
|
||||
int value = jedis.append("foo", "bar");
|
||||
assertEquals(3, value);
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
value = jedis.append("foo", "bar");
|
||||
assertEquals(6, value);
|
||||
assertEquals("barbar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void substr() throws JedisException {
|
||||
jedis.set("s", "This is a string");
|
||||
assertEquals("This", jedis.substr("s", 0, 3));
|
||||
assertEquals("ing", jedis.substr("s", -3, -1));
|
||||
assertEquals("This is a string", jedis.substr("s", 0, -1));
|
||||
assertEquals(" string", jedis.substr("s", 9, 100000));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user