Class Cached<K,V>
java.lang.Object
de.arstwo.twotil.Cached<K,V>
- Type Parameters:
K- anyV- any
- All Implemented Interfaces:
Function<K,V>
Simple key->value caching functionality, for both as a class and functional usage.
Null and thread safe, results are cached indefinitely as long as the function is in scope.
Usage example:
Cached<Long, Person> cache = new Cached(myDB::getByID);
// ...
Person person = cache.get(personID);
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionNecessary for functional usage.static <I,O> Function <I, O> Convenience accessor for functional feeling.voidclear()Clears the cache.Gets a value from the cache or the source.voidCleanup method to remove individual items.voidReplaces a cache entry.booleanReplaces a cache entry if it matches the old value.
-
Constructor Details
-
Cached
-
-
Method Details
-
cached
-
get
-
apply
-
clear
public void clear()Clears the cache. -
removeIf
-
replace
-
replaceIf
Replaces a cache entry if it matches the old value.- Parameters:
key- entry keyoldValue- expected old valuenewValue- new value to set- Returns:
- true if the entry was replaced, false otherwise, which usually indicates that the values did not match.
-