1

Is it possible to auto evict the record with value Optional.absent()? In some applications the Optional.absent() may not be the value for some keys. For example, if an application contains http calls where the key can be some string and the value is response returned from the http call, the http call can return some invalid values (for example null) because of network issues or authentication failures, then the invalid can be saved as Optional.absent() with the key in cache. At a later point, if the network and authentication problems are fixed, the key->invalidValue still remains in the cache. What is the best to fix this problem?

user2716189
  • 63
  • 1
  • 6
  • Is it that you don't want absent values to be cached at all? That at some specific point you want to remove all absent values? – Louis Wasserman Feb 17 '16 at 21:26
  • Seems to me that "absent" values are getting into the cache by mistake. If some call results in an exception, you should think twice about caching the value. So my question to you is: Why cache those problematic results at all? – Brian Feb 17 '16 at 21:34
  • AS @Brian stated, why would you cache those wrong results instead of propagating the error and properly handling ? – Louis F. Feb 17 '16 at 21:38
  • I was thinking to throw an InvalidCacheLoadException in LoadCache.load if the value is invalid and handle the exception when it gets the value from the cache, is this a good fix? Is there any other solutions rather than using exception? – user2716189 Feb 17 '16 at 22:07

1 Answers1

0

For example, if an application contains http calls where the key can be some string and the value is response returned from the http call, the http call can return some invalid values (for example null) because of network issues or authentication failures

If possible, I would change this behavior to throw an exception when the request fails or the response is invalid - that's what exceptions are for. See Effective Java: Item 57 for more.

then the invalid can be saved as Optional.absent() with the key in cache. At a later point, if the network and authentication problems are fixed, the key->invalidValue still remains in the cache. What is the best to fix this problem?

Is there a reason you need to save the invalid result in the cache? If you don't care about the absent case and simply want it excluded from the cache the easiest option would be to just not cache it in the first place. Throwing an exception on bad results would make that easy.

If you do need to keep the invalid results in the cache temporarily you can clear them once you're ready with a simple for loop:

ConcurrentMap<K, V> cacheAsMap = cache.asMap();
for (Entry<K, V> e : cacheAsMap .entrySet()) {
  if (!e.getValue().isPresent()) {
    cacheAsMap.remove(e.getKey(), e.getValue());
  }
}

By using ConcurrentMap.remove() you avoid a possible race condition where the entry is updated after e.getValue().isPresent() is called but before the entry is actually invalidated.

dimo414
  • 47,227
  • 18
  • 148
  • 244