1

I'm using a Guava Cache Loader and picking up the time to expire from a config file.

I'm wondering if there is a way to programmatically reset the time to expire value for a given cache. In my case this is desirable in the action of a listener that picks up the change to the configured value.

Manolo
  • 1,597
  • 4
  • 21
  • 35

1 Answers1

1

No, there's no way to change the expiration time for a whole cache (short of constructing a new cache and copying the contents of the old cache over). If you're asking about resetting a single entry in the cache that can be done simply by re-putting the value:

public static void <K, V> resetExpiration(Cache<K, V> cache, K key) {
  V value = cache.getIfPresent(key);
  if (value != null) {
    cache.put(key, value); // basically a no-op, but key's expiration is reset
  }
}

Or for an expire-on-access cache just call .get() or .getIfPresent() and that will reset the access expriration.

dimo414
  • 47,227
  • 18
  • 148
  • 244