Redission核心代码流程使用Promise模式。 这归功于netty优秀的架构,是Redission提供异步和同步编程的核心。
Config config = new Config();
config.useSingleServer().setAddress("redis://192.168.2.41:6379")
.setPassword("rabbit123@");
//创建客户端
RedissonClient redisson = Redisson.create(config);
//创建Bucket对象
RBucket<String> rBucket = redisson.getBucket("dddd");
rBucket.set("ffff");
String ss = rBucket.get();
这篇博客主要详细介绍Redission提供的RBucket 类。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.redisson.api;
import java.util.concurrent.TimeUnit;
public interface RBucket<V> extends RExpirable, RBucketAsync<V> {
long size();
V get();
V getAndDelete();
boolean trySet(V var1);
boolean trySet(V var1, long var2, TimeUnit var4);
boolean setIfExists(V var1);
boolean setIfExists(V var1, long var2, TimeUnit var4);
boolean compareAndSet(V var1, V var2);
V getAndSet(V var1);
V getAndSet(V var1, long var2, TimeUnit var4);
void set(V var1);
void set(V var1, long var2, TimeUnit var4);
int addListener(ObjectListener var1);
}
RBucket提供set()和sget()方法用于保存和获取对象。RedissonBucket 是对RBucket对象的实现。RedissonBucket实现set()和get()同步方法和异步方法。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.redisson;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import org.redisson.api.ObjectListener;
import org.redisson.api.RBucket;
import org.redisson.api.RFuture;
import org.redisson.api.RPatternTopic;
import org.redisson.api.listener.SetObjectListener;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.CountableListener;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;
public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
public RedissonBucket(CommandAsyncExecutor connectionManager, String name) {
super(connectionManager, name);
}
public RedissonBucket(Codec codec, CommandAsyncExecutor connectionManager, String name) {
super(codec, connectionManager, name);
}
public boolean compareAndSet(V expect, V update) {
return (Boolean)this.get(this.compareAndSetAsync(expect, update));
}
public RFuture<Boolean> compareAndSetAsync(V expect, V update) {
if (expect == null && update == null) {
return this.trySetAsync((Object)null);
} else if (expect == null) {
return this.trySetAsync(update);
} else {
return update == null ? this.commandExecutor.evalWriteAsync(this.getName(), this.codec, RedisCommands.EVAL_BOOLEAN, "if redis.call('get', KEYS[1]) == ARGV[1] then redis.call('del', KEYS[1]); return 1 else return 0 end", Collections.singletonList(this.getName()), new Object[]{this.encode(expect)}) : this.commandExecutor.evalWriteAsync(this.getName(), this.codec, RedisCommands.EVAL_BOOLEAN, "if redis.call('get', KEYS[1]) == ARGV[1] then redis.call('set', KEYS[1], ARGV[2]); return 1 else return 0 end", Collections.singletonList(this.getName()), new Object[]{this.encode(expect), this.encode(update)});
}
}
public V getAndSet(V newValue) {
return this.get(this.getAndSetAsync(newValue));
}
public RFuture<V> getAndSetAsync(V newValue) {
return newValue == null ? this.commandExecutor.evalWriteAsync(this.getName(), this.codec, RedisCommands.EVAL_OBJECT, "local v = redis.call('get', KEYS[1]); redis.call('del', KEYS[1]); return v", Collections.singletonList(this.getName()), new Object[0]) : this.commandExecutor.writeAsync(this.getName(), this.codec, RedisCommands.GETSET, new Object[]{this.getName(), this.encode(newValue)});
}
public V get() {
return this.get(this.getAsync());
}
public RFuture<V> getAsync() {
return this.commandExecutor.readAsync(this.getName(), this.codec, RedisCommands.GET, new Object[]{this.getName()});
}
public V getAndDelete() {
return this.get(this.getAndDeleteAsync());
}
public RFuture<V> getAndDeleteAsync() {
return this.commandExecutor.evalWriteAsync(this.getName(), this.codec, RedisCommands.EVAL_OBJECT, "local currValue = redis.call('get', KEYS[1]); redis.call('del', KEYS[1]); return currValue; ", Collections.singletonList(this.getName()), new Object[0]);
}
public long size() {
return (Long)this.get(this.sizeAsync());
}
public RFuture<Long> sizeAsync() {
return this.commandExecutor.readAsync(this.getName(), this.codec, RedisCommands.STRLEN, new Object[]{this.getName()});
}
public void set(V value) {
this.get(this.setAsync(value));
}
public RFuture<Void> setAsync(V value) {
return value == null ? this.commandExecutor.writeAsync(this.getName(), RedisCommands.DEL_VOID, new Object[]{this.getName()}) : this.commandExecutor.writeAsync(this.getName(), this.codec, RedisCommands.SET, new Object[]{this.getName(), this.encode(value)});
}