定时缓存到期如何工作? [英] How does timed cache expiry work?
问题描述
我知道Guava Cache允许使用到期时间来配置各个缓存.番石榴是否使用计时器来完成此操作,该计时器会在配置的秒数后唤醒以使缓存无效?
I know that Guava Cache allows individual caches to be configured with an expiry time. Does Guava do this using a timer that wakes up after a configured number of seconds to invalidate the cache?
我有一个长期运行的交易.无论事务开始时缓存中的内容是什么,我希望它一直持续到事务结束.因此,即使在事务处理期间缓存的有效性秒数过期,从缓存访问的值也应保持不变,直到我们到达事务处理结束为止.番石榴有可能吗?
I have a transaction that is long-running. Whatever is in the cache at the start of the transaction, I would like it to continue till the end of the transaction. So even if the number of seconds of validity of a cache gets expired during the transaction, the values accessed from the cache should remain intact till we reach the end of transaction. Is this possible in Guava?
谢谢,
雅虎
推荐答案
来自
使用 Caches built with 其原因如下:如果我们要执行 The reason for this is as follows: if we wanted to perform 相反,我们将选择权交给您.如果您的缓存是
高吞吐量,那么您不必担心执行缓存
维护以清理过期的条目等.如果您的缓存
确实很少写入,并且您不希望清理来阻止缓存
阅读,您可能希望创建自己的维护线程来调用
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
如果您要为某个高速缓存安排定期的高速缓存维护,
很少有写入,只需使用以下命令安排维护
If you want to schedule regular cache maintenance for a cache which
only rarely has writes, just schedule the maintenance using
因此,如果您只是在进行读取操作,则在交易前后都执行 As such, if you are only doing reads you "might" be good if you do a However, instead of trying to force items to stay in the cache you might instead simply evict items to another cache/map using a 以下是一个过于简化的示例: The following is an oversimplified example: 您的实际代码将需要有条件地 Your actual code would need to conditionally 这篇关于定时缓存到期如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!CacheBuilder
构建的缓存不执行 not 执行清除和逐出值
自动",或值过期后立即进行,或以下任何一种
排序.相反,它会在执行期间进行少量维护
写操作,或者在偶尔的读操作中(如果写)
稀有.
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.Cache
持续维护,我们需要创建一个线程,
操作将与用户操作争夺共享锁.
此外,某些环境会限制线程的创建,
这会使CacheBuilder
在该环境中不可用.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()
.Cache.cleanUp()
at regular intervals.ScheduledExecutorService
.ScheduledExecutorService
.Cache.cleanUp()
可能会很好,但是仍然不能保证.Cache.cleanUp()
just before and after your transaction but there is still no guarantee.removalListener
and then when you read you will first need to check the cache and then, if it wasn't there, check the items evicted during the long-running transaction.Map<Integer, String> evicted = new ConcurrentHashMap<>();
Cache<Integer, String> cache = CacheBuilder.newBuilder()
.expireAfterAccess(2, SECONDS)
.removalListener((RemovalListener<Integer, String>) notification ->
evicted.put(notification.getKey(), notification.getValue()))
.build();
assert evicted.size() == 0 && cache.size() == 0;
cache.put(0, "a");
cache.put(1, "b");
cache.put(2, "c");
assert evicted.size() == 0 && cache.size() == 3;
sleepUninterruptibly(1, SECONDS);
assert evicted.size() == 0 && cache.size() == 3;
cache.put(3, "d");
assert evicted.size() == 0 && cache.size() == 4;
sleepUninterruptibly(1, SECONDS);
cache.cleanUp();
assert evicted.size() == 3 && cache.size() == 1;
Integer key = 2;
String value;
{
value = cache.getIfPresent(key);
if (value == null) value = evicted.get(key);
}
assert Objects.equals(value, "c");
put
进入evicted
,清理evicted
,管理多个evicted
对象(如果同时运行长时间运行的事务)或在具有不同线程的线程之间使用公共缓存驱逐策略等,但希望这能充分说明您的想法,以帮助您入门.put
into evicted
, clean-up evicted
, manage multiple evicted
objects if your running long-running transactions concurrently or use a common cache between the threads with a different eviction strategy, etc. but hopefully this demonstrates the idea sufficiently to get you started.