

Spring Boot 2.x基础教程:使用集中式缓存Redis
source link: http://blog.didispace.com/spring-boot-learning-21-5-4/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring Boot 2.x基础教程:使用集中式缓存Redis
之前我们介绍了两种进程内缓存的用法,包括Spring Boot默认使用的ConcurrentMap缓存以及缓存框架EhCache。虽然EhCache已经能够适用很多应用场景,但是由于EhCache是进程内的缓存框架,在集群模式下时,各应用服务器之间的缓存都是独立的,因此在不同服务器的进程间会存在缓存不一致的情况。即使EhCache提供了集群环境下的缓存同步策略,但是同步依然是需要一定的时间,短暂的缓存不一致依然存在。
在一些要求高一致性(任何数据变化都能及时的被查询到)的系统和应用中,就不能再使用EhCache来解决了,这个时候使用集中式缓存就可以很好的解决缓存数据的一致性问题。接下来我们就来学习一下,如何在Spring Boot的缓存支持中使用Redis实现数据缓存。
本篇的实现将基于上一篇的基础工程来进行。先来回顾下上一篇中的程序要素:
User实体的定义
@Entity
@Data
@NoArgsConstructor
public class User implements Serializable {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
User实体的数据访问实现(涵盖了缓存注解)
@CacheConfig(cacheNames = "users")
public interface UserRepository extends JpaRepository<User, Long> {
@Cacheable
User findByName(String name);
}
下面开始改造这个项目:
第一步:pom.xml
中增加相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
在Spring Boot 1.x的早期版本中,该依赖的名称为
spring-boot-starter-redis
,所以在Spring Boot 1.x基础教程中与这里不同。
第二步:配置文件中增加配置信息,以本地运行为例,比如:
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.shutdown-timeout=100ms
关于连接池的配置,注意几点:
- Redis的连接池配置在1.x版本中前缀为
spring.redis.pool
与Spring Boot 2.x有所不同。- 在1.x版本中采用jedis作为连接池,而在2.x版本中采用了lettuce作为连接池
- 以上配置均为默认值,实际上生产需进一步根据部署情况与业务要求做适当修改.
再来试试单元测试:
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter54ApplicationTests {
@Autowired
private UserRepository userRepository;
@Autowired
private CacheManager cacheManager;
@Test
public void test() throws Exception {
System.out.println("CacheManager type : " + cacheManager.getClass());
// 创建1条记录
userRepository.save(new User("AAA", 10));
User u1 = userRepository.findByName("AAA");
System.out.println("第一次查询:" + u1.getAge());
User u2 = userRepository.findByName("AAA");
System.out.println("第二次查询:" + u2.getAge());
}
}
执行测试输出可以得到:
CacheManager type : class org.springframework.data.redis.cache.RedisCacheManager
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into user (age, name, id) values (?, ?, ?)
2020-08-12 16:25:26.954 INFO 68282 --- [ main] io.lettuce.core.EpollProvider : Starting without optional epoll library
2020-08-12 16:25:26.955 INFO 68282 --- [ main] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=?
第一次查询:10
第二次查询:10
可以看到:
- 第一行输出的CacheManager type为
org.springframework.data.redis.cache.RedisCacheManager
,而不是上一篇中的EhCacheCacheManager
了 - 第二次查询的时候,没有输出SQL语句,所以是走的缓存获取
整合成功!
更多本系列免费教程连载「点击进入汇总目录」
既然EhCache等进程内缓存有一致性问题存在,而Redis性能好而且还能解决一致性问题,那么我们只要学会用Redis就好了咯,为什么还要学进程内缓存呢?先留下你的思考,下一篇我们一起讨论这个问题!欢迎关注本系列教程:《Spring Boot 2.x基础教程》
本文的相关例子可以查看下面仓库中的chapter5-4
目录:
如果您觉得本文不错,欢迎Star
支持,您的关注是我坚持的动力!
Recommend
-
27
集中式内存缓存Guava Cache商领云22016.09.08 09:47:08字数 3,279阅读 18,313 缓存的主要作用是暂时在内存中保存业务系统的数据处理...
-
8
Spring Boot 2.x基础教程:使用EhCache缓存集群 【福利时刻】关...
-
13
在之前的《使用Sentinel实现接口限流》一文中,我们仅依靠引入Spring Cloud Alibaba对Sentinel的整合封装spring-cloud-starter-alibaba-sentinel,就完成了对所有Sprin...
-
10
之前已经介绍了很多在Spring Boot中使用MySQL的案例,包含了Spring Boot最原始的JdbcTemplate、Spring Data JPA以及我们国内最常用的MyBatis。同时,对于一些复杂场景比如:更换Druid数据源,或是多数据源的情况也都做了介绍。
-
3
在一个Spring Boot项目中,连接多个数据源还是比较常见的。之前也介绍了如何在几种常用框架的场景下配置多数据源,具体可见: Spring Boot 2.x基础教程:JdbcTe...
-
7
在一个Spring Boot项目中,连接多个数据源还是比较常见的。之前也介绍了如何在几种常用框架的场景下配置多数据源,具体可见: 当我们采用多数据源的时候,同时也会出现一个这样的特殊场景:我们希望对A数据源的更新和B数据源的更新具备事务性。这样的例...
-
10
前段时间因为团队调整,大部分时间放在了团队上,这系列的更新又耽误了一下。但既然承诺持久更新,那就不会落下,今天开始继续更新这部分的内容! 过了年,重申一下这个系列的目标:目前主要任务就是把Spring Boot 1.x部分没有升级的内容做完升级。我会...
-
13
通过前面一篇集中式缓存的使用教程,我们已经了解了Redis的核心功能:作为K、V存储的高性能缓存。 接下来我们会分几篇来继续讲讲Redis的一些其他强大用法!如果你对此感兴趣,一...
-
5
实现SpringBoot + Redis缓存的源码与教程总结了Redis的基础知识以及如何使用Redis在Spring Boot中集成缓存。首先是依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-sta...
-
3
VMware发布vSphere+和vSAN+,集中式基础架构管理简化运营-51CTO.COM
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK