大多数用户可能会使用及其对应的包,或其反应式变体。 事实上,该模板是 Redis 模块的核心类,因为它具有丰富的功能集。 该模板为 Redis 交互提供了高级抽象。 虽然提供了接受和返回二进制值(数组)的低级方法,但该模板负责序列化和连接管理,使用户无需处理此类详细信息。RedisTemplateorg.springframework.data.redis.coreReactiveRedisTemplate[Reactive]RedisConnectionbyteSpring中文文档

该类实现接口,其反应式变体实现 。RedisTemplateRedisOperationsReactiveRedisTemplateReactiveRedisOperationsSpring中文文档

引用实例上的操作的首选方法是通过接口。[Reactive]RedisTemplate[Reactive]RedisOperations

此外,该模板还提供操作视图(遵循 Redis 命令参考中的分组),这些视图提供丰富的生成接口,用于针对特定类型或特定键(通过接口)进行操作,如下表所述:KeyBoundSpring中文文档

操作视图
接口 描述

键类型操作Spring中文文档

GeoOperationsSpring中文文档

Redis 地理空间操作,例如 、 ,...GEOADDGEORADIUSSpring中文文档

HashOperationsSpring中文文档

Redis 哈希操作Spring中文文档

HyperLogLogOperationsSpring中文文档

Redis HyperLogLog 操作,例如 、 ,...PFADDPFCOUNTSpring中文文档

ListOperationsSpring中文文档

Redis 列表操作Spring中文文档

SetOperationsSpring中文文档

Redis 集操作Spring中文文档

ValueOperationsSpring中文文档

Redis 字符串(或值)操作Spring中文文档

ZSetOperationsSpring中文文档

Redis zset(或排序集)操作Spring中文文档

键绑定操作Spring中文文档

BoundGeoOperationsSpring中文文档

Redis 密钥绑定的地理空间操作Spring中文文档

BoundHashOperationsSpring中文文档

Redis 哈希键绑定操作Spring中文文档

BoundKeyOperationsSpring中文文档

Redis 键绑定操作Spring中文文档

BoundListOperationsSpring中文文档

Redis 列表键绑定操作Spring中文文档

BoundSetOperationsSpring中文文档

Redis 设置键绑定操作Spring中文文档

BoundValueOperationsSpring中文文档

Redis 字符串(或值)键绑定操作Spring中文文档

BoundZSetOperationsSpring中文文档

Redis zset(或排序集)键绑定操作Spring中文文档

接口 描述

键类型操作Spring中文文档

ReactiveGeoOperationsSpring中文文档

Redis 地理空间操作,例如 、 等)GEOADDGEORADIUSSpring中文文档

ReactiveHashOperationsSpring中文文档

Redis 哈希操作Spring中文文档

ReactiveHyperLogLogOperationsSpring中文文档

Redis HyperLogLog 操作,例如 (、 等)PFADDPFCOUNTSpring中文文档

ReactiveListOperationsSpring中文文档

Redis 列表操作Spring中文文档

ReactiveSetOperationsSpring中文文档

Redis 集操作Spring中文文档

ReactiveValueOperationsSpring中文文档

Redis 字符串(或值)操作Spring中文文档

ReactiveZSetOperationsSpring中文文档

Redis zset(或排序集)操作Spring中文文档

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

RedisTemplate使用基于 Java 的序列化程序进行大多数操作。 这意味着模板写入或读取的任何对象都通过 Java 进行序列化和反序列化。Spring中文文档

您可以更改模板上的序列化机制,Redis 模块提供了多种实现,这些实现在包中可用。 有关详细信息,请参阅序列化程序。 还可以将任何序列化程序设置为 null,并通过将属性设置为 来将 RedisTemplate 与原始字节数组一起使用 。 请注意,该模板要求所有键均为非 null。 但是,只要基础序列化程序接受值,值就可以为 null。 有关详细信息,请阅读每个序列化程序的 Javadoc。org.springframework.data.redis.serializerenableDefaultSerializerfalseSpring中文文档

