Add Redis 2.6 srandmember with count to SetOperations

DATAREDIS-116
This commit is contained in:
Jennifer Hickey
2013-06-24 16:39:45 -07:00
parent c1337d5067
commit 7173be2c28
5 changed files with 88 additions and 1 deletions

View File

@@ -62,6 +62,8 @@ public interface BoundSetOperations<K, V> extends BoundKeyOperations<K> {
V randomMember();
Set<V> randomMembers(long count);
Boolean remove(Object o);
V pop();

View File

@@ -113,12 +113,17 @@ class DefaultBoundSetOperations<K, V> extends DefaultBoundKeyOperations<K> imple
return ops.randomMember(getKey());
}
public Set<V> randomMembers(long count) {
return ops.randomMembers(getKey(), count);
}
public Boolean remove(Object o) {
return ops.remove(getKey(), o);
}
public V pop() {
return ops.pop(getKey());
}

View File

@@ -162,6 +162,18 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
}, true);
}
public Set<V> randomMembers(K key, final long count) {
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.sRandMember(rawKey, count);
}
}, true);
return deserializeValues(rawValues);
}
public Boolean remove(K key, Object o) {
final byte[] rawKey = rawKey(key);

View File

@@ -60,6 +60,8 @@ public interface SetOperations<K, V> {
V randomMember(K key);
Set<V> randomMembers(K key, long count);
Boolean remove(K key, Object o);
V pop(K key);

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.core;
import java.util.Arrays;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration test of {@link DefaultSetOperations}
*
* @author Jennifer Hickey
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("RedisTemplateTests-context.xml")
public class DefaultSetOperationsTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
private SetOperations<String, String> setOps;
@Before
public void setUp() {
setOps = redisTemplate.opsForSet();
}
@After
public void tearDown() {
redisTemplate.getConnectionFactory().getConnection().flushDb();
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testRandomMembers() {
setOps.add("test", "foo");
setOps.add("test", "bar");
setOps.add("test", "baz");
Set<String> members = setOps.randomMembers("test", 2);
assertEquals(2, members.size());
assertTrue(Arrays.asList(new String[] {"foo", "bar", "baz"}).containsAll(members));
}
}