Skip to content

Commit 280d954

Browse files
Support ZRANDMEMBER via RedisZSet.
Add randomValue() method to both RedisZSet and RedisSet. See: #2049 Original Pull Request: #2104
1 parent 4c4c96e commit 280d954

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisSet.java

+9
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ public RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String
187187
return new DefaultRedisSet<>(boundSetOps.getOperations().boundSetOps(destKey));
188188
}
189189

190+
/*
191+
* (non-Javadoc)
192+
* @see org.springframework.data.redis.support.collections.RedisSet#randomElement()
193+
*/
194+
@Override
195+
public E randomValue() {
196+
return boundSetOps.randomMember();
197+
}
198+
190199
/*
191200
* (non-Javadoc)
192201
* @see java.util.AbstractCollection#add(java.lang.Object)

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java

+9
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ public RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, Strin
277277
return new DefaultRedisZSet<>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
278278
}
279279

280+
/*
281+
* (non-Javadoc)
282+
* @see org.springframework.data.redis.support.collections.RedisZSet#randomElement()
283+
*/
284+
@Override
285+
public E randomValue() {
286+
return boundZSetOps.randomMember();
287+
}
288+
280289
/*
281290
* (non-Javadoc)
282291
* @see org.springframework.data.redis.support.collections.RedisZSet#range(long, long)

src/main/java/org/springframework/data/redis/support/collections/RedisSet.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
8282
Set<E> diff(Collection<? extends RedisSet<?>> sets);
8383

8484
/**
85-
* Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in
86-
* destination {@code destKey}.
85+
* Create a new {@link RedisSet} by intersecting this sorted set and {@link RedisSet} and store result in destination
86+
* {@code destKey}.
8787
*
8888
* @param set must not be {@literal null}.
8989
* @param destKey must not be {@literal null}.
@@ -93,8 +93,8 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
9393
RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey);
9494

9595
/**
96-
* Create a new {@link RedisSet} by intersecting this sorted set and the collection {@link RedisSet} and store
97-
* result in destination {@code destKey}.
96+
* Create a new {@link RedisSet} by intersecting this sorted set and the collection {@link RedisSet} and store result
97+
* in destination {@code destKey}.
9898
*
9999
* @param sets must not be {@literal null}.
100100
* @param destKey must not be {@literal null}.
@@ -125,6 +125,14 @@ public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
125125
*/
126126
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
127127

128+
/**
129+
* Get random element from the set.
130+
*
131+
* @return
132+
* @since 2.6
133+
*/
134+
E randomValue();
135+
128136
/**
129137
* Create a new {@link RedisSet} by diffing this sorted set and {@link RedisSet} and store result in destination
130138
* {@code destKey}.

src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java

+8
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ static <E> RedisZSet<E> create(String key, RedisOperations<String, E> operations
236236
*/
237237
RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
238238

239+
/**
240+
* Get random element from the set.
241+
*
242+
* @return
243+
* @since 2.6
244+
*/
245+
E randomValue();
246+
239247
/**
240248
* Get elements between {@code start} and {@code end} from sorted set.
241249
*

src/test/java/org/springframework/data/redis/support/collections/AbstractRedisSetIntegrationTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,14 @@ void testScanWorksCorrectly() throws IOException {
302302
}
303303
cursor.close();
304304
}
305+
306+
@ParameterizedRedisTest // GH-2049
307+
void randMemberReturnsSomething() {
308+
309+
Object[] valuesArray = new Object[]{getT(), getT(), getT()};
310+
311+
collection.addAll((List<T>) Arrays.asList(valuesArray));
312+
313+
assertThat(set.randomValue()).isIn(valuesArray);
314+
}
305315
}

src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.util.Arrays;
2323
import java.util.Iterator;
24+
import java.util.List;
2425
import java.util.NoSuchElementException;
2526
import java.util.Set;
2627
import java.util.concurrent.TimeUnit;
@@ -856,4 +857,15 @@ void testZAddIfAbsentWorks() {
856857
assertThat(zSet.addIfAbsent(t1, 1)).isTrue();
857858
assertThat(zSet.addIfAbsent(t1, 1)).isFalse();
858859
}
860+
861+
@ParameterizedRedisTest // GH-2049
862+
@EnabledOnCommand("ZRANDMEMBER")
863+
void randMemberReturnsSomething() {
864+
865+
Object[] valuesArray = new Object[]{getT(), getT(), getT()};
866+
867+
collection.addAll((List<T>) Arrays.asList(valuesArray));
868+
869+
assertThat(zSet.randomValue()).isIn(valuesArray);
870+
}
859871
}

0 commit comments

Comments
 (0)