简体   繁体   English

在一定时间内将项目添加到缓存中,SCALA

[英]Add items to cache within a certain amount of time, SCALA

Let's say I have value1 (that was created 1min ago), value2 (that was created 5mins ago), and value3 (created 15mins ago).假设我有 value1(1 分钟前创建)、value2(5 分钟前创建)和 value3(15 分钟前创建)。

I want to add to cache the values for the last 10 minutes.我想添加缓存过去 10 分钟的值。 How to do that in scala?如何在 Scala 中做到这一点? I'm only seeing maximum size but not maximum time in Google Guava CacheLoader.我在 Google Guava CacheLoader 中只看到最大大小,但没有看到最大时间。

In other words, I want to get value1 and value2 since they pass the criteria "Last 10 minutes" and evict value3 as it was created 15mins ago.换句话说,我想获得 value1 和 value2,因为它们通过了标准“最后 10 分钟”并驱逐了 15 分钟前创建的 value3。

Guava caches support timed eviction . Guava 缓存支持定时驱逐

For your usage scenario, you could use a CacheBuilder to create a cache that expires entries after 10 minutes using the expireAfterWrite(long duration, TimeUnit unit) method.对于您的使用场景,您可以使用CacheBuilder创建一个缓存,使用expireAfterWrite(long duration, TimeUnit unit)方法在 10 分钟后使条目过期。 (Use 10 for the duration and TimeUnit.MINUTES for the unit parameters.) duration使用10unit参数使用TimeUnit.MINUTES 。)

The javadoc clearly states that javadoc 明确指出

[e]xpired entries [...] will never be visible to read or write operations [e] 过期条目 [...] 将永远不会对读取或写入操作可见

so this should completely satisfy your use case.所以这应该完全满足您的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM