Package org.openhab.core.cache
Class ExpiringCache<V>
java.lang.Object
org.openhab.core.cache.ExpiringCache<V>
- Type Parameters:
V
- the type of the value
This is a simple expiring and reloading cache implementation.
There must be provided an action in order to retrieve/calculate the value. This action will be called only if the
answer from the last calculation is not valid anymore, i.e. if it is expired.
- Author:
- Christoph Weitkamp - Initial contribution, Martin van Wingerden - Add Duration constructor
-
Constructor Summary
ConstructorDescriptionExpiringCache
(long expiry, Supplier<@Nullable V> action) Create a new instance.ExpiringCache
(Duration expiry, Supplier<@Nullable V> action) Create a new instance. -
Method Summary
Modifier and TypeMethodDescription@Nullable V
getValue()
Returns the value - possibly from the cache, if it is still valid.final void
Invalidates the value in the cache.boolean
Checks if the value is expired.final void
Puts a new value into the cache.@Nullable V
Refreshes and returns the value in the cache.
-
Constructor Details
-
ExpiringCache
Create a new instance.- Parameters:
expiry
- the duration for how long the value stays validaction
- the action to retrieve/calculate the value- Throws:
IllegalArgumentException
- For an expirevalue <=0
.
-
ExpiringCache
Create a new instance.- Parameters:
expiry
- the duration in milliseconds for how long the value stays validaction
- the action to retrieve/calculate the value
-
-
Method Details
-
getValue
Returns the value - possibly from the cache, if it is still valid. -
putValue
Puts a new value into the cache.- Parameters:
value
- the new value
-
invalidateValue
public final void invalidateValue()Invalidates the value in the cache. -
refreshValue
Refreshes and returns the value in the cache.- Returns:
- the new value
-
isExpired
public boolean isExpired()Checks if the value is expired.- Returns:
- true if the value is expired
-