对于需要特定模板视图的情况,请将该视图声明为依赖项并注入模板。 容器会自动执行转换,消除调用,如以下示例所示:opsFor[X]Spring中文文档

配置模板 API
@Configuration
class MyConfig {

  @Bean
  LettuceConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory();
  }

  @Bean
  RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {

    RedisTemplate<String, String> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    return template;
  }
}
@Configuration
class MyConfig {

  @Bean
  LettuceConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory();
  }

  @Bean
  ReactiveRedisTemplate<String, String> ReactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) {
    return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory"/>
  <!-- redis template definition -->
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="redisConnectionFactory"/>
  ...

</beans>
使用[Reactive]RedisTemplate
public class Example {

  // inject the actual operations
  @Autowired
  private RedisOperations<String, String> operations;

  // inject the template as ListOperations
  @Resource(name="redisTemplate")
  private ListOperations<String, String> listOps;

  public void addLink(String userId, URL url) {
    listOps.leftPush(userId, url.toExternalForm());
  }
}
public class Example {

  // inject the actual template
  @Autowired
  private ReactiveRedisOperations<String, String> operations;

  public Mono<Long> addLink(String userId, URL url) {
    return operations.opsForList().leftPush(userId, url.toExternalForm());
  }
}
引用实例上的操作的首选方法是通过接口。[Reactive]RedisTemplate[Reactive]RedisOperations
接口 描述

键类型操作Spring中文文档

GeoOperationsSpring中文文档

Redis 地理空间操作,例如 、 ,...GEOADDGEORADIUSSpring中文文档

HashOperationsSpring中文文档

Redis 哈希操作Spring中文文档

HyperLogLogOperationsSpring中文文档

Redis HyperLogLog 操作,例如 、 ,...PFADDPFCOUNTSpring中文文档

ListOperationsSpring中文文档

Redis 列表操作Spring中文文档

SetOperationsSpring中文文档

Redis 集操作Spring中文文档

ValueOperationsSpring中文文档

Redis 字符串(或值)操作Spring中文文档

ZSetOperationsSpring中文文档

Redis zset(或排序集)操作Spring中文文档

键绑定操作Spring中文文档

BoundGeoOperationsSpring中文文档

Redis 密钥绑定的地理空间操作Spring中文文档

BoundHashOperationsSpring中文文档

Redis 哈希键绑定操作Spring中文文档

BoundKeyOperationsSpring中文文档

Redis 键绑定操作Spring中文文档

BoundListOperationsSpring中文文档

Redis 列表键绑定操作Spring中文文档

BoundSetOperationsSpring中文文档

Redis 设置键绑定操作Spring中文文档

BoundValueOperationsSpring中文文档

Redis 字符串(或值)键绑定操作Spring中文文档

BoundZSetOperationsSpring中文文档

Redis zset(或排序集)键绑定操作Spring中文文档

接口 描述

键类型操作Spring中文文档

ReactiveGeoOperationsSpring中文文档

Redis 地理空间操作,例如 、 等)GEOADDGEORADIUSSpring中文文档

ReactiveHashOperationsSpring中文文档

Redis 哈希操作Spring中文文档

ReactiveHyperLogLogOperationsSpring中文文档

Redis HyperLogLog 操作,例如 (、 等)PFADDPFCOUNTSpring中文文档

ReactiveListOperationsSpring中文文档

Redis 列表操作Spring中文文档

ReactiveSetOperationsSpring中文文档

Redis 集操作Spring中文文档

ReactiveValueOperationsSpring中文文档

Redis 字符串(或值)操作Spring中文文档

ReactiveZSetOperationsSpring中文文档

Redis zset(或排序集)操作Spring中文文档

以字符串为中心的便利类

由于存储在 Redis 中的键和值很常见,因此 Redis 模块分别提供了 和 的两个扩展(及其实现),并作为密集字符串操作的便捷一站式解决方案。 除了绑定到键之外,模板和连接还使用下面的键,这意味着存储的键和值是人类可读的(假设 Redis 和代码中都使用相同的编码)。 以下列表显示了一个示例:java.lang.StringRedisConnectionRedisTemplateStringRedisConnectionDefaultStringRedisConnectionStringRedisTemplateStringStringRedisSerializerSpring中文文档

