RBucket.trySet and RBucket.trySetAsync with TTL support added.

pull/365/head
Nikita 9 years ago
parent d7842136f3
commit cde8082493

@ -15,6 +15,7 @@
*/
package org.redisson;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.redisson.client.codec.Codec;
@ -89,6 +90,21 @@ public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SETNX, getName(), value);
}
@Override
public Future<Boolean> trySetAsync(V value, long timeToLive, TimeUnit timeUnit) {
try {
byte[] state = codec.getValueEncoder().encode(value);
return commandExecutor.writeAsync(getName(), codec, RedisCommands.SETPXNX, getName(), state, "PX", timeUnit.toMillis(timeToLive), "NX");
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public boolean trySet(V value, long timeToLive, TimeUnit timeUnit) {
return get(trySetAsync(value, timeToLive, timeUnit));
}
@Override
public boolean trySet(V value) {
return get(trySetAsync(value));

@ -196,6 +196,7 @@ public interface RedisCommands {
RedisCommand<Object> GET = new RedisCommand<Object>("GET");
RedisCommand<Void> SET = new RedisCommand<Void>("SET", new VoidReplayConvertor(), 2);
RedisCommand<Boolean> SETPXNX = new RedisCommand<Boolean>("SET", new BooleanNotNullReplayConvertor());
RedisCommand<Boolean> SETNX = new RedisCommand<Boolean>("SETNX", new BooleanReplayConvertor(), 2);
RedisCommand<Void> SETEX = new RedisCommand<Void>("SETEX", new VoidReplayConvertor(), 3);
RedisStrictCommand<Boolean> EXISTS = new RedisStrictCommand<Boolean>("EXISTS", new BooleanReplayConvertor());

@ -30,6 +30,8 @@ public interface RBucket<V> extends RExpirable, RBucketAsync<V> {
boolean trySet(V value);
boolean trySet(V value, long timeToLive, TimeUnit timeUnit);
void set(V value);
void set(V value, long timeToLive, TimeUnit timeUnit);

@ -32,6 +32,8 @@ public interface RBucketAsync<V> extends RExpirableAsync {
Future<Boolean> trySetAsync(V value);
Future<Boolean> trySetAsync(V value, long timeToLive, TimeUnit timeUnit);
Future<Void> setAsync(V value);
Future<Void> setAsync(V value, long timeToLive, TimeUnit timeUnit);

@ -22,6 +22,18 @@ public class RedissonBucketTest extends BaseTest {
assertThat(r1.get()).isEqualTo("3");
}
@Test
public void testTrySetTTL() throws InterruptedException {
RBucket<String> r1 = redisson.getBucket("12");
assertThat(r1.trySet("3", 500, TimeUnit.MILLISECONDS)).isTrue();
assertThat(r1.trySet("4", 500, TimeUnit.MILLISECONDS)).isFalse();
assertThat(r1.get()).isEqualTo("3");
Thread.sleep(500);
assertThat(r1.get()).isNull();
}
@Test
public void testSaveBuckets() {
Map<String, Integer> buckets = new HashMap<String, Integer>();

Loading…
Cancel
Save