Spring Boot 2.x基础:使用集中式缓存Redis

存储 存储软件 Redis
我们介绍了两种进程内缓存的用法,包括Spring Boot默认使用的ConcurrentMap缓存以及缓存框架EhCache。

 我们介绍了两种进程内缓存的用法,包括Spring Boot默认使用的ConcurrentMap缓存以及缓存框架EhCache。虽然EhCache已经能够适用很多应用场景,但是由于EhCache是进程内的缓存框架,在集群模式下时,各应用服务器之间的缓存都是独立的,因此在不同服务器的进程间会存在缓存不一致的情况。即使EhCache提供了集群环境下的缓存同步策略,但是同步依然是需要一定的时间,短暂的缓存不一致依然存在。

[[338584]]

在一些要求高一致性(任何数据变化都能及时的被查询到)的系统和应用中,就不能再使用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 nameInteger age) { 
        this.name = name
        this.age = age; 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

User实体的数据访问实现(涵盖了缓存注解)

@CacheConfig(cacheNames = "users"
public interface UserRepository extends JpaRepository<User, Long> { 
 
    @Cacheable 
    User findByName(String name); 
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

下面开始改造这个项目:

第一步: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> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

在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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

关于连接池的配置,注意几点:

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()); 
    } 
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

执行测试输出可以得到:

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

可以看到:

第一行输出的CacheManager type为org.springframework.data.redis.cache.RedisCacheManager,而不是上一篇中的EhCacheCacheManager了

第二次查询的时候,没有输出SQL语句,所以是走的缓存获取

整合成功!

思考题

既然EhCache等进程内缓存有一致性问题存在,而Redis性能好而且还能解决一致性问题,那么我们只要学会用Redis就好了咯,为什么还要学进程内缓存呢?先留下你的思考,下一篇我们一起讨论这个问题!欢迎关注本系列教程:《Spring Boot 2.x基础教程》

代码示例

本文的相关例子可以查看下面仓库中的chapter5-4目录:

Github:https://github.com/dyc87112/SpringBoot-Learning/

Gitee:https://gitee.com/didispace/SpringBoot-Learning/

【本文为51CTO专栏作者“翟永超”的原创稿件,转载请通过51CTO联系作者获取授权】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: 51CTO专栏
相关推荐

2012-02-23 23:33:37

开源memcached

2021-03-04 10:11:50

MongoDBSpring BootSpring Boot

2022-03-18 09:00:00

开发Web服务应用程序

2023-08-22 14:20:21

2021-02-03 10:49:34

JTA分布式事务

2019-11-07 11:21:21

安全软件IT

2022-03-29 14:28:03

架构安全设计

2012-02-21 09:59:52

2009-07-02 19:24:50

安全管理Windows审计

2022-06-29 10:19:57

VMware

2015-07-28 09:44:38

集中式云数据加密安全漏洞

2011-10-19 13:53:11

2009-07-20 11:11:34

分光器FTTP

2025-02-18 07:30:35

2014-08-05 09:15:14

SDN

2017-06-27 10:21:12

vRealize LoNSX日志管理

2021-12-31 08:48:23

Logback日志管理

2024-01-22 13:55:00

2014-08-29 16:39:27

华为

2022-06-06 07:32:44

数据库系统分布式
点赞
收藏

51CTO技术栈公众号