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?
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.
Guava Caches store values in RAM.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With