文章目录
- 一、配置
- 二、问题
- 三、文档
一、配置
(0)POM
配置
pom.xml
下配置
(1)客户端
主流有:
-
JedisConnectionFactory
: 使用Jedis
作为Redis
客户端 -
LettuceConnectionFactory
:使用Lettuce
作为Redis
客户端
Lettuce
是基于Netty
的开源连接器
(2)操作
Spring Data Redis
提供了 RedisTemplate
和 StringRedisTemplate
两个模板来进行数据操作。
(3)序列化
RedisTemplate
默认使用 JdkSerializationRedisSerializer
StringRedisTemplate
默认使用 StringRedisSerializer
Spring Data JPA
提供了如下Serializer
:
-
GenericToStringSerializer
-
Jackson2JsonRedisSerializer
-
JacksonJsonRedisSerializer
-
JdkSerializationRedisSerializer
-
OxmSerializer
-
StringRedisSerializer
对应上文RedisConfig.java
中:
(4)属性配置
在 application.properties
二、问题
(1)SpringBoot 2.x 默认依赖是 Lettuce
,为什么不是Jedis
?
官方解释:https://github.com/spring-projects/spring-session/issues/789
性能测试:https://translate.google.com/translate?hl=&sl=ko&tl=en&u=https%3A%2F%2Fjojoldu.tistory.com%2F418
Lettuce
:
Lettuce is built on netty and connection instances (StatefulRedisConnection) can be shared across multiple threads. So a multi-threaded application can use a single connection regardless the number of concurrent threads that interact with Lettuce.
按凡凡理解:就是底层支持的是netty
,有经验啊。
Jedis
:
Each concurrent thread using Jedis gets its own Jedis instance for the duration of Jedis interaction. Connection pooling comes at the cost of a physical connection per Jedis instance which increases the number of Redis connections.
按凡凡理解:每一个业务线程都会获得Jedis
的一个实例来连接,相对浪费实例。
三、文档
https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis.repositories