SpringBoot 整合 阿里开源的缓存框架JetCache

发布时间:2021-10-25作者:laosun阅读(1275)

SpringBoot 整合 阿里开源的缓存框架JetCache;@Cached、@CacheRefresh、@Cached、@CacheUpdate、@CacheInvalidate

    JetCache是一个基于Java的缓存系统封装,提供统一的API和注解来简化缓存的使用。 JetCache提供了比SpringCache更加强大的注解,可以原生的支持TTL、两级缓存、分布式自动刷新,还提供了Cache接口用于手工缓存操作。 当前有四个实现,RedisCache、TairCache(此部分未在github开源)、CaffeineCache(in memory)和一个简易的LinkedHashMapCache(in memory),要添加新的实现也是非常简单的。

    全部特性:

    1. 通过统一的API访问Cache系统

    2. 通过注解实现声明式的方法缓存,支持TTL和两级缓存

    3. 通过注解创建并配置Cache实例

    4. 针对所有Cache实例和方法缓存的自动统计

    5. Key的生成策略和Value的序列化策略是可以配置的

    6. 分布式缓存自动刷新,分布式锁 (2.2+)

    7. 异步Cache API (2.2+,使用Redis的lettuce客户端时)

    8. Spring Boot支持


    体验一下

    pom.xml

    <!-- 阿里开源的缓存框架JetCache -->
    <dependency>
      <groupId>com.alicp.jetcache</groupId>
      <artifactId>jetcache-starter-redis</artifactId>
      <version>2.6.0</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.70</version>
    </dependency>

    yml配置

    jetcache:
      statIntervalMinutes: 1
      areaInCacheName: false
      local:
        default:
          type: linkedhashmap
          keyConvertor: fastjson
      remote:
        default:
          type: redis
          keyConvertor: fastjson
          valueEncoder: java
          valueDecoder: java
          poolConfig:
            minIdle: 5
            maxIdle: 20
            maxTotal: 50
          host: 127.0.0.1
          port: 6379
          password: 密码


    启动类注解

    @EnableMethodCache(basePackages = "com.xkcoding")/** 开启 Cache注解 **/
    @EnableCreateCacheAnnotation/** 启用createCache注解 **/


    JetCacheController.java测试类

    import com.alicp.jetcache.anno.*;
    import com.xkcoding.cache.redis.entity.User;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.concurrent.TimeUnit;
    
    /**
     * 阿里开源的缓存框架JetCache
     *
     * @author sun
     * @date 2021/10/25 20:39
     */
    @RestController
    @RequestMapping("jetcache")
    @Slf4j
    public class JetCacheController {
    
        @RequestMapping("index")
        public String index() {
            return "OK";
        }
    
        /**
         * jetcache 放入缓存
         *
         * http://localhost:8080/jetcache/add?id=1&name=%E5%AD%99
         *
         * @author sun
         * @date 2021/10/25 21:27
         * @param user
         * @return com.xkcoding.cache.redis.entity.User
         */
        @RequestMapping("/add")
        @Cached(name = "jetcache.demo", key = "#user.id", expire = 1, timeUnit = TimeUnit.DAYS, cacheType = CacheType.REMOTE)
        @CacheRefresh(refresh = 10, timeUnit = TimeUnit.SECONDS, stopRefreshAfterLastAccess = 31)
        public User add(User user) {
            log.info("保存用户信息");
            return user;
        }
    
        /**
         * jetcache 获取缓存中的数据
         *
         * http://localhost:8080/jetcache/get?id=1
         *
         * @author sun
         * @date 2021/10/25 21:27
         * @param id
         * @return com.xkcoding.cache.redis.entity.User
         */
        @RequestMapping("/get")
        @Cached(name = "jetcache.demo", key = "#id")
        public User get(Long id) {
            log.info("缓存中获取用户信息");
            return null;
        }
    
        /**
         * jetcache 更新缓存
         *
         * http://localhost:8080/jetcache/update?id=1&name=%E5%AD%99%E5%9B%BD%E8%89%AF
         *
         * @author sun
         * @date 2021/10/25 21:28
         * @param user
         * @return void
         */
        @RequestMapping("/update")
        @CacheUpdate(name = "jetcache.demo", key = "#user.id", value = "#user")
        public void update(User user) {
            log.info("缓存中更新用户信息");
        }
    
        /**
         * jetcache 删除缓存
         *
         * http://localhost:8080/jetcache/delete?id=1
         *
         * @author sun
         * @date 2021/10/25 21:28
         * @param id
         * @return void
         */
        @RequestMapping("/delete")
        @CacheInvalidate(name = "jetcache.demo", key = "#id")
        public void delete(Long id) {
            log.info("缓存中删除用户信息");
        }
    
    }


    语法很简单,自己研究一下即可。

2 +1

版权声明

 Java  源码  开源  阿里巴巴

 请文明留言

0 条评论