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

JetCache是一个阿里巴巴开源的基于Java的缓存系统封装,提供统一的API和注解来简化缓存的使用,SpringBoot2.1.4截止到当前为止,Maven仓库发布的整合JetCache的版本为2.6.0.M1,JetCache提供了 本地缓存LinkedHashMapCache和CaffeineCache( 一个高性能的 Java 缓存库)两种,远程可以支持Tair,Redis,今天我们就来选择使用 Caffeine和Redis进行整合来实现二级缓存.

首先要引入依赖,

Caffeine是Spring Boot 2取代Guava,内置的本地缓存

<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
</dependency>

Redis是远程缓存,Redis比较常用的客户端有Jedis,Lettuce,Redisson,Spring Boot 2不再推荐使用Jedis作为redis的缓存客户端,而默认采用lettuce作为默认客户端,当然还有Redisson,但是由于JetCache不支持Redisson,只支持jedis和letture.所以我们只能选择这两个。JetCache的官方文档比较常用的示例都是用的Jedis或者使用的Redis哨兵集群模式,如果你的Redis是单点服务器,那么使用jedis也可以。这里我们拿Lettuce作为客户端举例,Redis则是采用的是Cluster集群模式。

<!-- https://mvnrepository.com/artifact/com.alicp.jetcache/jetcache-starter-redis-lettuce -->
<dependency>
    <groupId>com.alicp.jetcache</groupId>
	<artifactId>jetcache-starter-redis-lettuce</artifactId>
	<version>2.6.0.M1</version>
</dependency>

版本为最新的2.6.0.M1,因为JetCache 2.3以上版本才支持Lettuce,

同时还要引入Redis的依赖

<!-- 添加对redis的支持 -->
<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>

application.yml文件中要配置相关配置,有一点JetCache做的不好,就是yml下jetcache没有代码提示

jetcache:
  statIntervalMinutes: 1 #统计间隔
  areaInCacheName: false
  local:
    default: #默认area
      type: caffeine
      keyConvertor: fastjson
  remote:
    default:
      type: redis.lettuce #使用lettuce
      keyConvertor: fastjson
      valueEncoder: java
      valueDecoder: java
      poolConfig:
        minIdle: 1
        maxIdle: 50
        maxTotal: 1000
        maxWait: 1000  
#     uri: ['redis://[email protected]:6379/0','redis://[email protected]:6379/0','redis://[email protected]:6379/0']
      uri:
        - redis://[email protected]:6379/0  #redis://密码@IP:端口/库
        - redis://[email protected]:6379/0
        - redis://[email protected]:6379/0
      readFrom: masterPreferred #master优先

JetCache还内置了统计功能,statIntervalMinutes为统计间隔时间,这些基本配置可以从git上wiki获取。

如果除了使用JetCache注解的方式使用缓存以外,如果你还需要通过使用StringRedisTemplate或者RedisTemplate的方式使用Reids,那么还需要配置,Spring Boot 2默认也是是要弄个lettuce来作为java客户端连接的

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

然后还要在Application.java启动文件中添加注解、com.xxxx为自己的项目包名

@SpringBootApplication
@EnableMethodCache(basePackages = { "com.xxxx" })
@EnableCreateCacheAnnotation
public class Application {

}

如何通过JetCache的注解使用

@RestController
public class UserController {

    // 定义一个是String类型的远程缓存
    @CreateCache(name ="i5xforyou.username", expire = 120, cacheType = CacheType.REMOTE)
    private Cache<String, String> userNameCache;
    // 定义一个是User对象的二级缓存(本地+远程)
    @CreateCache(name ="i5xforyou.user", localExpire = 60, localLimit = 100, expire = 120, cacheType = CacheType.BOTH)
    private Cache<String, User> userCache;


    
    @GetMapping(value = "/getUser")
    public String getUserInfo() {
            
            userNameCache.put("123", "userName");
            System.out.println("userNameCache : " + userNameCache.get("123"));
            
            User user = new User();
            user.setUserName("user.userName");
            userCache.put("1234", user);
            System.out.println("userCache: " + userCache.get("1234").geUserName());

            return "";
        
    }
    
}

这里有一点要特别注意的: 就是User一定要 implements Serializable 进行序列化,否则userCache.get("1234")将会取出来一个null对象,因为无法存进Redis缓存内。

通过以上的配置,就可以使用JetCache注解的方式来使用缓存了。其他用法还可以继续关注https://github.com/alibaba/jetcache 来学习

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章