<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=SpringBoot%EF%BC%9A%E6%95%B4%E5%90%88Redis</id>
	<title>SpringBoot：整合Redis - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=SpringBoot%EF%BC%9A%E6%95%B4%E5%90%88Redis"/>
	<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=SpringBoot%EF%BC%9A%E6%95%B4%E5%90%88Redis&amp;action=history"/>
	<updated>2026-05-18T02:20:49Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=SpringBoot%EF%BC%9A%E6%95%B4%E5%90%88Redis&amp;diff=4837&amp;oldid=prev</id>
		<title>Eijux：​建立内容为“category:Redis category:SpringBoot  == 关于 == 关于使用 Redis 的使用： # 参见：spring-data-redis 的使用实践 # 参见：Spring 的…”的新页面</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=SpringBoot%EF%BC%9A%E6%95%B4%E5%90%88Redis&amp;diff=4837&amp;oldid=prev"/>
		<updated>2021-11-08T16:08:18Z</updated>

		<summary type="html">&lt;p&gt;建立内容为“&lt;a href=&quot;/%E5%88%86%E7%B1%BB:Redis&quot; title=&quot;分类:Redis&quot;&gt;category:Redis&lt;/a&gt; &lt;a href=&quot;/%E5%88%86%E7%B1%BB:SpringBoot&quot; title=&quot;分类:SpringBoot&quot;&gt;category:SpringBoot&lt;/a&gt;  == 关于 == 关于使用 Redis 的使用： # 参见：&lt;a href=&quot;/Spring-data-redis_%E7%9A%84%E4%BD%BF%E7%94%A8%E5%AE%9E%E8%B7%B5&quot; title=&quot;Spring-data-redis 的使用实践&quot;&gt;spring-data-redis 的使用实践&lt;/a&gt; # 参见：Spring 的…”的新页面&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[category:Redis]]&lt;br /&gt;
[[category:SpringBoot]]&lt;br /&gt;
&lt;br /&gt;
== 关于 ==&lt;br /&gt;
关于使用 Redis 的使用：&lt;br /&gt;
# 参见：[[spring-data-redis 的使用实践]]&lt;br /&gt;
# 参见：[[Spring 的 Redis缓存注解]]&lt;br /&gt;
&lt;br /&gt;
== 配置文件 ==&lt;br /&gt;
# '''配置依赖'''：在 '''pom.xml''' 两种引入 Redis 依赖支持&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;!--  加载 spring boot redis 包  --&amp;gt;&lt;br /&gt;
	&amp;lt;dependency&amp;gt;&lt;br /&gt;
		&amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;&lt;br /&gt;
		&amp;lt;artifactId&amp;gt;spring-boot-starter-data-redis&amp;lt;/artifactId&amp;gt;&lt;br /&gt;
	&amp;lt;/dependency&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &amp;lt;!-- 因为，&lt;br /&gt;
	     ，&lt;br /&gt;
		 所以，考虑到常用性，直接用 spring-boot-starter-web 了 --&amp;gt;&lt;br /&gt;
	&amp;lt;dependency&amp;gt;&lt;br /&gt;
		&amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;&lt;br /&gt;
		&amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;&lt;br /&gt;
	&amp;lt;/dependency&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
#* 因为，自定义的模板中用到了 '''Jackson2JsonRedisSerializer''' 序列化器：所以需要引入 '''spring-boot-starter-json'''，而 '''spring-boot-starter-web''' 本身依赖有很多其他依赖（包括 spring-boot-starter-json）。&lt;br /&gt;
#** 如果不引入 spring-boot-starter-web，则需要单独引入 spring-boot-starter-json；&lt;br /&gt;
# '''连接信息'''：在 '''application.properties''' 中配置&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;properties&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
# 配置 Redis：&lt;br /&gt;
# Redis数据库索引（默认为0）&lt;br /&gt;
spring.redis.database=0&lt;br /&gt;
# Redis服务器地址&lt;br /&gt;
spring.redis.host=192.168.255.129&lt;br /&gt;
# Redis服务器连接端口&lt;br /&gt;
spring.redis.port=6379&lt;br /&gt;
# Redis服务器连接密码（默认为空）&lt;br /&gt;
spring.redis.password=&lt;br /&gt;
# 连接池最大连接数（使用负值表示没有限制）&lt;br /&gt;
spring.redis.jedis.pool.max-active=100&lt;br /&gt;
# 连接池最大阻塞等待时间（使用负值表示没有限制）&lt;br /&gt;
spring.redis.jedis.pool.max-wait=-1&lt;br /&gt;
# 连接池中的最大空闲连接&lt;br /&gt;
spring.redis.jedis.pool.max-idle=10&lt;br /&gt;
# 连接池中的最小空闲连接&lt;br /&gt;
spring.redis.jedis.pool.min-idle=0&lt;br /&gt;
# 连接超时时间（毫秒）&lt;br /&gt;
spring.redis.timeout=5000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
#* 或者：使用“'''application.yml'''”：&lt;br /&gt;
#*: &amp;lt;syntaxhighlight lang=&amp;quot;properties&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
spring:&lt;br /&gt;
  redis:&lt;br /&gt;
    database: 0&lt;br /&gt;
    host: 192.168.255.129&lt;br /&gt;
    port: 6379&lt;br /&gt;
    password: 123456&lt;br /&gt;
    jedis:&lt;br /&gt;
      pool:&lt;br /&gt;
        max-active: 100&lt;br /&gt;
        max-idle: 10&lt;br /&gt;
        max-wait: -1&lt;br /&gt;
        min-idle: 0&lt;br /&gt;
    timeout: 1000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 配置类 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 使用Redis ==&lt;br /&gt;
