- All Known Implementing Classes:
Cache0,Cache1,CacheCaffeine,CacheSimple,CacheWrapper
public interface Cache<Key,Value>
An abstraction of a cache for basic use.
This cache does not support null as keys or values.
For more complex configuration of the cache, use the cache builder of the implementation of choice.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Key key) Does the cache contain the key?Get from cache; if not present, call theFunctionto fill the cache slot.getIfPresent(Key key) Get from cache - or return null.default ValueDeprecated, for removal: This API element is subject to removal in a future version.booleanisEmpty()keys()Iterate over all keys.voidInsert into the cachevoidRemove from cache - return true if key referenced an entrylongsize()Current size of cachedefault CacheInfostats()Cache statistics (not supported by all caches)
-
Method Details
-
containsKey
Does the cache contain the key?- Parameters:
key- The key to find. The key must not be null.- Returns:
- True, if the cache contains the key, otherwise false.
-
getIfPresent
Get from cache - or return null.- Parameters:
key- The key for which the value is requested. The key must not be null.- Returns:
- If the cache contains an entry for the given key, the value is returned, otherwise null.
-
getOrFill
Deprecated, for removal: This API element is subject to removal in a future version.Get from cache; if not present, call theCallableto try to fill the cache. This operation should be atomic. The 'key' and 'callcable' must not be null. -
get
Get from cache; if not present, call theFunctionto fill the cache slot. This operation should be atomic.- Parameters:
key- The key, for which the value should be returned or calculated. The key must not be null.callable- If the cache does not contain the key, the callable is called to calculate a value. If the callable returns null, the key is not associated with hat value, as nulls are not accepted as values. The callable must not be null.- Returns:
- Returns either the existing value or the calculated value. If callable is called and returns null, then null is returned.
-
put
Insert into the cache- Parameters:
key- The key for the 'thing' to store. The key must not be null.thing- If 'thing' is null, it will not be used as value, instead any existing entry with the same key will be removed.
-
remove
Remove from cache - return true if key referenced an entry- Parameters:
key- The key, which shall be removed along with its value. The key must not be null.
-
keys
Iterate over all keys. Iterating over the keys requires the caller be thread-safe. -
isEmpty
boolean isEmpty() -
clear
void clear() -
size
long size()Current size of cache -
stats
Cache statistics (not supported by all caches)
-
get(Object, Function)