此版本仍在开发中,尚未被视为稳定版本。如需最新的稳定版本,请使用 Spring Data Redis 3.4.0! |
使用 Object throughRedisTemplate
大多数用户可能会使用RedisTemplate
及其相应的软件包org.springframework.data.redis.core
或其反应式变体ReactiveRedisTemplate
.
事实上,由于其丰富的功能集,该模板是 Redis 模块的中心类。
该模板为 Redis 交互提供了高级抽象。
而[Reactive]RedisConnection
提供接受和返回二进制值 (byte
数组),该模板负责序列化和连接管理,使用户无需处理此类细节。
这RedisTemplate
类实现RedisOperations
interface 及其响应式变体ReactiveRedisTemplate
实现ReactiveRedisOperations
.
在[Reactive]RedisTemplate 实例通过[Reactive]RedisOperations 接口。 |
此外,该模板还提供了作视图(遵循 Redis 命令参考中的分组),这些视图提供了丰富的泛型接口,用于处理特定类型或特定键(通过KeyBound
interfaces),如下表所述:
作视图
-
Imperative
-
Reactive
接口 | 描述 |
---|---|
键类型作 |
|
Redis 地理空间作,例如 |
|
Redis 哈希作 |
|
Redis HyperLogLog作,例如 |
|
Redis 列表作 |
|
Redis set作 |
|
Redis 字符串(或值)作 |
|
Redis zset(或排序集)作 |
|
键绑定作 |
|
Redis 键绑定地理空间作 |
|
Redis 哈希键绑定作 |
|
Redis 键绑定作 |
|
Redis list 键绑定作 |
|
Redis set 键绑定作 |
|
Redis 字符串(或值)键绑定作 |
|
Redis zset(或排序集)键绑定作 |
接口 | 描述 |
---|---|
键类型作 |
|
Redis 地理空间作,例如 |
|
Redis 哈希作 |
|
Redis HyperLogLog作,例如 ( |
|
Redis 列表作 |
|
Redis set作 |
|
Redis 字符串(或值)作 |
|
Redis zset(或排序集)作 |
配置后,模板是线程安全的,并且可以在多个实例之间重复使用。
RedisTemplate
对大多数作使用基于 Java 的序列化程序。
这意味着模板写入或读取的任何对象都通过 Java 进行序列化和反序列化。
您可以更改模板上的序列化机制,Redis 模块提供了多种实现,这些实现在org.springframework.data.redis.serializer
包。
有关更多信息,请参阅 序列化程序 。
您还可以将任何序列化器设置为 null,并通过将 RedisTemplate 设置为原始字节数组enableDefaultSerializer
property 设置为false
.
请注意,模板要求所有键都为非 null。
但是,只要基础序列化程序接受值,值就可以为 null。
有关更多信息,请阅读每个序列化程序的 Javadoc。
对于需要某个模板视图的情况,请将该视图声明为依赖项并注入模板。
容器自动执行转换,从而消除opsFor[X]
调用,如以下示例所示:
-
Java Imperative
-
Java Reactive
-
XML
@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(ReactoveRedisConnectionFactory 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
-
Imperative
-
Reactive
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());
}
}
以字符串为中心的便利类
由于存储在 Redis 中的键和值是很常见的java.lang.String
中,Redis 模块提供了两个扩展RedisConnection
和RedisTemplate
,分别是StringRedisConnection
(及其DefaultStringRedisConnection
implementation 和StringRedisTemplate
作为密集 String作的便捷一站式解决方案。
除了被绑定到String
键中,模板和连接使用StringRedisSerializer
下,这意味着存储的键和值是人类可读的(假设在 Redis 和您的代码中使用相同的编码)。
以下清单显示了一个示例:
-
Java Imperative
-
Java Reactive
-
XML
@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>
-
Imperative
-
Reactive
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 模板一样,RedisTemplate
和StringRedisTemplate
让您通过RedisCallback
接口。
此功能为您提供了完全控制权,因为它直接与RedisConnection
.
请注意,回调接收StringRedisConnection
当StringRedisTemplate
被使用。
以下示例演示如何使用RedisCallback
接口:
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 Data 中,用户(自定义)类型和原始数据(反之亦然)之间的转换由 Spring Data Redis 在org.springframework.data.redis.serializer
包。
此包包含两种类型的序列化程序,顾名思义,它们负责序列化过程:
-
使用
RedisElementReader
和RedisElementWriter
.
这些变体之间的主要区别在于RedisSerializer
主要序列化为byte[]
而读取器和写入器使用ByteBuffer
.
有多种实现可用(包括本文档中已经提到的两种实现):
-
JdkSerializationRedisSerializer
,默认情况下用于RedisCache
和RedisTemplate
. -
这
StringRedisSerializer
.
但是,可以使用OxmSerializer
通过 Spring OXM 支持进行对象/XML 映射,或者Jackson2JsonRedisSerializer
或GenericJackson2JsonRedisSerializer
用于以 JSON 格式存储数据。
请注意,存储格式不仅限于值。 它可以不受任何限制地用于键、值或哈希。
默认情况下, 如果您担心 Java 序列化导致的安全漏洞,请考虑核心 JVM 级别的通用序列化过滤器机制: |