700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Spring Boot 2.x基础教程:使用集中式缓存Redis

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

时间:2023-05-30 01:22:45

相关推荐

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

点击上方蓝色“程序猿DD”,选择“设为星标”

回复“资源”获取独家整理的学习资料!

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

在一些要求高一致性(任何数据变化都能及时的被查询到)的系统和应用中,就不能再使用EhCache来解决了,这个时候使用集中式缓存就可以很好的解决缓存数据的一致性问题。接下来我们就来学习一下,如何在Spring Boot的缓存支持中使用Redis实现数据缓存。

动手试试

本篇的实现将基于上一篇的基础工程来进行。先来回顾下上一篇中的程序要素:

User实体的定义

@Entity@Data@NoArgsConstructorpublicclassUserimplementsSerializable{@Id@GeneratedValueprivateLongid;privateStringname;privateIntegerage;publicUser(Stringname,Integerage){this.name=name;this.age=age;}}

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

@CacheConfig(cacheNames="users")publicinterfaceUserRepositoryextendsJpaRepository<User,Long>{@CacheableUserfindByName(Stringname);}

下面开始改造这个项目:

第一步pom.xml中增加相关依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>mons</groupId><artifactId>commons-pool2</artifactId></dependency>

在Spring Boot 1.x的早期版本中,该依赖的名称为spring-boot-starter-redis,所以在Spring Boot 1.x基础教程中与这里不同。

第二步:配置文件中增加配置信息,以本地运行为例,比如:

spring.redis.host=localhostspring.redis.port=6379spring.redis.lettuce.pool.max-idle=8spring.redis.lettuce.pool.max-active=8spring.redis.lettuce.pool.max-wait=-1msspring.redis.lettuce.pool.min-idle=0spring.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)@SpringBootTestpublicclassChapter54ApplicationTests{@AutowiredprivateUserRepositoryuserRepository;@AutowiredprivateCacheManagercacheManager;@Testpublicvoidtest()throwsException{System.out.println("CacheManagertype:"+cacheManager.getClass());//创建1条记录userRepository.save(newUser("AAA",10));Useru1=userRepository.findByName("AAA");System.out.println("第一次查询:"+u1.getAge());Useru2=userRepository.findByName("AAA");System.out.println("第二次查询:"+u2.getAge());}}

执行测试输出可以得到:

CacheManagertype:classorg.springframework.data.redis.cache.RedisCacheManagerHibernate:selectnext_valasid_valfromhibernate_sequenceforupdateHibernate:updatehibernate_sequencesetnext_val=?wherenext_val=?Hibernate:insertintouser(age,name,id)values(?,?,?)-08-1216:25:26.954INFO68282---[main]io.lettuce.core.EpollProvider:Startingwithoutoptionalepolllibrary-08-1216:25:26.955INFO68282---[main]io.lettuce.core.KqueueProvider:StartingwithoutoptionalkqueuelibraryHibernate:selectuser0_.idasid1_0_,user0_.ageasage2_0_,user0_.nameasname3_0_fromuseruser0_whereuser0_.name=?第一次查询:10第二次查询:10

可以看到:

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

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

整合成功!

思考题

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

代码示例

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

Github:/dyc87112/SpringBoot-Learning/

Gitee:/didispace/SpringBoot-Learning/

如果您觉得本文不错,欢迎Star支持,您的关注是我坚持的动力!

往期推荐

Spring Boot 2.x基础教程:使用EhCache缓存集群

Spring Boot 2.x基础教程:EhCache缓存的使用

Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解

Spring Boot 2.x基础教程:事务管理入门

Spring Boot 2.x基础教程:MyBatis的多数据源配置

Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置

离职成为自由开发者的100天

我在星球与你分享经验、交流成长

????????????????

星球两大分享内容

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。