SpringBoot+Caffeine 本地缓存

maven配置

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

yml配置

spring:
  cache:
    type: caffeine
    cache-names:
      - interfaceCache
    caffeine:
      spec: initialCapacity=100,maximumSize=2000,expireAfterWrite=1h
maximumSize
配置缓存的最大容量,当快要达到容量上限的时候,缓存管理器会根据一定的策略将部分缓存项移除。
expireAfterAccess
配置缓存项的过期机制,如代码所示当缓存项被访问后一小时将会过期,从而被移除。   (秒级配置:10s)   

代码层

1、启动类Application添加@EnableCaching

@EnableCaching
@SpringBootApplication
public class Application {}

2、无参实现

    @Cacheable(value = "interfaceCache", key = Constants.PREFIX_OF_CACHE_KEY1, unless = "#result == null || #result.size()==0")
    public String getA() {}

3、有参实现

    @Cacheable(value = "interfaceCache", key = "T(com.a.utils.Constants).PREFIX_OF_CACHE_KEY2.concat('-').concat(#p1).concat('-').concat(#p2)", unless = "#result == null || #result.size()==0")
    public String  getA(String p1, Integer p2) {}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值