Skip to content

Commit dedc8d1

Browse files
committed
Merge branch 'feature/exists' of https://github.com/charsyam/jedis into charsyam-feature/exists
Conflicts: src/main/java/redis/clients/jedis/BinaryJedis.java src/main/java/redis/clients/jedis/Client.java src/main/java/redis/clients/jedis/Jedis.java
2 parents 9e95ce1 + b540775 commit dedc8d1

15 files changed

Lines changed: 106 additions & 3 deletions

src/main/java/redis/clients/jedis/BinaryClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public void quit() {
114114
sendCommand(QUIT);
115115
}
116116

117+
public void exists(final byte[]... keys) {
118+
sendCommand(EXISTS, keys);
119+
}
120+
121+
@Deprecated
117122
public void exists(final byte[] key) {
118123
sendCommand(EXISTS, key);
119124
}

src/main/java/redis/clients/jedis/BinaryJedis.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,28 @@ public String quit() {
174174
return quitReturn;
175175
}
176176

177+
/**
178+
* Test if the specified keys exist. The command returns the number of keys existed
179+
* Time complexity: O(N)
180+
* @param keys
181+
* @return Integer reply, specifically: an integer greater than 0 if one or more keys existed
182+
* 0 if none of the specified keys existed
183+
*/
184+
public Long exists(final byte[]... keys) {
185+
checkIsInMultiOrPipeline();
186+
client.exists(keys);
187+
return client.getIntegerReply();
188+
}
189+
177190
/**
178191
* Test if the specified key exists. The command returns "1" if the key exists, otherwise "0" is
179192
* returned. Note that even keys set with an empty string as value will return "1". Time
180193
* complexity: O(1)
181194
* @param key
182-
* @return Integer reply, "1" if the key exists, otherwise "0"
195+
* @return Boolean reply, true if the key exists, otherwise false
183196
*/
184197
@Override
198+
@Deprecated
185199
public Boolean exists(final byte[] key) {
186200
checkIsInMultiOrPipeline();
187201
client.exists(key);

src/main/java/redis/clients/jedis/BinaryJedisCluster.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public byte[] execute(Jedis connection) {
9999
}.runBinary(key);
100100
}
101101

102+
@Override
103+
public Long exists(final byte[]... keys) {
104+
return new JedisClusterCommand<Long>(connectionHandler, maxRedirections) {
105+
@Override
106+
public Long execute(Jedis connection) {
107+
return connection.exists(keys);
108+
}
109+
}.runBinary(keys.length, keys);
110+
}
111+
102112
@Override
103113
public Boolean exists(final byte[] key) {
104114
return new JedisClusterCommand<Boolean>(connectionHandler, maxRedirections) {

src/main/java/redis/clients/jedis/Client.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ public void get(final String key) {
4545
}
4646

4747
@Override
48+
@Deprecated
4849
public void exists(final String key) {
4950
exists(SafeEncoder.encode(key));
5051
}
5152

5253
@Override
54+
public void exists(final String... keys) {
55+
final byte[][] bkeys = SafeEncoder.encodeMany(keys);
56+
exists(bkeys);
57+
}
58+
5359
public void del(final String... keys) {
5460
final byte[][] bkeys = new byte[keys.length][];
5561
for (int i = 0; i < keys.length; i++) {

src/main/java/redis/clients/jedis/Jedis.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ public String get(final String key) {
107107
return client.getBulkReply();
108108
}
109109

110+
/**
111+
* Test if the specified key exists. The command returns the number of keys existed
112+
* Time complexity: O(N)
113+
* @param keys
114+
* @return Integer reply, specifically: an integer greater than 0 if one or more keys were removed
115+
* 0 if none of the specified key existed
116+
*/
117+
public Long exists(final String... keys) {
118+
checkIsInMultiOrPipeline();
119+
client.exists(keys);
120+
return client.getIntegerReply();
121+
}
122+
110123
/**
111124
* Test if the specified key exists. The command returns "1" if the key exists, otherwise "0" is
112125
* returned. Note that even keys set with an empty string as value will return "1". Time
@@ -115,6 +128,7 @@ public String get(final String key) {
115128
* @return Boolean reply, true if the key exists, otherwise false
116129
*/
117130
@Override
131+
@Deprecated
118132
public Boolean exists(final String key) {
119133
checkIsInMultiOrPipeline();
120134
client.exists(key);

src/main/java/redis/clients/jedis/JedisCluster.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public String execute(Jedis connection) {
8383
}.run(key);
8484
}
8585

86+
@Deprecated
8687
@Override
8788
public Boolean exists(final String key) {
8889
return new JedisClusterCommand<Boolean>(connectionHandler, maxRedirections) {
@@ -93,6 +94,16 @@ public Boolean execute(Jedis connection) {
9394
}.run(key);
9495
}
9596

97+
@Override
98+
public Long exists(final String... keys) {
99+
return new JedisClusterCommand<Long>(connectionHandler, maxRedirections) {
100+
@Override
101+
public Long execute(Jedis connection) {
102+
return connection.exists(keys);
103+
}
104+
}.run(keys.length, keys);
105+
}
106+
96107
@Override
97108
public Long persist(final String key) {
98109
return new JedisClusterCommand<Long>(connectionHandler, maxRedirections) {

src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public Response<Long> del(byte[]... keys) {
7272
return getResponse(BuilderFactory.LONG);
7373
}
7474

75+
public Response<Long> exists(String... keys) {
76+
client.exists(keys);
77+
return getResponse(BuilderFactory.LONG);
78+
}
79+
80+
public Response<Long> exists(byte[]... keys) {
81+
client.exists(keys);
82+
return getResponse(BuilderFactory.LONG);
83+
}
84+
7585
public Response<Set<String>> keys(String pattern) {
7686
getClient(pattern).keys(pattern);
7787
return getResponse(BuilderFactory.STRING_SET);

src/main/java/redis/clients/jedis/commands/Commands.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ public interface Commands {
1919

2020
public void get(final String key);
2121

22+
@Deprecated
2223
public void exists(final String key);
2324

25+
public void exists(final String... keys);
26+
2427
public void del(final String... keys);
2528

2629
public void type(final String key);

src/main/java/redis/clients/jedis/commands/MultiKeyBinaryCommands.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
public interface MultiKeyBinaryCommands {
1212
Long del(byte[]... keys);
1313

14+
Long exists(byte[]... keys);
15+
1416
List<byte[]> blpop(int timeout, byte[]... keys);
1517

1618
List<byte[]> brpop(int timeout, byte[]... keys);

src/main/java/redis/clients/jedis/commands/MultiKeyBinaryJedisClusterCommands.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
public interface MultiKeyBinaryJedisClusterCommands {
1212
Long del(byte[]... keys);
1313

14+
Long exists(byte[]... keys);
15+
1416
List<byte[]> blpop(int timeout, byte[]... keys);
1517

1618
List<byte[]> brpop(int timeout, byte[]... keys);
@@ -66,4 +68,4 @@ public interface MultiKeyBinaryJedisClusterCommands {
6668
String pfmerge(final byte[] destkey, final byte[]... sourcekeys);
6769

6870
Long pfcount(byte[]... keys);
69-
}
71+
}

0 commit comments

Comments
 (0)