diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index 812defe..2534f25 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -6,6 +6,9 @@ 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.STORE; +import static redis.clients.jedis.Protocol.Keyword.REFCOUNT; +import static redis.clients.jedis.Protocol.Keyword.ENCODING; +import static redis.clients.jedis.Protocol.Keyword.IDLETIME; import static redis.clients.jedis.Protocol.Keyword.WITHSCORES; import java.util.ArrayList; @@ -718,4 +721,16 @@ public class BinaryClient extends Connection { db = 0; super.disconnect(); } + + public void objectRefcount(byte[] key) { + sendCommand(OBJECT, REFCOUNT.raw, key); + } + + public void objectIdletime(byte[] key) { + sendCommand(OBJECT, IDLETIME.raw, key); + } + + public void objectEncoding(byte[] key) { + sendCommand(OBJECT, ENCODING.raw, key); + } } diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 7b4cd30..136c21d 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -3006,4 +3006,19 @@ public class BinaryJedis implements BinaryJedisCommands { public Long getDB() { return client.getDB(); } + + public Long objectRefcount(byte[] key) { + client.objectRefcount(key); + return client.getIntegerReply(); + } + + public byte[] objectEncoding(byte[] key) { + client.objectEncoding(key); + return client.getBinaryBulkReply(); + } + + public Long objectIdletime(byte[] key) { + client.objectIdletime(key); + return client.getIntegerReply(); + } } diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java index ef8aebd..cfaefe7 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCommands.java @@ -154,4 +154,10 @@ public interface BinaryJedisCommands { Long zremrangeByScore(byte[] key, double start, double end); Long linsert(byte[] key, LIST_POSITION where, byte[] pivot, byte[] value); + + Long objectRefcount(byte[] key); + + Long objectIdletime(byte[] key); + + byte[] objectEncoding(byte[] key); } diff --git a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java index 9c1abd2..48a243d 100644 --- a/src/main/java/redis/clients/jedis/BinaryShardedJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryShardedJedis.java @@ -407,4 +407,19 @@ public class BinaryShardedJedis extends Sharded pipeline.setShardedJedis(this); return pipeline; } + + public Long objectRefcount(byte[] key) { + Jedis j = getShard(key); + return j.objectRefcount(key); + } + + public byte[] objectEncoding(byte[] key) { + Jedis j = getShard(key); + return j.objectEncoding(key); + } + + public Long objectIdletime(byte[] key) { + Jedis j = getShard(key); + return j.objectIdletime(key); + } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 1f493e0..738bed2 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -595,4 +595,16 @@ public class Client extends BinaryClient implements Commands { public void configGet(String pattern) { configGet(SafeEncoder.encode(pattern)); } + + public void objectRefcount(String key) { + objectRefcount(SafeEncoder.encode(key)); + } + + public void objectIdletime(String key) { + objectIdletime(SafeEncoder.encode(key)); + } + + public void objectEncoding(String key) { + objectEncoding(SafeEncoder.encode(key)); + } } diff --git a/src/main/java/redis/clients/jedis/Commands.java b/src/main/java/redis/clients/jedis/Commands.java index 235ee88..0d17d25 100644 --- a/src/main/java/redis/clients/jedis/Commands.java +++ b/src/main/java/redis/clients/jedis/Commands.java @@ -3,6 +3,7 @@ package redis.clients.jedis; import java.util.Map; import redis.clients.jedis.BinaryClient.LIST_POSITION; +import redis.clients.util.SafeEncoder; public interface Commands { @@ -259,4 +260,10 @@ public interface Commands { public void exec(); public void discard(); + + public void objectRefcount(String key); + + public void objectIdletime(String key); + + public void objectEncoding(String key); } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index c71d9aa..081cb69 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -2688,5 +2688,20 @@ public class Jedis extends BinaryJedis implements JedisCommands { public String configSet(final String parameter, final String value) { client.configSet(parameter, value); return client.getStatusCodeReply(); - } + } + + public Long objectRefcount(String string) { + client.objectRefcount(string); + return client.getIntegerReply(); + } + + public String objectEncoding(String string) { + client.objectEncoding(string); + return client.getBulkReply(); + } + + public Long objectIdletime(String string) { + client.objectIdletime(string); + return client.getIntegerReply(); + } } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index a4f1c7e..0baee7f 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -140,7 +140,7 @@ public final class Protocol { } public static enum Command { - PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE; + PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, OBJECT; public final byte[] raw; @@ -150,7 +150,7 @@ public final class Protocol { } public static enum Keyword { - AGGREGATE, ALPHA, ASC, BY, DESC, GET, LIMIT, MESSAGE, NO, NOSORT, PMESSAGE, PSUBSCRIBE, PUNSUBSCRIBE, OK, ONE, QUEUED, SET, STORE, SUBSCRIBE, UNSUBSCRIBE, WEIGHTS, WITHSCORES, RESETSTAT; + AGGREGATE, ALPHA, ASC, BY, DESC, GET, LIMIT, MESSAGE, NO, NOSORT, PMESSAGE, PSUBSCRIBE, PUNSUBSCRIBE, OK, ONE, QUEUED, SET, STORE, SUBSCRIBE, UNSUBSCRIBE, WEIGHTS, WITHSCORES, RESETSTAT, REFCOUNT, ENCODING, IDLETIME; public final byte[] raw; Keyword() { diff --git a/src/test/java/redis/clients/jedis/tests/commands/ObjectCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ObjectCommandsTest.java new file mode 100644 index 0000000..cbab817 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/ObjectCommandsTest.java @@ -0,0 +1,48 @@ +package redis.clients.jedis.tests.commands; + +import org.junit.Test; + +import redis.clients.util.SafeEncoder; + +public class ObjectCommandsTest extends JedisCommandTestBase { + + private String key = "key"; + private byte[] binaryKey = SafeEncoder.encode(key); + + @Test + public void objectRefcount() { + jedis.set(key, "test"); + Long refcount = jedis.objectRefcount(key); + assertEquals(new Long(1), refcount); + + //Binary + refcount = jedis.objectRefcount(binaryKey); + assertEquals(new Long(1), refcount); + + } + + @Test + public void objectEncoding() { + jedis.set(key, "test"); + String encoding = jedis.objectEncoding(key); + assertEquals("raw", encoding); + + //Binary + encoding = SafeEncoder.encode(jedis.objectEncoding(binaryKey)); + assertEquals("raw", encoding); + } + + @Test + public void objectIdletime() throws InterruptedException { + jedis.set(key, "test"); + + //Wait a little bit more than 10 seconds so the idle time is 10 seconds. + Thread.sleep(10001); + Long time = jedis.objectIdletime(key); + assertEquals(new Long(10), time); + + //Binary + time = jedis.objectIdletime(binaryKey); + assertEquals(new Long(10), time); + } +} \ No newline at end of file