Spring Boot 2.1.4整合JetCache缓存框架实现本地caffeine远程redis存储(二、使用Redis-SpringData)

五一放假过后JetCache进行了最新版本2.6.0.M2 的升级

从此版本的Git提交记录看,此版本增加了对  spring-boot-starter-data-redis 的支持,这意味着配置更简单了,上一个版本对Redis的支持是使用Lettuce,直接对接lettuce, 如果需要使用RedisTemplate,之前的配置方式需要配置jetCache,同时要配置spring.redis的配置。(https://blog.csdn.net/ankeway/article/details/89711228这里介绍过),而当前这个版本通过测试,只需要配置spring.redis的配置,jetCache不需要配置Redis服务器的信息。

还是以Redis的Cluster集群的模式介绍。

首先要引入依赖,Caffeine的配置方式没有变化

Redis的引用变为

<dependency>
    <groupId>com.alicp.jetcache</groupId>
    <artifactId>jetcache-starter-redis-springdata</artifactId>
    <version>2.6.0.M2</version>
    <optional>true</optional>
</dependency>

而 jetcache-starter-redis-lettuce 无需引用, jetcache-starter-redis-springdata 内置了对 spring-boot-starter-data-redis 的依赖,所以只需要再添加 连接池的依赖即可

<!-- 添加对连接池的支持 -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
</dependency>

application.yml文件中要配置相关配置,最大的变动是不需要使用jetcache来配置redis服务器信息,转而使用spring.redis的配置,jetCache的remote type由 redis.lettuce 变为了redis.springdata

spring:
  redis:
    cluster:
      nodes: 
       - 192.168.14.231:6379
       - 192.168.14.232:6379
       - 192.168.14.233:6379
    password: password
    lettuce:
      pool:
        min-idle: 5
        max-idle: 50
        max-active: 100
        max-wait: 1000


jetcache:
  statIntervalMinutes: 1 #统计间隔分钟
  areaInCacheName: false
  local:
    default: #默认area
      type: caffeine
      keyConvertor: fastjson
  remote:
    default:
      type: redis.springdata # type由 redis.lettuce 变为了redis.springdata
      keyConvertor: fastjson
      valueEncoder: java
      valueDecoder: java

其他的使用方式保持不变,仍需要在启动类添加注解等操作。


版权声明:本文为ankeway原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。