Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava LoadingCache - how to handle keys which don't exist in backing store

I am using CacheBuilder and LoadingCache to implement an in-memory cache of database data.

Suppose a client queries the cache for an item that does not exist in the backing store. I want the client to know that no data was found for the specified key. What is the best approach for handling this?

  • Store special value in cache which signifies "no data".
  • Store nothing in cache and raise exception.
  • Other ideas?
like image 996
frankadelic Avatar asked Jun 13 '12 15:06

frankadelic


People also ask

Is Guava cache thread-safe?

Cache entries are manually added using get(Object, Callable) or put(Object, Object) , and are stored in the cache until either evicted or manually invalidated. Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.

Where is guava cache stored?

Guava Caches store values in RAM.

What is concurrency level in guava cache?

Guava's cache is built on top of Java 5's ConcurrentHashMap with a default concurrency level of 4. This setting is because that hash table is segmented into multiple smaller tables, so more segments allows for higher concurrency at a cost of a larger memory footprint.


1 Answers

I've always solved this in the following way.

interface KeyValueService<K,V> {
    V get(K key);
}

class CachingKeyValueService<K,V> {
    Cache<K,Optional<V>> cache;
    V get(K key) {
        return cache.get(key).orNull();
    }

}

Ideally you would change the interface for KeyValueService to always return Optional, but sometimes thats not possible.

You can use weighting to cause all Optional.ABSENT references to be evicted quickly.

like image 65
Emily Avatar answered Nov 02 '22 23:11

Emily