定时缓存到期如何工作? [英] How does timed cache expiry work?

查看:126
本文介绍了定时缓存到期如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道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?

谢谢,
雅虎

推荐答案

来自

使用CacheBuilder构建的缓存不执行 not 执行清除和逐出值 自动",或值过期后立即进行,或以下任何一种 排序.相反,它会在执行期间进行少量维护 写操作,或者在偶尔的读操作中(如果写) 稀有.

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.

其原因如下:如果我们要执行Cache 持续维护,我们需要创建一个线程, 操作将与用户操作争夺共享锁. 此外,某些环境会限制线程的创建, 这会使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.

因此,如果您只是在进行读取操作,则在交易前后都执行Cache.cleanUp()可能会很好,但是仍然不能保证.

As such, if you are only doing reads you "might" be good if you do a Cache.cleanUp() just before and after your transaction but there is still no guarantee.

但是,与其尝试迫使项目保留在缓存中,不如使用

However, instead of trying to force items to stay in the cache you might instead simply evict items to another cache/map using a 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.

以下是一个过于简化的示例:

The following is an oversimplified example:

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对象(如果同时运行长时间运行的事务)或在具有不同线程的线程之间使用公共缓存驱逐策略等,但希望这能充分说明您的想法,以帮助您入门.

Your actual code would need to conditionally 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.

这篇关于定时缓存到期如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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