=== 使用：模板 ===&lt;br /&gt;
* 参考：[[Spring-data-redis 的使用实践]] 中 '''[http://wiki.eijux.com/Spring-data-redis_%E7%9A%84%E4%BD%BF%E7%94%A8%E5%AE%9E%E8%B7%B5#.E4.BD.BF.E7.94.A8_RedisTemplate_.E6.A8.A1.E6.9D.BF_API 使用 RedisTemplate 模板 API]、[http://wiki.eijux.com/Spring-data-redis_%E7%9A%84%E4%BD%BF%E7%94%A8%E5%AE%9E%E8%B7%B5#.E4.BD.BF.E7.94.A8_RedisTemplate_.E6.A8.A1.E6.9D.BF_API_.E5.AE.8C.E6.88.90_CRUD_.E7.9A.84.E5.AE.9E.E8.B7.B5.E6.A1.88.E4.BE.8B 使用 RedisTemplate 模板 API 完成 CRUD 的实践案例]'''&lt;br /&gt;
&lt;br /&gt;
=== 使用：注解 ===&lt;br /&gt;
* 参考：[[Spring 的 Redis缓存注解]] 中 '''[http://wiki.eijux.com/Spring_%E7%9A%84_Redis%E7%BC%93%E5%AD%98%E6%B3%A8%E8%A7%A3#.E4.BD.BF.E7.94.A8_Spring_.E7.BC.93.E5.AD.98.E6.B3.A8.E8.A7.A3.E5.AE.8C.E6.88.90_CRUD_.E7.9A.84.E5.AE.9E.E8.B7.B5.E6.A1.88.E4.BE.8B 使用 Spring 缓存注解完成 CRUD 的实践案例]'''&lt;br /&gt;
&lt;br /&gt;
== 关于：Redis模板 ==&lt;br /&gt;
 以前都是直接使用 Lettuce 或 Jedis 等对 Redis 进行操作，不过后来 spring-boot-starter-data-redis 模板块儿项目对 Lettuce、Jedis 进行了封装改进，对外提供了一些操作 Redis 的模板，现在如果是 SpringBoot 的项目，一般都是直接使用模板进行 Redis 操作。&lt;br /&gt;
