使用 Guava 进行缓存

发布于 2024-10-12 17:40:41 字数 352 浏览 4 评论 0原文

哪些 Guava 类适合线程安全缓存?我使用组合键,它是动态构建的,所以 softKeys() 没有意义,对吧?我在某个地方看到了 ConcurentLinkedHashMap,这是要走的路吗?它已经在最近的版本中了吗?抱歉,您的提问方式很混乱……

更新

这个问题已经很老了,浏览他的答案可能会浪费时间。长期以来,有一个 CacheBuilder 这是要走的路。

What Guava classes are suitable for thread-safe caching? I use a composed key, which gets constructed on the fly, so softKeys() makes no sense, right? I saw somewhere ConcurentLinkedHashMap, is it the way to go? Is it already in the recent release? Sorry for the chaotic way of asking...

Update

This question is pretty old and looking through he answers could possible be a waste of time. Since long there's a CacheBuilder which is the way to go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧瑾黎汐 2024-10-19 17:40:41

新的 Guava 库 10.0 版引入了 Cache 接口,专为缓存而设计。

它带有 CacheBuilder,类似于 MapMaker 以及 MapMaker 的所有缓存方法将在版本 11 中删除。

文档中的示例:

Cache<Key, Graph> graphs = CacheBuilder.newBuilder()
   .concurrencyLevel(4)
   .weakKeys()
   .maximumSize(10000)
   .expireAfterWrite(10, TimeUnit.MINUTES)
   .build(
       new CacheLoader<Key, Graph>() {
         public Graph load(Key key) throws AnyException {
           return createExpensiveGraph(key);
         }
       });

The new Guava library with version 10.0 introduces the Cache interface which is designed especially for caching.

It comes with CacheBuilder, which is similar to MapMaker and all caching methods of MapMaker will be removed in the release 11.

Example from the documentation:

Cache<Key, Graph> graphs = CacheBuilder.newBuilder()
   .concurrencyLevel(4)
   .weakKeys()
   .maximumSize(10000)
   .expireAfterWrite(10, TimeUnit.MINUTES)
   .build(
       new CacheLoader<Key, Graph>() {
         public Graph load(Key key) throws AnyException {
           return createExpensiveGraph(key);
         }
       });
于我来说 2024-10-19 17:40:41

听起来您想要 MapMaker.makeComputingMap,但您提到了 softKeys 所以我假设您已经熟悉该类。

您对 softKeys 的看法是正确的 - 如果您即时组合键,它将不起作用,因为 softKeys 会导致地图使用 ==而不是 equals 进行键比较。但是,只要重新创建被逐出的条目不会产生副作用,您就应该可以接受 softValuesexpiration

Sounds like you want MapMaker.makeComputingMap, but you mention softKeys so I assume you are already familiar with that class.

You are right about softKeys - it will not work if you compose keys on-the-fly, because softKeys causes the map to use == instead of equals for key comparison. But you should be fine with softValues and expiration, as long as there is no side-effect from recreating an evicted entry.

長街聽風 2024-10-19 17:40:41

MapMaker.maximumSize()ConcurrentLinkedHashMap 的长期替代品。如果社区达成共识,CLHM 仍然是改进算法的测试平台,以便以后移植。不过,我预计 v2.0 将是将这些改进移植到 MapMaker 后的最后一个版本。该项目将根据需要保持活力,因为它拥有良好的用户基础(例如 Apache Cassandra)。我很高兴 Guava 包含了它。

MapMaker.maximumSize() is the long-term replacement for ConcurrentLinkedHashMap. CLHM remains the test-bed for improved algorithms for later porting if there is community consensus. I expect v2.0 will be the last release after porting those improvements to MapMaker, though. The project will remain alive as needed since it has a good user base (e.g. Apache Cassandra). I'm quite happy that Guava subsumed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文