Spring Data Redis 教程

ReactiveValueOperations 操作接口

Spring Data Redis 中,Value 类型的响应式接口为 ReactiveValueOperations,该接口定义的方法和 ValueOperations、BoundValueOperations 接口定义的方法非常类似。

我们可以通过 ReactiveRedisTemplate 的 opsForValue() 方法获取获取,代码如下:

ReactiveValueOperations<String,String> ops = reactiveRedisTemplate.opsForValue();

设置值

  • reactor.core.publisher.Mono<Boolean> set(K key, V value) 将给定的值设置到键

  • reactor.core.publisher.Mono<Boolean> set(K key, V value, Duration timeout) 将给定的值设置到键,且指定过期时间

  • reactor.core.publisher.Mono<Long> set(K key, V value, long offset) 用给定值覆盖从指定偏移量开始的键的部分。

  • reactor.core.publisher.Mono<Boolean> setBit(K key, long offset, boolean value) 将位设置在 key 中存储的值的偏移量处。

  • reactor.core.publisher.Mono<Boolean> setIfAbsent(K key, V value) 如果 key 不存在,则设置 key 以保存字符串值。

  • reactor.core.publisher.Mono<Boolean> setIfPresent(K key, V value) 如果 key 存在,则设置 key 以保存字符串值。

  • reactor.core.publisher.Mono<Boolean> multiSet(Map<? extends K,? extends V> map) 将参数Map中所有的键值对设置为多个值

  • reactor.core.publisher.Mono<Boolean> multiSetIfAbsent(Map<? extends K,? extends V> map) 仅当提供的键不存在时,才使用Map中提供的键值对将多个键设置为多个值。

示例:

@Test
public void set() {
    CountDownLatch countDownLatch = new CountDownLatch(3);
    ReactiveValueOperations<String,String> ops = reactiveRedisTemplate.opsForValue();
    // 异步设置值
    ops.set("value-key", "value1").subscribe(new Consumer<Boolean>() {
        @Override
        public void accept(Boolean aBoolean) {
            System.out.println(aBoolean ? "设置 value-key 成功" :
                    "设置 value-key 失败");
            countDownLatch.countDown();
        }
    });

    //  异步更新值
    ops.getAndSet("value-key", "value2").subscribe(new Consumer<String>() {
        @Override
        public void accept(String s) {
            System.out.println("修改 value-key 失败,旧值:" + s);
            countDownLatch.countDown();
        }
    });

    // 异步批量设置值
    Map<String,String> map = new HashMap<>();
    map.put("value-key1", "value1");
    map.put("value-key2", "value2");
    map.put("value-key3", "value3");
    ops.multiSet(map).subscribe(new Consumer<Boolean>() {
        @Override
        public void accept(Boolean aBoolean) {
            System.out.println(aBoolean ? "批量设置成功" : "批量设置失败");
            countDownLatch.countDown();
        }
    });

    try {
        // 等待设置值完成
        countDownLatch.await();

        // 同步获取数据
        Mono<List<String>> mono = ops.multiGet(Arrays.asList(
                "value-key1", "value-key3", "value-key2"));
        List<String> list = mono.block();
        for(String value : list) {
            System.out.println(value);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

运行示例,输出结果如下:

设置 value-key 成功
修改 value-key 失败,旧值:value1
批量设置成功
value1
value3
value2

追加值

  • reactor.core.publisher.Mono<Long> append(K key, String value) 将给定的值追加到键的值的尾部

示例:

@Test
public void append() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    ReactiveValueOperations<String,String> ops = reactiveRedisTemplate.opsForValue();
    // 同步添加一个值
    Mono<Boolean> mono = ops.set("value-key", "hello world");
    mono.block();

    // 异步追加值到键
    ops.append("value-key", " new value").subscribe(new Consumer<Long>() {
        @Override
        public void accept(Long aLong) {
            ops.get("value-key").subscribe(new Consumer<String>() {
                @Override
                public void accept(String s) {
                    System.out.println("新值:" + s);
                    countDownLatch.countDown();
                }
            });
        }
    });

    try {
        countDownLatch.await();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

运行示例,输出结果如下:

新值:hello world new value

获取值

  • reactor.core.publisher.Mono<String> get(K key, long start, long end) 从指定键中获取一个子字符串,子字符串从 bengin 到 end 范围内。

  • reactor.core.publisher.Mono<V> get(Object key) 获取指定键的值

  • reactor.core.publisher.Mono<V> getAndSet(K key, V value) 将值设置到键,且返回旧的值。

  • reactor.core.publisher.Mono<Boolean> getBit(K key, long offset) 获取键中指定下标的 Bit 位的值

  • reactor.core.publisher.Mono<List<V>> multiGet(Collection<K> keys) 批量获取键的值

示例:

@Test
public void get() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    ReactiveValueOperations<String,String> ops = reactiveRedisTemplate.opsForValue();
    // 同步添加一个值
    Mono<Boolean> mono = ops.set("value-key", "hello world");
    mono.block();

    // 异步获取值
    ops.get("value-key").subscribe(new Consumer<String>() {
        @Override
        public void accept(String s) {
            System.out.println("value-key=" + s);
            // 异步获取且设置新值
            ops.getAndSet("value-key", "new value").subscribe(new Consumer<String>() {
                @Override
                public void accept(String s) {
                    System.out.println("value-key=" + s);
                    countDownLatch.countDown();
                }
            });
        }
    });

    try {
        countDownLatch.await();

        // 同步获取最新设置的值
        String value = ops.get("value-key").block();
        System.out.println("value-key=" + value);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

运行示例,输出结果如下:

value-key=hello world
value-key=hello world
value-key=new value

删除键

  • reactor.core.publisher.Mono<Boolean> delete(K key) 删除给定的键

示例:

@Test
public void delete() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    ReactiveValueOperations<String,String> ops = reactiveRedisTemplate.opsForValue();
    // 同步添加数据
    Mono<Boolean> mono = ops.set("value-key", "hello world");
    Boolean flag = mono.block();
    System.out.println(flag ? "添加成功" : "添加失败");

    // 异步删除键
    Mono<Boolean> mono2 = ops.delete("value-key");
    mono2.subscribe(new Consumer<Boolean>() {
        @Override
        public void accept(Boolean aBoolean) {
            System.out.println(aBoolean ? "删除成功" : "删除失败");
            countDownLatch.countDown();
        }
    });

    try {
        countDownLatch.await();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

运行示例,输出结果如下:

添加成功
删除成功

获取大小

  • reactor.core.publisher.Mono<Long> size(K key) 获取指定健存储值的长度

示例:

@Test
public void size() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    ReactiveValueOperations<String,String> ops = reactiveRedisTemplate.opsForValue();
    // 同步添加数据
    Mono<Boolean> mono = ops.set("value-key", "hello world");
    mono.block();

    // 获取 value-key 存储值的长度
    ops.size("value-key").subscribe(new Consumer<Long>() {
        @Override
        public void accept(Long aLong) {
            System.out.println("size=" + aLong);
            countDownLatch.countDown();
        }
    });

    try {
        countDownLatch.await();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

运行示例,输出结果如下:

size=11


说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号