&lt;br /&gt;
spring-boot-starter-data-redis 提供了好几个 Redis 操作模板：&lt;br /&gt;
* '''RedisTemplate'''&lt;br /&gt;
* '''StringRedisTemplate'''&lt;br /&gt;
* ReactiveRedisTemplate&lt;br /&gt;
* ReactiveStringRedisTemplate&lt;br /&gt;
* RedisKeyValueTemplate&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 其中 RedisTemplate 和 StringRedisTemplate 是最常用的。&lt;br /&gt;
 &lt;br /&gt;
 两者的数据是不共通的；也就是说 StringRedisTemplate 只能管理 StringRedisTemplate 里面的数据，RedisTemplate 只能管理 RedisTemplate 中的数据。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== SpringBoot 默认注入的模板 ===&lt;br /&gt;
 在 SpringBoot 中 spring-boot-autoconfigure 模块的 RedisAutoConfiguration 配置类也默认注入了 RedisTemplate 和 StringRedisTemplate。&lt;br /&gt;
 &lt;br /&gt;
 但，这个 RedisTemplate 的泛型是 '''&amp;lt;Object,Object&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
RedisAutoConfiguration 源码：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
@Configuration&lt;br /&gt;
@ConditionalOnClass(RedisOperations.class)&lt;br /&gt;
@EnableConfigurationProperties(RedisProperties.class)&lt;br /&gt;
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })&lt;br /&gt;
public class RedisAutoConfiguration {&lt;br /&gt;
    @Bean&lt;br /&gt;
    @ConditionalOnMissingBean(name = &amp;quot;redisTemplate&amp;quot;)&lt;br /&gt;
    public RedisTemplate&amp;lt;Object, Object&amp;gt; redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {&lt;br /&gt;
        RedisTemplate&amp;lt;Object, Object&amp;gt; template = new RedisTemplate&amp;lt;&amp;gt;();&lt;br /&gt;
        template.setConnectionFactory(redisConnectionFactory);&lt;br /&gt;
        return template;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Bean&lt;br /&gt;
    @ConditionalOnMissingBean&lt;br /&gt;
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {&lt;br /&gt;
        StringRedisTemplate template = new StringRedisTemplate();&lt;br /&gt;
        template.setConnectionFactory(redisConnectionFactory);&lt;br /&gt;
        return template;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
如上：&lt;br /&gt;
# “@Configuration”：表明这是一个配置类，并尝试注入Spring容器。&lt;br /&gt;
# “@ConditionalOnClass(RedisOperations.class)”：当 classpath 下存在“RedisOperations”类时，被“@ConditionalOnClass”注解的类才能注入 Spring 容器。&lt;br /&gt;
# “@EnableConfigurationProperties(RedisProperties.class)”：将被“@ConfigurationProperties”注解的“RedisProperties” 类注入为 Spring 容器的 Bean。&lt;br /&gt;
# “@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })”：注入“LettuceConnectionConfiguration”与“JedisConnectionConfiguration”为 Spring 容器的 Bean。&lt;br /&gt;
# “@Bean”：尝试将实例注入 Spring 容器。&lt;br /&gt;
# “@ConditionalOnMissingBean”：当容器中不存在类的实例时，被“@ConditionalOnMissingBean”注解的类 或 被“@ConditionalOnMissingBean”注解的方法的返回的实例，才能注入Spring容器。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
总结：&lt;br /&gt;
 当 SpringBoot 中引入了对 Redis 的支持，且用户没有主动注入 RedisTemplate 和 StringRedisTemplate 模板时，SpringBoot 才会往 Spring 中注入默认的 RedisTemplate 和 StringRedisTemplate 模板。&lt;br /&gt;
 &lt;br /&gt;
 换句话说：'''如果 Spring 容器中有了 RedisTemplate 对象了，这个自动配置的 RedisTemplate 和 StringRedisTemplate 不会实例化。'''&lt;br /&gt;
&lt;br /&gt;
=== RedisTemplate 和 StringRedisTemplate ===&lt;br /&gt;
 从源码可以看出，SpringBoot 自动生成的模板类型为：'''RedisTemplate&amp;lt;Object, Object&amp;gt;''' 和 '''StringRedisTemplate'''。&lt;br /&gt;
&lt;br /&gt;
区别：&lt;br /&gt;
# '''RedisTemplate&amp;lt;Object, Object&amp;gt;'''：默认各个“key-value”序列化器，均采用 '''JdkSerializationRedisSerializer'''。&lt;br /&gt;
#* 所以：其存储的 key、value 为序列化转义的值；&lt;br /&gt;
#* 适用于：要存储的数据比较复杂时；（如：一个对象，一个map对象，一个数组等）&lt;br /&gt;
# '''StringRedisTemplate'''：默认各个“key-value”序列化器，均采用 StringRedisSerializer.UTF_8 字符集的 '''StringRedisSerializer'''。&lt;br /&gt;
#* 所以：其存储的 key、value 为普通字符串；&lt;br /&gt;
#* 适用于：存储的数据比较简单，无需改动时；(字符串类型数据)&lt;br /&gt;
&lt;br /&gt;
* “key-value”序列化器，包括：keySerializer、valueSerializer、hashKeySerializer、hashValueSerializer。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
: &amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
// 存储的 key 是：“\xac\xed\x00\x05t\x00\x03msg”&lt;br /&gt;
// 存储的 value 是：“\xac\xed\x00\x05t\x00\x03msg”&lt;br /&gt;
redisTemplate.opsForValue().set(“msg”,“hello world”);&lt;br /&gt;
&lt;br /&gt;
// 存储的 key 是：“msg”&lt;br /&gt;
// 存储的 value 是：“\xac\xed\x00\x05t\x00\x03msg”&lt;br /&gt;
stringRedisTemplate.opsForValue().append(“msg”,“hello”);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 由于 RedisTemplate&amp;lt;Object, Object&amp;gt; 存储的数据不便于阅读，读取、存储时需要相应的转换。&lt;br /&gt;
 &lt;br /&gt;
 所以开发时一般都在 Redis '''配置类'''中自定义模板 Bean：'''RedisTemplate&amp;lt;String, Object&amp;gt;'''&lt;br /&gt;
 &lt;br /&gt;
    1、其 keySerializer、hashKeySerializer 使用“'''StringRedisSerializer'''”；&lt;br /&gt;
    2、其 valueSerializer、hashValueSerializer 使用“'''Jackson2JsonRedisSerializer'''”；&lt;br /&gt;
&lt;br /&gt;
== 关于：缓存管理器 ==&lt;br /&gt;
=== 使用一个管理器管理所有缓存类 ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== 配置多个缓存管理器分开管理 ===&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>