简体   繁体   English

Guava 缓存:如何在“CacheLoader.load()”而不是在 CacheBuilder.newBuilder() 期间设置过期时间?

[英]Guava cache: how to set expiration on 'CacheLoader.load()' instead of during CacheBuilder.newBuilder()?

I am trying to create a cache using guava cache library.我正在尝试使用番石榴缓存库创建缓存。 One my main requirement is that I want to set the cache expiry after the CacheLoader.load(..) function instead of something most of the examples I encountered on the web, like the one below.我的主要要求之一是我想在 CacheLoader.load(..) 函数之后设置缓存到期,而不是我在网上遇到的大多数示例,如下所示。

LoadingCache<String, MyClass> myCache = 
CacheBuilder.newBuilder().maximumSize(MAX_SIZE).expireAfterWrite(10, TimeUnit.Minutes).build(cacheLoader);

The reason for this is that the object retrieved from the database by the CacheLoader.load(...) function contains the expiration data.这样做的原因是 CacheLoader.load(...) 函数从数据库中检索的对象包含过期数据。 So I want to use this information instead of some "random" static value.所以我想使用这个信息而不是一些“随机”静态值。

I want something like this.我想要这样的东西。

LoadingCache<String, MyClass> myCache = 
CacheBuilder.newBuilder().maximumSize(MAX_SIZE).build(cacheLoader);

... ...

CacheLoader meCacheLoder = new CacheLoader<String MyClass>(){
   @Override
   public MyClass load(String key) throws Exception {
      // Retrieve the MyClass object from database using 'key'
      MyClass myObj = getMyObjectFromDb(key);  
      int expiry = myObj.getExpiry();

         // Now somehow set this 'expiry' value with the cache
         ????

      return myObj;
   }
 };

OR Is there any better option available than Guava cache for this purpose?或者为此目的,是否有比 Guava 缓存更好的选择?

There is no such feature in Guava, as Louis already pointed out.正如路易斯已经指出的那样,番石榴中没有这样的功能。

For example you can use EHCache or cache2k .例如,您可以使用 EHCache 或cache2k For cache2k I can give you quick directions since this is a core feature we use regularly:对于 cache2k,我可以为您提供快速指导,因为这是我们经常使用的核心功能:

You can either implement the interface ValueWithExpiryTime on your value object, which is:您可以在值对象上实现接口ValueWithExpiryTime ,即:

interface ValueWithExpiryTime {

  long getCacheExpiryTime();

}

Or, you can register a EntryExpiryCalculator to extract the time value.或者,您可以注册一个EntryExpiryCalculator来提取时间值。 The cache is build as follows:缓存构建如下:

Cache<Key, Value> cache = 
  CacheBuilder.newCache(Key.class, Value.class)
    .expiryCalculator(new EntryExpiryCalculator<Key, Value>() {
        @Override
        public long calculateExpiryTime(
            final Key key, final Value value, 
            final long loadTime, final CacheEntry<Key, Value> oldEntry) {
          return value.getExpiry();
        }
      }
    )
    .build();

The time is the standard long type represented in milliseconds since the epoch.时间是自纪元以来以毫秒表示的标准 long 类型。 By default the expiry will happen not exactly at the specified time, but zero or a few milliseconds later, depending on your machine load.默认情况下,到期不会完全在指定时间发生,而是在零或几毫秒后发生,具体取决于您的机器负载。 This is the most effective mode.这是最有效的模式。 If this is a problem, add sharpExpiry(true) .如果这是一个问题,请添加sharpExpiry(true)

Disclaimer: I am the author of cache2k....免责声明:我是 cache2k 的作者....

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

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