本文介绍了Guava如何在其CacheBuilder中过期条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个CacheBuilder,如这里所推荐的:





但是,当

Guava如何做到这一点,以及什么性能成本呢?

解决方案

番石榴队员在这里。



Guava 缓存在正常的维护操作过程中,这些维护操作在高速缓存写入操作期间以每个段为基础发生,并且偶尔在高速缓存读取操作期间发生。条目通常不会在完成他们的到期时间过期,只是因为 Cache 使得故意决定不创建自己的维护线程,而是让用户决定是否需要持续维护。



我将专注于 expireAfterAccess ,但程序对于 expireAfterWrite 几乎相同。在力学方面,当您在 CacheBuilder 中指定 expireAfterAccess 时,缓存的每个段都维护一个链表从最近访问到最近访问的顺序访问队列。缓存条目实际上是链接列表中的节点,因此当访问条目时,它将自己从其在访问队列中的旧位置移除,并将其自身移动到队列的末尾。



当执行缓存维护时,所有缓存都要使队列前端的每个条目过期,直到发现未过期的条目为止。这是直接的,并且需要相对较少的开销,并且发生在正常的高速缓存维护过程中。 (另外,缓存有意限制了在单次清理中完成的工作量,从而最大限度地减少了任何单个缓存操​​作的费用。)通常,高速缓存维护的成本由计算缓存中实际条目的费用支配。 p>

I want to use a CacheBuilder, as recommended here:

Java time-based map with expiring keys

However I don't understand when Guava knows to expire entries.

How does Guava do it and what performance cost does it incurr?

解决方案

Guava team member here.

The Guava Cache implementation expires entries in the course of normal maintenance operations, which occur on a per-segment basis during cache write operations and occasionally during cache read operations. Entries usually aren't expired at exactly their expiration time, just because Cache makes the deliberate decision not to create its own maintenance thread, but rather to let the user decide whether continuous maintenance is required.

I'm going to focus on expireAfterAccess, but the procedure for expireAfterWrite is almost identical. In terms of the mechanics, when you specify expireAfterAccess in the CacheBuilder, then each segment of the cache maintains a linked list access queue for entries in order from least-recent-access to most-recent-access. The cache entries are actually themselves nodes in the linked list, so when an entry is accessed, it removes itself from its old position in the access queue, and moves itself to the end of the queue.

When cache maintenance is performed, all the cache has to do is to expire every entry at the front of the queue until it finds an unexpired entry. This is straightforward and requires relatively little overhead, and it occurs in the course of normal cache maintenance. (Additionally, the cache deliberately limits the amount of work done in a single cleanup, minimizing the expense to any single cache operation.) Typically, the cost of cache maintenance is dominated by the expense of computing the actual entries in the cache.

这篇关于Guava如何在其CacheBuilder中过期条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 20:48