谷歌番石榴缓存invalidateAll()和cleanUp()之间的区别 [英] java - google guava cache difference between invalidateAll() and cleanUp()

查看:1016
本文介绍了谷歌番石榴缓存invalidateAll()和cleanUp()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个Cache,其定义如下:

private static Cache<String, Long> alertsUIDCache = CacheBuilder.newBuilder().
           expireAfterAccess(60).build();

从我读到的内容(如果我写错了,请纠正我):

如果在0:00时将值写入Cache,则应在60秒后将其移至准备退出"状态.从Cache中实际删除值的操作将在下一个缓存修改(缓存修改究竟是什么?)时发生. 是不是?

此外,我不确定invalidateAll()cleanUp()方法之间的区别是什么,有人可以提供解释吗?

此链接的

解决方案

第一部分: Guava CacheLoader-无效不会如果同时设置了expireAfterWrite和expireAfterAccess,则立即使条目无效

invalidate应该立即删除该条目-不等待其他查询-并应在下一个查询中强制将该值重新加载到该键.

cleanUp:执行高速缓存所需的所有暂挂维护操作.确切地执行哪些活动(如果有)取决于实现.

来自 guava文档: https://github.com/google /guava/wiki/CachesExplained

明确删除

您可以随时显式地使高速缓存条目无效,而不是等待将其逐出.可以这样做:

individually, using Cache.invalidate(key)
in bulk, using Cache.invalidateAll(keys)
to all entries, using Cache.invalidateAll()

何时进行清理?

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

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

相反,我们将选择权交给您.如果您的缓存是高吞吐量的,那么您不必担心执行缓存维护以清理过期的条目等.如果您的缓存确实很少写入,并且您不想清理来阻止缓存读取,那么您可能希望创建自己的维护线程,该线程定期调用Cache.cleanUp().

如果您要为很少有写操作的缓存安排定期的缓存维护,只需使用ScheduledExecutorService安排维护.

Say I have a Cache that is defined like this:

private static Cache<String, Long> alertsUIDCache = CacheBuilder.newBuilder().
           expireAfterAccess(60).build();

From what I read (please correct me if I am wrong):

If value is written to Cache at 0:00, it should be moved to "ready to be evicted" status after 60 seconds. The actual removing of the value from the Cache will happen at the next cache modification (what is exactly is cache modification ?). is that right?

Also, I am not sure what the difference between the invalidateAll() and the cleanUp() methods, can someone provide an explanation?

解决方案

first part from this link : How does Guava expire entries in its CacheBuilder?

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.

second part : from this link : Guava CacheLoader - invalidate does not immediately invalidate entry if both expireAfterWrite and expireAfterAccess are set

invalidate should remove the entry immediately -- not waiting for another query -- and should force the value to get reloaded on the very next query to that key.

cleanUp: Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.

from guava documents: https://github.com/google/guava/wiki/CachesExplained

Explicit Removals

At any time, you may explicitly invalidate cache entries rather than waiting for entries to be evicted. This can be done:

individually, using Cache.invalidate(key)
in bulk, using Cache.invalidateAll(keys)
to all entries, using Cache.invalidateAll()

When Does Cleanup Happen?

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.

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.

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.

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

这篇关于谷歌番石榴缓存invalidateAll()和cleanUp()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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