redission 源代码剖析3 future模式

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 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 extends RExpirable, RBucketAsync {
    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 extends RedissonExpirable implements RBucket {
    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 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 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 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 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 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 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)});
    }

    public void set(V value, long timeToLive, TimeUnit timeUnit) {
        this.get(this.setAsync(value, timeToLive, timeUnit));
    }

    public RFuture setAsync(V value, long timeToLive, TimeUnit timeUnit) {
        return value == null ? this.commandExecutor.writeAsync(this.getName(), RedisCommands.DEL_VOID, new Object[]{this.getName()}) : this.commandExecutor.writeAsync(this.getName(), this.codec, RedisCommands.PSETEX, new Object[]{this.getName(), timeUnit.toMillis(timeToLive), this.encode(value)});
    }

    public RFuture trySetAsync(V value) {
        return value == null ? this.commandExecutor.readAsync(this.getName(), this.codec, RedisCommands.NOT_EXISTS, new Object[]{this.getName()}) : this.commandExecutor.writeAsync(this.getName(), this.codec, RedisCommands.SETNX, new Object[]{this.getName(), this.encode(value)});
    }

    public RFuture trySetAsync(V value, long timeToLive, TimeUnit timeUnit) {
        if (value == null) {
            throw new IllegalArgumentException("Value can't be null");
        } else {
            return this.commandExecutor.writeAsync(this.getName(), this.codec, RedisCommands.SET_BOOLEAN, new Object[]{this.getName(), this.encode(value), "PX", timeUnit.toMillis(timeToLive), "NX"});
        }
    }

    public boolean trySet(V value, long timeToLive, TimeUnit timeUnit) {
        return (Boolean)this.get(this.trySetAsync(value, timeToLive, timeUnit));
    }

    public boolean trySet(V value) {
        return (Boolean)this.get(this.trySetAsync(value));
    }

    public boolean setIfExists(V value) {
        return (Boolean)this.get(this.setIfExistsAsync(value));
    }

    public RFuture setIfExistsAsync(V value) {
        return value == null ? this.commandExecutor.evalWriteAsync(this.getName(), this.codec, RedisCommands.EVAL_BOOLEAN, "local currValue = redis.call('get', KEYS[1]); if currValue ~= false then redis.call('del', KEYS[1]); return 1;end;return 0; ", Collections.singletonList(this.getName()), new Object[0]) : this.commandExecutor.writeAsync(this.getName(), this.codec, RedisCommands.SET_BOOLEAN, new Object[]{this.getName(), this.encode(value), "XX"});
    }

    public boolean setIfExists(V value, long timeToLive, TimeUnit timeUnit) {
        return (Boolean)this.get(this.setIfExistsAsync(value, timeToLive, timeUnit));
    }

    public RFuture setIfExistsAsync(V value, long timeToLive, TimeUnit timeUnit) {
        if (value == null) {
            throw new IllegalArgumentException("Value can't be null");
        } else {
            return this.commandExecutor.writeAsync(this.getName(), this.codec, RedisCommands.SET_BOOLEAN, new Object[]{this.getName(), this.encode(value), "PX", timeUnit.toMillis(timeToLive), "XX"});
        }
    }

    public RFuture getAndSetAsync(V value, long timeToLive, TimeUnit timeUnit) {
        return this.commandExecutor.evalWriteAsync(this.getName(), this.codec, RedisCommands.EVAL_OBJECT, "local currValue = redis.call('get', KEYS[1]); redis.call('psetex', KEYS[1], ARGV[2], ARGV[1]); return currValue; ", Collections.singletonList(this.getName()), new Object[]{this.encode(value), timeUnit.toMillis(timeToLive)});
    }

    public V getAndSet(V value, long timeToLive, TimeUnit timeUnit) {
        return this.get(this.getAndSetAsync(value, timeToLive, timeUnit));
    }

    public int addListener(ObjectListener listener) {
        return listener instanceof SetObjectListener ? this.addListener("__keyevent@*:set", (SetObjectListener)listener, SetObjectListener::onSet) : super.addListener(listener);
    }

    public RFuture addListenerAsync(ObjectListener listener) {
        return listener instanceof SetObjectListener ? this.addListenerAsync("__keyevent@*:set", (SetObjectListener)listener, SetObjectListener::onSet) : super.addListenerAsync(listener);
    }

    public void removeListener(int listenerId) {
        RPatternTopic expiredTopic = new RedissonPatternTopic(StringCodec.INSTANCE, this.commandExecutor, "__keyevent@*:set");
        expiredTopic.removeListener(listenerId);
        super.removeListener(listenerId);
    }

    public RFuture removeListenerAsync(int listenerId) {
        RPromise result = new RedissonPromise();
        CountableListener listener = new CountableListener(result, (Object)null, 3);
        RPatternTopic setTopic = new RedissonPatternTopic(StringCodec.INSTANCE, this.commandExecutor, "__keyevent@*:set");
        setTopic.removeListenerAsync(listenerId).onComplete(listener);
        this.removeListenersAsync(listenerId, listener);
        return result;
    }
}

 RedissonBucket 对象又继承了基类RedissionObject对象。 在RedissionObject 包含name 作为键,codec 作为编码解码器,commandExecutor 用来执行所以的操作。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.redisson;

