Redisson.saveBuckets added. #336

pull/365/head
Nikita 9 years ago
parent 9dfa71ce78
commit 6b6f67cbdb

@ -15,11 +15,13 @@
*/
package org.redisson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.redisson.api.RedissonReactiveClient;
@ -195,6 +197,25 @@ public class Redisson implements RedissonClient {
return result;
}
public void saveBuckets(Map<String, ?> buckets) {
if (config.isClusterConfig()) {
throw new IllegalStateException("This method can't be used in cluster mode!");
}
List<Object> params = new ArrayList<Object>(buckets.size());
for (Entry<String, ?> entry : buckets.entrySet()) {
params.add(entry.getKey());
try {
params.add(config.getCodec().getValueEncoder().encode(entry.getValue()));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
Future<Void> future = commandExecutor.writeAsync(null, RedisCommands.MSET, params.toArray());
commandExecutor.get(future);
}
@Override
public <V> List<RBucket<V>> getBuckets(String pattern) {
return findBuckets(pattern);

@ -143,7 +143,7 @@ public interface RedissonClient {
<V> List<RBucket<V>> findBuckets(String pattern);
/**
* <p>Returns RBucket value mapped by key. Result Map is not contains
* <p>Returns Redis object mapped by key. Result Map is not contains
* key-value entry for null values.
*
* <p>Uses <code>MGET</code> Redis command.
@ -154,7 +154,7 @@ public interface RedissonClient {
<V> Map<String, V> loadBucketValues(Collection<String> keys);
/**
* <p>Returns RBucket value mapped by key. Result Map is not contains
* <p>Returns Redis object mapped by key. Result Map is not contains
* key-value entry for null values.
*
* <p>Uses <code>MGET</code> Redis command.
@ -164,6 +164,13 @@ public interface RedissonClient {
*/
<V> Map<String, V> loadBucketValues(String ... keys);
/**
* Saves Redis object mapped by key.
*
* @param buckets
*/
void saveBuckets(Map<String, ?> buckets);
/**
* Use {@link #findBuckets(String)}
*/

@ -171,6 +171,7 @@ public interface RedisCommands {
RedisStrictCommand<List<String>> KEYS = new RedisStrictCommand<List<String>>("KEYS", new StringListReplayDecoder());
RedisCommand<List<Object>> MGET = new RedisCommand<List<Object>>("MGET", new ObjectListReplayDecoder<Object>());
RedisStrictCommand<Void> MSET = new RedisStrictCommand<Void>("MSET", new VoidReplayConvertor());
RedisCommand<Boolean> HSET = new RedisCommand<Boolean>("HSET", new BooleanReplayConvertor(), 2, ValueType.MAP);
RedisStrictCommand<String> HINCRBYFLOAT = new RedisStrictCommand<String>("HINCRBYFLOAT");

@ -10,9 +10,24 @@ import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
import org.redisson.core.RBucket;
import static org.assertj.core.api.Assertions.*;
public class RedissonBucketTest extends BaseTest {
@Test
public void testSaveBuckets() {
Map<String, Integer> buckets = new HashMap<String, Integer>();
buckets.put("12", 1);
buckets.put("41", 2);
redisson.saveBuckets(buckets);
RBucket<Object> r1 = redisson.getBucket("12");
assertThat(r1.get()).isEqualTo(1);
RBucket<Object> r2 = redisson.getBucket("41");
assertThat(r2.get()).isEqualTo(2);
}
@Test
public void testLoadBucketValues() {
RBucket<String> bucket1 = redisson.getBucket("test1");

Loading…
Cancel
Save