番石榴缓存'expireAfterWrite'似乎并不总是有效 [英] Guava cache 'expireAfterWrite' does not seem to always work

查看:451
本文介绍了番石榴缓存'expireAfterWrite'似乎并不总是有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private Cache<Long, Response> responseCache = CacheBuilder.newBuilder()
            .maximumSize(10000)
            .expireAfterWrite(10, TimeUnit.MINUTES)
            .build();

我期望10分钟内未发送给客户端的响应对象已过期并从缓存中删除自动,但我注意到即使在10、15、20分钟之后,Response对象也不总是过期。当大量填充高速缓存时,它们的确会过期,但是当系统变为空闲状态时(例如最近的500个响应对象),它将停止删除这些对象。
有人可以帮助您了解此行为吗?谢谢

I am expecting that response objects that are not send to client within 10 minutes are expired and removed from cache automatically but I notice that Response objects are not always getting expired even after 10, 15, 20 minutes. They do get expire when cache is being populated in large numbers but when the system turn idle, something like last 500 response objects, it stops removing these objects. Can someone help to understand this behavior? Thank you

推荐答案

这在文档中指定:


如果请求expireAfterWrite或expireAfterAccess,则可能在每次修改缓存,偶尔访问缓存或调用Cache.cleanUp()时逐出条目。过期的条目可能会被Cache.size()计数,但对读取或写入操作将永远不可见。

If expireAfterWrite or expireAfterAccess is requested entries may be evicted on each cache modification, on occasional cache accesses, or on calls to Cache.cleanUp(). Expired entries may be counted by Cache.size(), but will never be visible to read or write operations.

还有更多详细信息在Wiki上:

And there's more detail on the wiki:


使用CacheBuilder构建的缓存不会自动执行清理和逐出值,或者在值过期后立即执行逐出,或执行任何操作那种。取而代之的是,它在写操作期间或偶尔进行的读操作(如果很少发生写操作)中进行少量维护。

Caches built with CacheBuilder do not perform cleanup and evict values "automatically," or instantly after a value expires, or anything of the sort. Instead, it performs small amounts of maintenance during write operations, or during occasional read operations if writes are rare.

其原因如下:如果我们要执行连续缓存
维护,我们需要创建一个线程,其
操作将与用户操作争夺共享锁。
此外,某些环境限制了线程的创建,
会使CacheBuilder在该环境中无法使用。

The reason for this is as follows: if we wanted to perform Cache maintenance continuously, we would need to create a thread, and its operations would be competing with user operations for shared locks. Additionally, some environments restrict the creation of threads, which would make CacheBuilder unusable in that environment.

相反,我们将选择放在您的计算机中手。如果您的缓存是
高吞吐量,那么您不必担心需要进行缓存
维护来清理过期的条目等。如果您的高速缓存
确实很少写入,并且您不希望清理阻止高速缓存
的读取,则您可能希望创建自己的维护线程,该线程会定期调用
Cache.cleanUp()

Instead, we put the choice in your hands. If your cache is high-throughput, then you don't have to worry about performing cache maintenance to clean up expired entries and the like. If your cache does writes only rarely and you don't want cleanup to block cache reads, you may wish to create your own maintenance thread that calls Cache.cleanUp() at regular intervals.

如果要为
很少写入的缓存安排定期的缓存维护,只需使用
ScheduledExecutorService安排维护。

If you want to schedule regular cache maintenance for a cache which only rarely has writes, just schedule the maintenance using ScheduledExecutorService.

这篇关于番石榴缓存'expireAfterWrite'似乎并不总是有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