@Configuration
class RedisConfiguration {

  @Bean
  LettuceConnectionFactory redisConnectionFactory() {
    return new LettuceConnectionFactory();
  }

  @Bean
  StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {

    StringRedisTemplate template = new StringRedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
  }
}
@Configuration
class RedisConfiguration {

  @Bean
  LettuceConnectionFactory redisConnectionFactory() {
    return new LettuceConnectionFactory();
  }

  @Bean
  ReactiveStringRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
    return new ReactiveStringRedisTemplate<>(factory);
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory"/>

  <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="redisConnectionFactory"/>

</beans>
public class Example {

  @Autowired
  private StringRedisTemplate redisTemplate;

  public void addLink(String userId, URL url) {
    redisTemplate.opsForList().leftPush(userId, url.toExternalForm());
  }
}
public class Example {

  @Autowired
  private ReactiveStringRedisTemplate redisTemplate;

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

与其他 Spring 模板一样,您可以通过界面直接与 Redis 对话。 此功能为您提供完全控制权,因为它直接与 . 请注意,回调接收使用 a 时的实例。 以下示例演示如何使用该接口:RedisTemplateStringRedisTemplateRedisCallbackRedisConnectionStringRedisConnectionStringRedisTemplateRedisCallbackSpring中文文档

public void useCallback() {

  redisOperations.execute(new RedisCallback<Object>() {
    public Object doInRedis(RedisConnection connection) throws DataAccessException {
      Long size = connection.dbSize();
      // Can cast to StringRedisConnection if using a StringRedisTemplate
      ((StringRedisConnection)connection).set("key", "value");
    }
   });
}

序列化程序

从框架的角度来看,Redis中存储的数据只有字节。 虽然 Redis 本身支持各种类型,但在大多数情况下,这些类型指的是数据的存储方式,而不是它所代表的内容。 由用户决定是否将信息转换为字符串或任何其他对象。Spring中文文档

在 Spring Data 中,用户(自定义)类型和原始数据之间的转换(反之亦然)由包中的 Spring Data Redis 处理。org.springframework.data.redis.serializerSpring中文文档

此包包含两种类型的序列化程序,顾名思义,它们负责序列化过程:Spring中文文档

这些变体之间的主要区别在于,当读者和写入器使用 时,它们主要序列化为 。RedisSerializerbyte[]ByteBufferSpring中文文档

有多种实现可用(包括本文档中已经提到的两种):Spring中文文档

但是,可以通过Spring OXM支持用于对象/ XML映射,或者以JSON格式存储数据。OxmSerializerJackson2JsonRedisSerializerGenericJackson2JsonRedisSerializerSpring中文文档

请注意,存储格式不仅限于值。 它可以用于键、值或哈希,没有任何限制。Spring中文文档

缺省情况下,配置为使用 Java 本机序列化。 众所周知,Java 本机序列化允许运行由有效负载引起的远程代码,这些有效负载利用易受攻击的库和类注入未经验证的字节码。 操作的输入可能会导致在反序列化步骤期间在应用程序中运行不需要的代码。 因此,不要在不受信任的环境中使用序列化。 通常,我们强烈建议改用任何其他消息格式(例如 JSON)。RedisCacheRedisTemplateSpring中文文档

如果您担心 Java 序列化导致的安全漏洞,请考虑核心 JVM 级别的通用序列化过滤器机制:Spring中文文档

缺省情况下,配置为使用 Java 本机序列化。 众所周知,Java 本机序列化允许运行由有效负载引起的远程代码,这些有效负载利用易受攻击的库和类注入未经验证的字节码。 操作的输入可能会导致在反序列化步骤期间在应用程序中运行不需要的代码。 因此,不要在不受信任的环境中使用序列化。 通常,我们强烈建议改用任何其他消息格式(例如 JSON)。RedisCacheRedisTemplateSpring中文文档

如果您担心 Java 序列化导致的安全漏洞,请考虑核心 JVM 级别的通用序列化过滤器机制:Spring中文文档