Caffeine缓存框架入门学习

打印 上一主题 下一主题

主题 942|帖子 942|积分 2826

引入依赖
  1. <dependency>
  2.     <groupId>com.github.ben-manes.caffeine</groupId>
  3.     <artifactId>caffeine</artifactId>
  4.     <version>2.5.5</version>
  5. </dependency>
复制代码
基础创建方式
  1. Cache<String, Integer> cache = Caffeine.newBuilder().build();
复制代码
常用创建模板
  1. private static final Cache<String, String> CONFIG_VALUE =
  2.             Caffeine.newBuilder()
  3.                     //初始容量
  4.                     .initialCapacity(100)
  5.                     //最大容量
  6.                     .maximumSize(1000)
  7.                     //过期时间
  8.                     .expireAfterWrite(10, TimeUnit.MINUTES)
  9.                     //缓存元素删除监听器,参数分别为缓存的key,value,驱逐原因
  10.                     .removalListener((Key key, Object value, RemovalCause cause) ->
  11.                             System.out.printf("Key %s was removed (%s)%n", key, cause))
  12.                     .build();
复制代码
填充策略

填充策略分为手动填充,自动填充,异步手动填充和异步自动填充四种


  • 手动填充
  1. Cache<String, Integer> cache = Caffeine.newBuilder().build();// 查找一个缓存元素, 没有查找到的时候返回nullMyObject graph = cache.getIfPresent(key);// 查找缓存,如果缓存不存在则生成缓存元素,  如果无法生成则返回nullgraph = cache.get(key, k -> createObject(key));// 添加或者更新一个缓存元素cache.put(key, graph);// 移除一个缓存元素cache.invalidate(key);
复制代码

  • 自动填充
  1. LoadingCache<Key, Graph> cache = Caffeine.newBuilder()
  2.     //自动填充即在初始化的时候就指定自动填充函数,当缓存中没有命中数据的时候自动执行填充函数
  3.     .build(key -> createExpensiveGraph(key));
  4. // 查找缓存,如果缓存不存在则生成缓存元素,  如果无法生成则返回null
  5. Graph graph = cache.get(key);
  6. // 批量查找缓存,如果缓存不存在则生成缓存元素
  7. Map<Key, Graph> graphs = cache.getAll(keys);
复制代码

  • 异步手动填充
  1. AsyncCache<Key, Graph> cache = Caffeine.newBuilder()
  2.     .buildAsync();
  3. // 查找缓存元素,如果不存在,则异步生成
  4. CompletableFuture<Graph> graph = cache.get(key, k -> createExpensiveGraph(key));
复制代码

  • 异步自动填充
  1. AsyncLoadingCache<Key, Graph> cache = Caffeine.newBuilder()
  2.     // 你可以选择: 去异步的封装一段同步操作来生成缓存元素
  3.     .buildAsync(key -> createExpensiveGraph(key));
  4.     // 你也可以选择: 构建一个异步缓存元素操作并返回一个future
  5.     .buildAsync((key, executor) -> createExpensiveGraphAsync(key, executor));
  6. // 查找缓存元素,如果其不存在,将会异步进行生成
  7. CompletableFuture<Graph> graph = cache.get(key);
  8. // 批量查找缓存元素,如果其不存在,将会异步进行生成
  9. CompletableFuture<Map<Key, Graph>> graphs = cache.getAll(keys);
复制代码
驱逐策略

驱逐策略有三种:基于大小驱逐,基于时间驱逐,基于引用驱逐


  • 基于大小分为基于缓存大小,和基于权重大小两种
  1. // 根据缓存的计数进行驱逐
  2. LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
  3.     .maximumSize(10_000)
  4.     .build(key -> createExpensiveGraph(key));
  5. // 根据缓存的权重来进行驱逐(权重只是用于确定缓存大小,不会用于决定该缓存是否被驱逐)
  6. LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
  7.     .maximumWeight(10_000)
  8.     .weigher((Key key, Graph graph) -> graph.vertices().size())
  9.     .build(key -> createExpensiveGraph(key));
复制代码

  • 基于时间驱逐
  1. LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
  2.     //当指定时间内没有被读写则被清除
  3.     .expireAfterAccess(5, TimeUnit.MINUTES)
  4.     .build(key -> createExpensiveGraph(key));
  5. LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
  6.     //最后一次写入固定时间后被清除
  7.     .expireAfterWrite(10, TimeUnit.MINUTES)
  8.     .build(key -> createExpensiveGraph(key));
  9. //还可以使用exoureAfter()来自定义到期驱逐策略,详见文章末尾参考链接
复制代码

  • 基于引用驱逐
  1. LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
  2.     //弱引用键值
  3.     .weakKeys()
  4.     .weakValues()
  5.     .build(key -> createExpensiveGraph(key));
  6. // 当垃圾收集器需要释放内存时驱逐
  7. LoadingCache<Key, Graph> graphs = Caffeine.newBuilder()
  8.     //软引用值
  9.     .softValues()
  10.     .build(key -> createExpensiveGraph(key));
复制代码
注意:AsyncLoadingCache不支持弱引用和软引用。
以上仅为基础部分内容,详细学习参考文末链接。
参考:
https://blog.csdn.net/crazymakercircle/article/details/113751575
https://zhuanlan.zhihu.com/p/329684099

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

自由的羽毛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表