实现 Expiry 接口
1 | public class CaffeineExpiry implements Expiry<String, Object> { |
注意:如果使用常规的 put(key, value) 方法,则需要 expireAfterCreate 方法返回具体的值;下文使用 put(key, value, duration, timeUnit) 方法,expireAfterCreate 方法可以范围任意值,不影响缓存过期时间。
配置
1 | @Configuration |
使用
1 | @Component |
参考
Eviction#time-based
解读JVM级别本地缓存Caffeine青出于蓝的要诀3 —— 讲透Caffeine的数据驱逐淘汰机制与用法
Spring 缓存动态设置过期时间 - Caffeine