import io.netty.buffer.ByteBuf;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.redisson.api.DeletedObjectListener;
import org.redisson.api.ExpiredObjectListener;
import org.redisson.api.ObjectListener;
import org.redisson.api.RFuture;
import org.redisson.api.RObject;
import org.redisson.api.RPatternTopic;
import org.redisson.api.listener.PatternMessageListener;
import org.redisson.client.codec.ByteArrayCodec;
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.Hash;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;

public abstract class RedissonObject implements RObject {
    protected final CommandAsyncExecutor commandExecutor;
    protected String name;
    protected final Codec codec;

    public RedissonObject(Codec codec, CommandAsyncExecutor commandExecutor, String name) {
        this.codec = codec;
        this.name = name;
        this.commandExecutor = commandExecutor;
    }

    public RedissonObject(CommandAsyncExecutor commandExecutor, String name) {
        this(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name);
    }

    public static String prefixName(String prefix, String name) {
        return name.contains("{") ? prefix + ":" + name : prefix + ":{" + name + "}";
    }

    public static String suffixName(String name, String suffix) {
        return name.contains("{") ? name + ":" + suffix : "{" + name + "}:" + suffix;
    }

    protected final  Stream toStream(Iterator iterator) {
        Spliterator spliterator = Spliterators.spliteratorUnknownSize(iterator, 256);
        return StreamSupport.stream(spliterator, false);
    }

    protected final  V get(RFuture future) {
        return this.commandExecutor.get(future);
    }

    protected final long toSeconds(long timeout, TimeUnit unit) {
        long seconds = unit.toSeconds(timeout);
        if (timeout != 0L && seconds == 0L) {
            seconds = 1L;
        }

        return seconds;
    }

    public String getName() {
        return this.name;
    }

    protected String getName(Object o) {
        return this.getName();
    }

    public void rename(String newName) {
        this.get(this.renameAsync(newName));
    }

