Package org.openhab.core.cache
Class ExpiringCacheMap<K,V>
java.lang.Object
org.openhab.core.cache.ExpiringCacheMap<K,V>
- Type Parameters:
K
- the type of the keyV
- the type of the value
This is a simple expiring and reloading multiple key-value-pair cache implementation. The value expires after the
specified duration has passed since the item was created, or the most recent replacement of the value.
- Author:
- Christoph Weitkamp - Initial contribution, Martin van Wingerden - Added constructor accepting Duration and putIfAbsentAndGet
-
Constructor Summary
ConstructorDescriptionExpiringCacheMap
(long expiry) Creates a new instance.ExpiringCacheMap
(Duration expiry) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Discards all items from the cache.boolean
containsKey
(K key) Checks if the key is present in the cache.@Nullable V
Returns the value associated with the given key - possibly from the cache, if it is still valid.void
invalidate
(K key) Invalidates the value associated with the given key in the cache.void
Invalidates all values in the cache.keys()
Returns a set of all keys.void
Creates anExpiringCache
and adds it to the cache.void
put
(K key, ExpiringCache<@Nullable V> item) Adds anExpiringCache
to the cache.void
putIfAbsent
(K key, ExpiringCache<V> item) If the specified key is not already associated with a value, associate it with the givenExpiringCache
.@Nullable V
putIfAbsentAndGet
(K key, Supplier<V> action) If the specified key is not already associated, associate it with the given action.@Nullable V
putIfAbsentAndGet
(K key, ExpiringCache<V> item) If the specified key is not already associated with a value, associate it with the givenExpiringCache
.void
Puts a new value into the cache if the specified key is present.@Nullable V
Refreshes and returns the value associated with the given key in the cache.Collection<@Nullable V>
Refreshes and returns a collection of all new values in the cache.void
Removes the item associated with the given key from the cache.Collection<@Nullable V>
values()
Returns a collection of all values - possibly from the cache, if they are still valid.
-
Constructor Details
-
ExpiringCacheMap
Creates a new instance.- Parameters:
expiry
- the duration for how long the value stays valid
-
ExpiringCacheMap
public ExpiringCacheMap(long expiry) Creates a new instance.- Parameters:
expiry
- the duration in milliseconds for how long the value stays valid
-
-
Method Details
-
put
Creates anExpiringCache
and adds it to the cache.- Parameters:
key
- the key with which the specified value is to be associatedaction
- the action for the item to be associated with the specified key to retrieve/calculate the value
-
put
Adds anExpiringCache
to the cache.- Parameters:
key
- the key with which the specified value is to be associateditem
- the item to be associated with the specified key
-
putIfAbsent
If the specified key is not already associated with a value, associate it with the givenExpiringCache
.- Parameters:
key
- the key with which the specified value is to be associateditem
- the item to be associated with the specified key
-
putIfAbsentAndGet
If the specified key is not already associated, associate it with the given action. Note that this method has the overhead of actually calling/performing the action- Parameters:
key
- the key with which the specified value is to be associatedaction
- the action for the item to be associated with the specified key to retrieve/calculate the value- Returns:
- the (cached) value for the specified key
-
putIfAbsentAndGet
If the specified key is not already associated with a value, associate it with the givenExpiringCache
. Note that this method has the overhead of actually calling/performing the action- Parameters:
key
- the key with which the specified value is to be associateditem
- the item to be associated with the specified key- Returns:
- the (cached) value for the specified key
-
putValue
Puts a new value into the cache if the specified key is present.- Parameters:
key
- the key whose value in the cache is to be updatedvalue
- the new value
-
containsKey
Checks if the key is present in the cache.- Parameters:
key
- the key whose presence in the cache is to be tested- Returns:
- true if the cache contains a value for the specified key
-
remove
Removes the item associated with the given key from the cache.- Parameters:
key
- the key whose associated value is to be removed
-
clear
public void clear()Discards all items from the cache. -
keys
Returns a set of all keys.- Returns:
- the set of all keys
-
get
Returns the value associated with the given key - possibly from the cache, if it is still valid.- Parameters:
key
- the key whose associated value is to be returned- Returns:
- the value associated with the given key, or null if there is no cached value for the given key
-
values
Returns a collection of all values - possibly from the cache, if they are still valid.- Returns:
- the collection of all values
-
invalidate
Invalidates the value associated with the given key in the cache.- Parameters:
key
- the key whose associated value is to be invalidated
-
invalidateAll
public void invalidateAll()Invalidates all values in the cache. -
refresh
Refreshes and returns the value associated with the given key in the cache.- Parameters:
key
- the key whose associated value is to be refreshed- Returns:
- the value associated with the given key, or null if there is no cached value for the given key
-
refreshAll
Refreshes and returns a collection of all new values in the cache.- Returns:
- the collection of all values
-