使用 Guava 进行缓存
哪些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
新的 Guava 库 10.0 版引入了
Cache
接口,专为缓存而设计。它带有
CacheBuilder
,类似于MapMaker
以及MapMaker
的所有缓存方法将在版本 11 中删除。文档中的示例:
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 toMapMaker
and all caching methods ofMapMaker
will be removed in the release 11.Example from the documentation:
听起来您想要
MapMaker.makeComputingMap
,但您提到了softKeys
所以我假设您已经熟悉该类。您对
softKeys
的看法是正确的 - 如果您即时组合键,它将不起作用,因为softKeys
会导致地图使用==
而不是equals
进行键比较。但是,只要重新创建被逐出的条目不会产生副作用,您就应该可以接受softValues
和expiration
。Sounds like you want
MapMaker.makeComputingMap
, but you mentionsoftKeys
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, becausesoftKeys
causes the map to use==
instead ofequals
for key comparison. But you should be fine withsoftValues
andexpiration
, as long as there is no side-effect from recreating an evicted entry.MapMaker.maximumSize()
是ConcurrentLinkedHashMap
的长期替代品。如果社区达成共识,CLHM 仍然是改进算法的测试平台,以便以后移植。不过,我预计 v2.0 将是将这些改进移植到 MapMaker 后的最后一个版本。该项目将根据需要保持活力,因为它拥有良好的用户基础(例如 Apache Cassandra)。我很高兴 Guava 包含了它。MapMaker.maximumSize()
is the long-term replacement forConcurrentLinkedHashMap
. 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 toMapMaker
, 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.