redission 源代码剖析3 future模式

本文深入探讨Redisson的核心代码,重点解析RBucket类及其set()和get()方法如何利用Promise模式实现异步和同步操作。RedissonBucket继承RedissionObject,其中包含了name、codec和commandExecutor等关键属性。在set()方法执行时,通过CommandAsyncExecutor封装redis命令并借助netty的Promise接口进行异步处理,最终通过get()方法获取结果。
摘要由CSDN通过智能技术生成

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)});
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非ban必选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值