Package com.atlassian.cache
Interface ReadThroughCache<K,V>
- Type Parameters:
K- the key typeV- the value type
- All Known Subinterfaces:
Cache<K,V>
@PublicApi
public interface ReadThroughCache<K,V>
Interface that defines the common read-through cache operations.
Note: null keys and values are NOT supported. Although using null values may work for some
implementations, it won't work for all implementations and the behaviour may change over time.
- Since:
- 6.1
-
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(K key) Returns whether an entry exists in the cache under the specified key.Retrieve an object from this cache.Retrieve an object from this cache.Retrieve multiple entries from the cache.getKeys()Gets the keys of all objects currently stored in the cache.getName()The name of the cache, uniquely identifies this cache.voidRemove the object identified by the key from the cache.booleanAtomically removes the entry for a key only if currently mapped to a given value.voidRemove all of the objects from this cache.
-
Method Details
-
getName
The name of the cache, uniquely identifies this cache.- Returns:
- the name of the cache
-
containsKey
Returns whether an entry exists in the cache under the specified key.Note that:
- If the cache was created with a
CacheLoader, it will not be called. Obviously, any call toget(Object)will call the correspondingCacheLoader(if required). - If the cache was created with
CacheSettings.getReplicateViaCopy()set tofalseandCacheSettings.getLocal()set tofalse, then only the local copy of the cache is checked. A local cache on another node may contain an entry under the specified key.
- Parameters:
key- the key for the entry to check for containment- Returns:
- true iff the cache already contains an entry under the specified key
- Since:
- 2.2.0
- If the cache was created with a
-
getKeys
Gets the keys of all objects currently stored in the cache. This will return the keys in a new collection.- Returns:
- a collection of
Objects keys
-
get
Retrieve an object from this cache. Note that a copy of the cached object may be returned. Any changes that are made to the returned object may not be reflected in the cache. Cached values should be considered effectively immutable.- Parameters:
key- the key uniquely identifying the object to be retrieved- Returns:
- the object from the cache, or
nullif the object is not found
-
get
Retrieve an object from this cache. Note that a copy of the cached object may be returned. Any changes that are made to the returned object may not be reflected in the cache. Cached values should be considered effectively immutable.If no value is present in the cache, the
valueSupplierwill be used to populate the entry and be counted as a cache miss.- Parameters:
key- the key uniquely identifying the object to be retrievedvalueSupplier- the supplier to call if no value is stored in the cache. the value supplied by the supplier cannot benull- Returns:
- the object from the cache, or the newly created value from the supplier
- Since:
- 2.5
-
getBulk
@Nonnull default Map<K,V> getBulk(@Nonnull Set<K> keys, @Nonnull Function<Set<K>, Map<K, V>> valuesSupplier) Retrieve multiple entries from the cache. Any entries not present in the cache will be loaded using the given bulk loader function. By default, this method is implemented rather naively based upon calls toget(Object)andMap.put(Object, Object). This should be fine for the majority of cache implementations, and will still benefit from the bulk value loader. Individual cache implementations, however, may wish to override this method and provide an optimised implementation.- Parameters:
keys- the set of keys for the entries to be fetched from the cachevaluesSupplier- a function that takes the set of keys that were not present in the cache, and returns a map of keys to new values- Returns:
- a map of keys to values, a combination of entries that were already cached, and entries that had to be loaded
- Since:
- 5.3
-
remove
Remove the object identified by the key from the cache. If no object can be found associated with this key then no action is taken.- Parameters:
key- the key that uniquely identifies the object to be removed
-
remove
Atomically removes the entry for a key only if currently mapped to a given value.NOTE: Some cache backends (e.g. Ehcache) require this operation to be performed by CAS and will throw an exception when the cache is replicated
- Parameters:
key- the key with which the specified value is associatedvalue- the value expected to be associated with the specified key- Returns:
trueif the value was removed,falseotherwise
-
removeAll
void removeAll()Remove all of the objects from this cache.
-