com.alicp.jetcache 方法缓存

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 ;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值