SpringBoot2.x中如何使用集中式缓存Redis

今天就跟大家聊聊有关Spring Boot 2.x中如何使用集中式缓存redis,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、做网站、西乌珠穆沁网络推广、小程序开发、西乌珠穆沁网络营销、西乌珠穆沁企业策划、西乌珠穆沁品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供西乌珠穆沁建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

虽然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 {

    @Cacheable
    User findByName(String name);

}

下面开始改造这个项目:

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


    org.springframework.boot
    spring-boot-starter-data-redis



    org.apache.commons
    commons-pool2

在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. Redis的连接池配置在1.x版本中前缀为spring.redis.pool与Spring Boot 2.x有所不同。

  2. 在1.x版本中采用jedis作为连接池,而在2.x版本中采用了lettuce作为连接池

  3. 以上配置均为默认值,实际上生产需进一步根据部署情况与业务要求做适当修改.

再来试试单元测试:

@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

可以看到:

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

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

整合成功!

看完上述内容,你们对Spring Boot 2.x中如何使用集中式缓存Redis有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


本文标题:SpringBoot2.x中如何使用集中式缓存Redis
网站网址:http://lszwz.com/article/jgphch.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款