关于java:Spring-Boot-2x基础教程使用集中式缓存Redis

2次阅读

共计 3123 个字符,预计需要花费 8 分钟才能阅读完成。

之前咱们介绍了两种过程内缓存的用法,包含 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

对于连接池的配置,留神几点:

  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 语句,所以是走的缓存获取

整合胜利!

思考题

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

代码示例

本文的相干例子能够查看上面仓库中的 chapter5-4 目录:

  • Github:https://github.com/dyc87112/SpringBoot-Learning/
  • Gitee:https://gitee.com/didispace/SpringBoot-Learning/

如果您感觉本文不错,欢送 Star 反对,您的关注是我保持的能源!

本文首发:Spring Boot 2.x 基础教程:应用集中式缓存 Redis,转载请注明出处。
欢送关注我的公众号:程序猿 DD,取得独家整顿的学习资源和日常干货推送。点击中转本系列教程目录。

正文完
 0