com.alicp.jetcache 方法缓存

  • Post author:
  • Post category:其他


1.POM 文件加入依赖

<dependency>w
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alicp.jetcache/jetcache-starter-redis -->
<dependency>
   <groupId>com.alicp.jetcache</groupId>
   <artifactId>jetcache-starter-redis</artifactId>
   <version>2.6.7</version>
</dependency>
<!-- 加入依赖 Redis end -->

2.YML文件配置redis配置

jetcache:
    statIntervalMinutes: 15 # 统计间隔,默认0:表示不统计
    areaInCacheName: false  # areaName是否作为缓存key前缀,默认True
    local:
        default: # 默认default,可以配置更多的area
            type: linkedhashmap # 已支持可选:linkedhashmap、caffeine
            keyConvertor: fastjson # key转换器
    remote:
        default:
            type: redis
            keyConvertor: fastjson
            valueEncoder: java # 序列化器,只有remote需要
            valueDecoder: java # 序列化器,只有remote需要
            poolConfig:
                minIdle: 8
                maxIdle: 20
                maxTotal: 50
            host: 127.0.0.0
            port: 6379
            password: xxxx
            database: 6
            timeout: 100000

3.启动类增加启动缓存注解

//启动缓存  指定启动缓存包路径
@EnableMethodCache(basePackages = "com.xx.xxx")
@EnableCreateCacheAnnotation
public class XxxxApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(XxxxApplication.class, args);
       
    }

4.创建controller 和Service

再对应 Service方法上加缓存直接

xxx.metheod.key  方法缓存唯一key 
cacheType  缓存类型 一般选择 REMOTE 
expire  缓存失效时间
refresh  接口首次调用后间隔刷新时间
stopRefreshAfterLastAccess  接口停止访问后,间隔停止刷新时间
@Cached(name="xxx.me", key="#userId", expire = 600, cacheType = CacheType.REMOTE)
@CacheRefresh(refresh = 60, stopRefreshAfterLastAccess = 120, timeUnit = TimeUnit.SECONDS)
@CachePenetrationProtect
public Map<String, List<BaseCode>> redisMethodCache(String userId){


    Map<String, List<BaseCode>> cMap = new HashMap<>();
    log.info(" redisMethodCache ====> 开始查询DDB...  ");
    List<BaseCode> baseCodeList = baseCodeMapper.selectAll().subList(1,10);
    return baseCodeList ;

}



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