    public RFuture sizeInMemoryAsync() {
        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.MEMORY_USAGE, new Object[]{this.getName()});
    }

    public final RFuture sizeInMemoryAsync(List keys) {
        return this.commandExecutor.evalWriteAsync((String)keys.get(0), StringCodec.INSTANCE, RedisCommands.EVAL_LONG, "local total = 0;for j = 1, #KEYS, 1 do local size = redis.call('memory', 'usage', KEYS[j]); if size ~= false then total = total + size;end; end; return total; ", keys, new Object[0]);
    }

    public long sizeInMemory() {
        return (Long)this.get(this.sizeInMemoryAsync());
    }

    public RFuture renameAsync(String newName) {
        RFuture f = this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.RENAME, new Object[]{this.getName(), newName});
        f.onComplete((r, e) -> {
            if (e == null) {
                this.name = newName;
            }

        });
        return f;
    }

    public void migrate(String host, int port, int database, long timeout) {
        this.get(this.migrateAsync(host, port, database, timeout));
    }

    public RFuture migrateAsync(String host, int port, int database, long timeout) {
        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.MIGRATE, new Object[]{host, port, this.getName(), database, timeout});
    }

    public void copy(String host, int port, int database, long timeout) {
        this.get(this.copyAsync(host, port, database, timeout));
    }

    public RFuture copyAsync(String host, int port, int database, long timeout) {
        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.MIGRATE, new Object[]{host, port, this.getName(), database, timeout, "COPY"});
    }

    public boolean move(int database) {
        return (Boolean)this.get(this.moveAsync(database));
    }

    public RFuture moveAsync(int database) {
        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.MOVE, new Object[]{this.getName(), database});
    }

    public boolean renamenx(String newName) {
        return (Boolean)this.get(this.renamenxAsync(newName));
    }

    public RFuture renamenxAsync(String newName) {
        RFuture f = this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.RENAMENX, new Object[]{this.getName(), newName});
        f.onComplete((value, e) -> {
            if (e == null && value) {
                this.name = newName;
            }

        });
        return f;
    }

    public boolean delete() {
        return (Boolean)this.get(this.deleteAsync());
    }

    public RFuture deleteAsync() {
        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.DEL_BOOL, new Object[]{this.getName()});
    }

    protected RFuture deleteAsync(String... keys) {
        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.DEL_OBJECTS, keys);
    }

    public boolean unlink() {
        return (Boolean)this.get(this.unlinkAsync());
    }

    public RFuture unlinkAsync() {
        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.UNLINK_BOOL, new Object[]{this.getName()});
    }

    public boolean touch() {
        return (Boolean)this.get(this.touchAsync());
    }

    public RFuture touchAsync() {
        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.TOUCH, new Object[]{this.getName()});
    }

    public boolean isExists() {
        return (Boolean)this.get(this.isExistsAsync());
    }

    public RFuture isExistsAsync() {
        return this.commandExecutor.readAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.EXISTS, new Object[]{this.getName()});
    }

    public Codec getCodec() {
        return this.codec;
    }

    protected List encode(Object... values) {
        List result = new ArrayList(values.length);
        Object[] var3 = values;
        int var4 = values.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            Object object = var3[var5];
            result.add(this.encode(object));
        }

        return result;
    }

    protected List encode(Collection values) {
        List result = new ArrayList(values.size());
        Iterator var3 = values.iterator();

        while(var3.hasNext()) {
            Object object = var3.next();
            result.add(this.encode(object));
        }

        return result;
    }

    public void encode(Collection params, Collection values) {
        Iterator var3 = values.iterator();

        while(var3.hasNext()) {
            Object object = var3.next();
            params.add(this.encode(object));
        }

    }

    public String getLockByMapKey(Object key, String suffix) {
        ByteBuf keyState = this.encodeMapKey(key);

        String var4;
        try {
            var4 = suffixName(this.getName(key), Hash.hash128toBase64(keyState) + ":" + suffix);
        } finally {
            keyState.release();
        }

        return var4;
    }

    public String getLockByValue(Object key, String suffix) {
        ByteBuf keyState = this.encode(key);

        String var4;
        try {
            var4 = suffixName(this.getName(key), Hash.hash128toBase64(keyState) + ":" + suffix);
        } finally {
            keyState.release();
        }

        return var4;
    }

    protected void encodeMapKeys(Collection params, Collection values) {
        Iterator var3 = values.iterator();

        while(var3.hasNext()) {
            Object object = var3.next();
            params.add(this.encodeMapKey(object));
        }

    }

    protected void encodeMapValues(Collection params, Collection values) {
        Iterator var3 = values.iterator();

        while(var3.hasNext()) {
            Object object = var3.next();
            params.add(this.encodeMapValue(object));
        }

    }

    public ByteBuf encode(Object value) {
        if (this.commandExecutor.isRedissonReferenceSupportEnabled()) {
            RedissonReference reference = this.commandExecutor.getObjectBuilder().toReference(value);
            if (reference != null) {
                value = reference;
            }
        }

        try {
            return this.codec.getValueEncoder().encode(value);
        } catch (IOException var3) {
            throw new IllegalArgumentException(var3);
        }
    }

    public ByteBuf encodeMapKey(Object value) {
        if (this.commandExecutor.isRedissonReferenceSupportEnabled()) {
            RedissonReference reference = this.commandExecutor.getObjectBuilder().toReference(value);
            if (reference != null) {
                value = reference;
            }
        }

        try {
            return this.codec.getMapKeyEncoder().encode(value);
        } catch (IOException var3) {
            throw new IllegalArgumentException(var3);
        }
    }

    public ByteBuf encodeMapValue(Object value) {
        if (this.commandExecutor.isRedissonReferenceSupportEnabled()) {
            RedissonReference reference = this.commandExecutor.getObjectBuilder().toReference(value);
            if (reference != null) {
                value = reference;
            }
        }

        try {
            return this.codec.getMapValueEncoder().encode(value);
        } catch (IOException var3) {
            throw new IllegalArgumentException(var3);
        }
    }

    public byte[] dump() {
        return (byte[])this.get(this.dumpAsync());
    }

    public RFuture dumpAsync() {
        return this.commandExecutor.readAsync(this.getName(), ByteArrayCodec.INSTANCE, RedisCommands.DUMP, new Object[]{this.getName()});
    }

    public void restore(byte[] state) {
        this.get(this.restoreAsync(state));
    }

    public RFuture restoreAsync(byte[] state) {
        return this.restoreAsync(state, 0L, (TimeUnit)null);
    }

    public void restore(byte[] state, long timeToLive, TimeUnit timeUnit) {
        this.get(this.restoreAsync(state, timeToLive, timeUnit));
    }

    public RFuture restoreAsync(byte[] state, long timeToLive, TimeUnit timeUnit) {
        long ttl = 0L;
        if (timeToLive > 0L) {
            ttl = timeUnit.toMillis(timeToLive);
        }

        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.RESTORE, new Object[]{this.getName(), ttl, state});
    }

    public void restoreAndReplace(byte[] state, long timeToLive, TimeUnit timeUnit) {
        this.get(this.restoreAndReplaceAsync(state, timeToLive, timeUnit));
    }

    public RFuture restoreAndReplaceAsync(byte[] state, long timeToLive, TimeUnit timeUnit) {
        long ttl = 0L;
        if (timeToLive > 0L) {
            ttl = timeUnit.toMillis(timeToLive);
        }

        return this.commandExecutor.writeAsync(this.getName(), StringCodec.INSTANCE, RedisCommands.RESTORE, new Object[]{this.getName(), ttl, state, "REPLACE"});
    }

    public void restoreAndReplace(byte[] state) {
        this.get(this.restoreAndReplaceAsync(state));
    }

    public RFuture restoreAndReplaceAsync(byte[] state) {
        return this.restoreAndReplaceAsync(state, 0L, (TimeUnit)null);
    }

    protected final  int addListener(String name, T listener, BiConsumer consumer) {
        RPatternTopic topic = new RedissonPatternTopic(StringCodec.INSTANCE, this.commandExecutor, name);
        return topic.addListener(String.class, (pattern, channel, msg) -> {
            if (msg.equals(this.getName())) {
                consumer.accept(listener, msg);
            }

        });
    }

    protected final  RFuture addListenerAsync(String name, T listener, BiConsumer consumer) {
        RPatternTopic topic = new RedissonPatternTopic(StringCodec.INSTANCE, this.commandExecutor, name);
        return topic.addListenerAsync(String.class, (pattern, channel, msg) -> {
            if (msg.equals(this.getName())) {
                consumer.accept(listener, msg);
            }

        });
    }

    public int addListener(ObjectListener listener) {
        if (listener instanceof ExpiredObjectListener) {
            return this.addListener("__keyevent@*:expired", (ExpiredObjectListener)listener, ExpiredObjectListener::onExpired);
        } else if (listener instanceof DeletedObjectListener) {
            return this.addListener("__keyevent@*:del", (DeletedObjectListener)listener, DeletedObjectListener::onDeleted);
        } else {
            throw new IllegalArgumentException();
        }
    }

    public RFuture addListenerAsync(ObjectListener listener) {
        if (listener instanceof ExpiredObjectListener) {
            return this.addListenerAsync("__keyevent@*:expired", (ExpiredObjectListener)listener, ExpiredObjectListener::onExpired);
        } else if (listener instanceof DeletedObjectListener) {
            return this.addListenerAsync("__keyevent@*:del", (DeletedObjectListener)listener, DeletedObjectListener::onDeleted);
        } else {
            throw new IllegalArgumentException();
        }
    }

    public void removeListener(int listenerId) {
        RPatternTopic expiredTopic = new RedissonPatternTopic(StringCodec.INSTANCE, this.commandExecutor, "__keyevent@*:expired");
        expiredTopic.removeListener(listenerId);
        RPatternTopic deletedTopic = new RedissonPatternTopic(StringCodec.INSTANCE, this.commandExecutor, "__keyevent@*:del");
        deletedTopic.removeListener(listenerId);
    }

    public RFuture removeListenerAsync(int listenerId) {
        RPromise result = new RedissonPromise();
        CountableListener listener = new CountableListener(result, (Object)null, 2);
        this.removeListenersAsync(listenerId, listener);
        return result;
    }

    protected final void removeListenersAsync(int listenerId, CountableListener listener) {
        RPatternTopic expiredTopic = new RedissonPatternTopic(StringCodec.INSTANCE, this.commandExecutor, "__keyevent@*:expired");
        expiredTopic.removeListenerAsync(listenerId).onComplete(listener);
        RPatternTopic deletedTopic = new RedissonPatternTopic(StringCodec.INSTANCE, this.commandExecutor, "__keyevent@*:del");
        deletedTopic.removeListenerAsync(listenerId).onComplete(listener);
    }
}
 
  

当RedissionBucket执行set()方法时,这将是Redission整个流程的核心。

1 -RedissonBucket 执行set()方法,并通过get()方法获取返回值

  public void set(V value) {
        this.get(this.setAsync(value));
    }

 2 -真正执行set操作,为CommandAsyncExecutor 来完成。主要封装redis的键值,命令以及所需要的参数


    public RFuture 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)});
    }

3-CommandAsyncService 首先通过netty框架创建一个promise接口,并且返回。  

  public  RFuture writeAsync(String key, Codec codec, RedisCommand command, Object... params) {
        RPromise mainPromise = this.createPromise();
        NodeSource source = this.getNodeSource(key);
        this.async(false, source, codec, command, params, mainPromise, false);
        return mainPromise;
    }

 在此,RBucket 执行一个set操作,由netty创建一个promise对象,并且RBucket通过get()同步方法获取promise完成时的结果。

 

你可能感兴趣的:(Redis)