Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d555512

Browse files
AnneMayormp911de
authored andcommitted
Add exists(...) accepting multiple keys to Reactive API.
Closes #2883 Original pull request: #2918 Signed-off-by: Anne Lee <anne.lee@buzzvil.com>
1 parent 1f360e4 commit d555512

File tree

6 files changed

+86
-7
lines changed

6 files changed

+86
-7
lines changed

‎src/main/java/org/springframework/data/redis/connection/ReactiveKeyCommands.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ default Mono<Boolean> exists(ByteBuffer key) {
177177

178178
return exists(Mono.just(new KeyCommand(key))).next().map(BooleanResponse::getOutput);
179179
}
180+
/**
181+
* Determine the number of given {@literal keys} that exist.
182+
*
183+
* @param keys must not be {@literal null} or {@literal empty}.
184+
* @return {@link Mono} emitting {@literal the number of existing keys}.
185+
* @see <a href="https://redis.io/docs/commands/exists/">Redis Documentation: EXISTS</a>
186+
*/
187+
Mono<Long> exists(List<ByteBuffer> keys);
180188

181189
/**
182190
* Determine if given {@literal key} exists.

‎src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommands.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ public Mono<Long> refcount(ByteBuffer key) {
344344
return connection.execute(cmd -> cmd.objectRefcount(key)).next();
345345
}
346346

347+
@Override
348+
public Mono<Long> exists(List<ByteBuffer> keys) {
349+
Assert.notNull(keys, "Key list must not be null");
350+
Assert.notEmpty(keys, "Key list must not be empty");
351+
352+
return connection.execute(cmd -> cmd.exists(keys.toArray(ByteBuffer[]::new))).next();
353+
}
354+
347355
private static ExpireArgs getExpireArgs(ExpirationOptions options) {
348356

349357
return new ExpireArgs() {

‎src/main/java/org/springframework/data/redis/core/ReactiveRedisOperations.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.time.Duration;
2323
import java.time.Instant;
2424
import java.util.Arrays;
25+
import java.util.Collection;
2526
import java.util.Collections;
2627
import java.util.List;
2728

@@ -421,6 +422,15 @@ default Flux<K> scan() {
421422
*/
422423
Mono<Duration> getExpire(K key);
423424

425+
/**
426+
* Get the number of given {@code keys} that exists.
427+
*
428+
* @param keys must not be {@literal null} or {@literal empty}.
429+
* @return the number of existing keys in redis. 0 if there are no existing keys.
430+
* @see <a href="https://redis.io/docs/commands/exists/">Redis Documentation: EXISTS</a>
431+
*/
432+
Mono<Long> countExistingKeys(Collection<K> keys);
433+
424434
// -------------------------------------------------------------------------
425435
// Methods dealing with Redis Lua scripts
426436
// -------------------------------------------------------------------------

‎src/main/java/org/springframework/data/redis/core/ReactiveRedisTemplate.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.time.Duration;
2424
import java.time.Instant;
2525
import java.util.Arrays;
26+
import java.util.Collection;
2627
import java.util.List;
2728
import java.util.stream.Collectors;
2829

@@ -505,6 +506,14 @@ public Mono<Boolean> move(K key, int dbIndex) {
505506
return doCreateMono(connection -> connection.keyCommands().move(rawKey(key), dbIndex));
506507
}
507508

509+
@Override
510+
public Mono<Long> countExistingKeys(Collection<K> keys) {
511+
Assert.notNull(keys, "Keys must not be null");
512+
513+
ByteBuffer[] rawKeys = rawKeys(keys);
514+
return doCreateMono(connection -> connection.keyCommands().exists(Arrays.asList(rawKeys)));
515+
}
516+
508517
// -------------------------------------------------------------------------
509518
// Methods dealing with Redis Lua scripts
510519
// -------------------------------------------------------------------------
@@ -688,6 +697,17 @@ private ByteBuffer rawKey(K key) {
688697
return getSerializationContext().getKeySerializationPair().getWriter().write(key);
689698
}
690699

