spring-boot-starter-data-redis 翻译官方文档 6.1 - 6.4

参考文档: https://docs.spring.io/spring-data/redis/docs/2.0.3.RELEASE/reference/html/

Redis中文教程: http://www.redis.net.cn/tutorial/3501.html

6. Reactive Redis support

本节介绍Redis支持以及如何入门。 你会发现与 imperative Redis support有某些重叠。

6.1. Redis Requirements

Spring Data Redis需要Redis 2.6或更高版本以及Java SE 8.0或更高版本。 在语言绑定(或连接器)方面,Spring Data Redis目前与Lettuce集成为唯一的reactive Java连接器。 Project Reactor被作为reactive组合库。

6.2. Connecting to Redis using a reactive driver


使用Redis和Spring的首要任务之一是通过IoC容器连接到商店。为此,需要Java连接器connector(或绑定binding)。无论选择哪个库,只有一组SpringDataRedis API需要使用,它们在所有连接器中的行为一致,org.springframework.data.redis.connection包和它的ReactiveRedisConnection和ReactiveRedisConnectionFactory接口,以便处理 并检索到Redis的活动连接。

6.2.1. Redis Operation Modes

Redis可以作为独立服务器运行,使用Redis SentinelRedis Cluster模式运行。 Lettuce
支持上面提到的所有连接类型。

6.2.2. ReactiveRedisConnection and ReactiveRedisConnectionFactory
ReactiveRedisConnection为Redis通信提供构建块,因为它处理与Redis后端的通信。它还自动将底层驱动程序异常转换为Spring的一致DAO异常层次结构,因此可以在不更改任何代码的情况下切换连接器,因为操作语义保持不变。

ReactiveRedisConnections的实例是通过ReactiveRedisConnectionFactory创建的。另外,工厂充当PersistenceExceptionTranslators,意味着一旦声明,它允许人们处理成明确的异常。例如,通过使用@Repository注释和AOP进行异常处理。 有关更多信息,请参阅Spring Framework文档中的专用章节   section

根据底层配置,工厂可以返回新连接或现有连接(如果使用池或共享本地连接)。

使用Reactive RedisConnectionFactory的最简单方法是通过IoC容器配置适当的连接器,并将其注入到使用的类中。

6.2.3. Configuring Lettuce connector
Spring Data Redis通过org.springframework.data.redis.connection.lettuce包支持Lettuce。

为Lettuce设置ReactiveRedisConnectionFactory可以按如下方式完成:
@Bean
public ReactiveRedisConnectionFactory connectionFactory() {
  return new LettuceConnectionFactory("localhost", 6379);
}

使用LettuceClientConfigurationBuilder的更复杂的配置(包括SSL和超时)如下所示:

@Bean
public ReactiveRedisConnectionFactory lettuceConnectionFactory() {

  LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
    .useSsl().and()
    .commandTimeout(Duration.ofSeconds(2))
    .shutdownTimeout(Duration.ZERO)
    .build();

  return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379), clientConfig);
}

有关更详细的客户端配置调整,请参阅LettuceClientConfiguration。

6.3. Working with Objects through ReactiveRedisTemplate

Most users are likely to use ReactiveRedisTemplate and its corresponding package org.springframework.data.redis.core - the template is in fact the central class of the Redis module due to its rich feature set. The template offers a high-level abstraction for Redis interactions. While ReactiveRedisConnection offers low level methods that accept and return binary values (ByteBuffer), the template takes care of serialization and connection management, freeing the user from dealing with such details.

Moreover, the template provides operation views (following the grouping from Redis command reference) that offer rich, generified interfaces for working against a certain type as described below:

大多数用户可能使用ReactiveRedisTemplate及其相应的包org.springframework.data.redis.core - 由于其丰富的功能集,template实际上是Redis模块的中心类。 该模板为Redis交互提供了高级抽象。 虽然ReactiveRedisConnection提供接受和返回二进制值(ByteBuffer)的低级方法,但该模板负责序列化和连接管理,使用户无需处理这些细节。

此外,该模板提供了操作视图(来自Redis命令参考的分组之后),该视图提供了丰富的,通用的接口,用于针对某种类型进行处理,如下所述:

Table 6. Operational views


InterfaceDescription

Key Type Operations

ReactiveGeoOperations

Redis geospatial operations like GEOADDGEORADIUS,…​)

ReactiveHashOperations

Redis hash operations

ReactiveHyperLogLogOperations

Redis HyperLogLog operations like (PFADDPFCOUNT,…​)

ReactiveListOperations

Redis list operations

ReactiveSetOperations

Redis set operations

ReactiveValueOperations

Redis string (or value) operations

ReactiveZSetOperations

Redis zset (or sorted set) operations

配置完成后,该模板是线程安全的,可以在多个实例中重复使用。

开箱即用,ReactiveRedisTemplate在其大部分操作中使用基于Java的序列化程序。 这意味着模板写入或读取的任何对象都将通过RedisElementWriter和RedisElementReader进行序列化/反序列化。序列化上下文在构建时传递给模板,Redis模块在org.springframework.data.redis.serializer包中提供了几个可用实现 - 请参阅序列化程序以获取更多信息。
@Configuration
class RedisConfiguration {

  @Bean
  ReactiveRedisTemplate<String, String> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
  }
}
public class Example {

  @Autowired
  private ReactiveRedisTemplate<String, String> template;

  public Mono<Long> addLink(String userId, URL url) {
    return template.opsForList().leftPush(userId, url.toExternalForm());
  }
}


6.4. Reactive Scripting

通过ReactiveRedisTemplate使用ReactiveScriptExecutor也可以实现Reactive基础构建执行 Redis scripts的功能。

public class Example {

  @Autowired
  private ReactiveRedisTemplate<String, String> template;

  public Flux<Long> theAnswerToLife() {

    DefaultRedisScript<Long> script = new DefaultRedisScript<>();
    script.setLocation(new ClassPathResource("META-INF/scripts/42.lua"));
    script.setResultType(Long.class);

    return reactiveTemplate.execute(script);
  }
}














  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值