K
- key type of cache (must be immutable and have correct hashCode & equals)V
- value type of cache (should, for monitoring and predictable memory
effect of eviction, typically, be of "similar" size for all keys;
else consider simply using separate Cache, if feasible)@ThreadSafe public interface Cache<K,V> extends BaseCache<K,V>
Users typically obtain implementations from the CacheProvider
.
Caches are not Map
s! Differences include that a Map persists
all elements that are added to it until they are explicitly removed. A Cache on the
other hand is generally configured to evict entries automatically, in order to constrain
its memory footprint, based on some policy (quoted from
Caffeine's or
Guava's
introduction). Another notable difference, enforced by this caching API, is
that caches should not be thought of as data structures that you put
something in somewhere in your code to get it out of somewhere else. Instead, a
Cache is "just a façade" to a CacheFunction
's get. This design enforces
proper encapsulation, and helps you not to screw up the content of your cache (like you
easily can, and usually do, when you use a Map as a cache).
The implementation of this API is, typically, backed by established cache frameworks, such as Ehcache, Infinispan, Guava's, Caffeine, ..., imcache, cache2k, ... etc.
Modifier and Type | Method and Description |
---|---|
com.google.common.collect.ImmutableMap<K,V> |
get(Iterable<? extends K> keys)
Get several cache entries, in one go.
|
default com.google.common.collect.ImmutableMap<K,V> |
get(K... keys)
Convenience short-cut to
get(Iterable) with vararg syntax. |
V |
get(K key)
Get cache entry.
|
evict, getManager, put
close
@Nonnull V get(@Nonnull K key) throws BadCacheFunctionRuntimeException, NullPointerException
CacheFunction
given when the cache was
created.key
- key of cache entryBadCacheFunctionRuntimeException
- if the cache's function returned null valueNullPointerException
- if the cache's users passed a null key@Nonnull com.google.common.collect.ImmutableMap<K,V> get(@Nonnull Iterable<? extends K> keys) throws BadCacheFunctionRuntimeException, NullPointerException
keys
- list of keys of cache entriesBadCacheFunctionRuntimeException
- if the cache's function returned null valueNullPointerException
- if the cache's users passed a null key@Nonnull default com.google.common.collect.ImmutableMap<K,V> get(@Nonnull K... keys) throws BadCacheFunctionRuntimeException, NullPointerException
get(Iterable)
with vararg syntax.Copyright © 2019 OpenDaylight. All rights reserved.