700+
private ByteBuffer[] rawKeys(Collection<K> keys) {
701+
final ByteBuffer[] rawKeys = new ByteBuffer[keys.size()];
702+
703+
int i = 0;
704+
for (K key : keys) {
705+
rawKeys[i++] = rawKey(key);
706+
}
707+
708+
return rawKeys;
709+
}
710+
691711
@Nullable
692712
private K readKey(ByteBuffer buffer) {
693713
return getSerializationContext().getKeySerializationPair().getReader().read(buffer);

‎src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveKeyCommandsIntegrationTests.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ void existsShouldReturnFalseForNonExistingKeys() {
7070
connection.keyCommands().exists(KEY_1_BBUFFER).as(StepVerifier::create).expectNext(false).verifyComplete();
7171
}
7272

73+
@ParameterizedRedisTest
74+
void existsKeyReturnsKeyCount() {
75+
nativeCommands.set(KEY_1, "1000");
76+
nativeCommands.set(KEY_2, "2000");
77+
nativeCommands.set(KEY_3, "3000");
78+
79+
connection.keyCommands().exists(List.of(KEY_1_BBUFFER, KEY_2_BBUFFER, KEY_3_BBUFFER)).as(StepVerifier::create)
80+
.expectNext(3L).verifyComplete();
81+
}
82+
83+
@ParameterizedRedisTest
84+
void existsKeyReturnsZeroWhenKeyDoesNotExist() {
85+
connection.keyCommands().exists(List.of(KEY_1_BBUFFER, KEY_2_BBUFFER, KEY_3_BBUFFER)).as(StepVerifier::create)
86+
.expectNext(0L).verifyComplete();
87+
}
88+
7389
@ParameterizedRedisTest // DATAREDIS-525
7490
void typeShouldReturnTypeCorrectly() {
7591

@@ -168,7 +184,7 @@ void renameShouldAlterKeyNameCorrectly() {
168184
connection.keyCommands().rename(KEY_1_BBUFFER, KEY_2_BBUFFER).as(StepVerifier::create).expectNext(true)
169185
.verifyComplete();
170186
assertThat(nativeCommands.exists(KEY_2)).isEqualTo(1L);
171-
assertThat(nativeCommands.exists(KEY_1)).isEqualTo(0L);
187+
assertThat(nativeCommands.exists(KEY_1)).isZero();
172188
}
173189

174190
@ParameterizedRedisTest // DATAREDIS-525
@@ -187,7 +203,7 @@ void renameNXShouldAlterKeyNameCorrectly() {
187203
.verifyComplete();
188204

189205
assertThat(nativeCommands.exists(KEY_2)).isEqualTo(1L);
190-
assertThat(nativeCommands.exists(KEY_1)).isEqualTo(0L);
206+
assertThat(nativeCommands.exists(KEY_1)).isZero();
191207
}
192208

193209
@ParameterizedRedisTest // DATAREDIS-525
@@ -428,7 +444,7 @@ void shouldMoveToDatabase() {
428444
.expectNext(true) //
429445
.expectComplete() //
430446
.verify();
431-
assertThat(nativeCommands.exists(KEY_1)).isEqualTo(0L);
447+
assertThat(nativeCommands.exists(KEY_1)).isZero();
432448
}
433449

434450
@ParameterizedRedisTest // DATAREDIS-694

‎src/test/java/org/springframework/data/redis/core/ReactiveRedisTemplateIntegrationTests.java‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424

2525
import java.time.Duration;
2626
import java.time.Instant;
27-
import java.util.Collection;
28-
import java.util.Collections;
29-
import java.util.HashMap;
30-
import java.util.Map;
27+
import java.util.*;
3128
import java.util.function.Function;
3229

3330
import org.junit.jupiter.api.BeforeEach;
@@ -582,4 +579,24 @@ void listenToPatternLaterShouldReceiveChannelMessagesCorrectly() {
582579
.thenCancel() //
583580
.verify(Duration.ofSeconds(3));
584581
}
582+
583+
@ParameterizedRedisTest
584+
void countExistingKeysIfValidKeyExists() {
585+
586+
K key = keyFactory.instance();
587+
K key2 = keyFactory.instance();
588+
K key3 = keyFactory.instance();
589+
590+
redisTemplate.opsForValue().set(key, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
591+
redisTemplate.opsForValue().set(key2, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
592+
redisTemplate.opsForValue().set(key3, valueFactory.instance()).as(StepVerifier::create).expectNext(true).verifyComplete();
593+
594+
redisTemplate.countExistingKeys(Arrays.asList(key, key2, key3)).as(StepVerifier::create).expectNext(3L).verifyComplete();
595+
}
596+
597+
@ParameterizedRedisTest
598+
void countExistingKeysIfNotValidKeyExists() {
599+
K key = keyFactory.instance();
600+
redisTemplate.countExistingKeys(List.of(key)).as(StepVerifier::create).expectNext(0L).verifyComplete();
601+
}
585602
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /