Package overit.geocall.cache
Class Cache
java.lang.Object
overit.geocall.cache.Cache
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
CaffeineCache,RedisCache
Define an abstract class far a generic cache system capable of caching
Entry objects.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classUtility class that holds the information about a specific cache entry. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckPrefix(String key, boolean exception) protected abstract booleanclear()Clears all the cachevoidclose()Close the cache and disconnects from the parentabstract <T,S> Item <T, S> Extracts data from cache, or fetches and caches data from the parent cache.protected voidfinalize()abstract <T,S> Item <T, S> Locally retrieve the data, without search in the parent cache if the entry has not been found.abstract List<Cache.Info> Return the information about the cache's entries related to the passed key.Return the parent cache from where fetch the entries that are not present into the current cache.protected final CachegetRoot()protected final <T,S> void Insert the entry in the current cache and all depending cachesfinal voidinvalidate(String key) Tells the cache that an entry is changed and should be invalidate in all the cache treefinal voidinvalidate(Predicate<String> predicate) Tells the cache that all the entries matching the predicate are changed and should be invalidated in all the cache treevoidTells the cache that all the cache tree is invalid and must be clearedkeys()Returns the keys stored inside the cacheprotected final voidRemove the entry from all the depending cachesprotected final voidRemove all the entries from all the depending cachesprotected final voidRemove the entry from the current cache and all depending cachesprotected final voidloseAll()Remove all the entries from the current cache and all depending cachesvoidonInvalidation(String key, Runnable listener) Register the passed listener to the invalidation of the specific key.protected abstract <T,S> void Locally stores an item inside the cacheprotected abstract booleanLocally removes a key from the cachefinal voidSet the passed cache as parent of the current one.<T,S> void tells the cache that an entry has a new value and must be propagated in all the cache tree
-
Constructor Details
-
Cache
Construct a generic cache, working with a som-prefix keys and possibly referring a parent cache- Parameters:
keyPrefix- the prefix af all the key cachedparent- the optional parent cache
-
-
Method Details
-
getParent
Return the parent cache from where fetch the entries that are not present into the current cache.- Returns:
- the parent cache or
nullif there's no one.
-
setParent
Set the passed cache as parent of the current one. All the entries that are not loaded in the current cache, will be fetched from the parent one. If the current cache has no parent, the entries will be directly loaded.- Parameters:
parent- the cache's instance that will be used as parent of the current one.
-
close
public void close()Close the cache and disconnects from the parent- Specified by:
closein interfaceAutoCloseable
-
finalize
-
checkPrefix
-
fetch
Extracts data from cache, or fetches and caches data from the parent cache. If the parent cache is missing it can use theEntry.load()to load the real data.- Type Parameters:
T- the type of the raw data contained in the cache itemS- the type of the computed data contained in the cache item- Parameters:
entry- the cache's entry to look for- Returns:
- the data contained in cache, on the parent cache, or potentially loaded and cached
-
remove
Locally removes a key from the cache- Parameters:
key- the key of the entry to be removed- Returns:
trueif the key has been removed,falseif the key was absent
-
clear
protected abstract boolean clear()Clears all the cache- Returns:
trueif the cache has been cleaned,falseif the cache was empty
-
put
Locally stores an item inside the cache- Type Parameters:
T- the type of the raw data contained in the cache itemS- the type of the computed data contained in the cache item- Parameters:
entry- the item to be stored
-
get
Locally retrieve the data, without search in the parent cache if the entry has not been found.- Type Parameters:
T- the type of the raw data contained in the cache itemS- the type of the computed data contained in the cache item- Parameters:
entry- the cache's entry to look for- Returns:
- the data contained in cache or
nullif not present
-
keys
Returns the keys stored inside the cache- Returns:
- the keys stored inside the cache or an empty set if there's none.
-
getInfo
Return the information about the cache's entries related to the passed key. Each element contains the information about a specific view for that entry.- Parameters:
key- the key of the entry- Returns:
- the list containing the information, or an empty list the there's no information related to the passed key
-
invalidate
Tells the cache that an entry is changed and should be invalidate in all the cache tree- Parameters:
key- the name of the entry to be invalidate
-
invalidate
Tells the cache that all the entries matching the predicate are changed and should be invalidated in all the cache tree- Parameters:
predicate- the predicate to match the keys to be invalidated
-
invalidateAll
public void invalidateAll()Tells the cache that all the cache tree is invalid and must be cleared -
onInvalidation
Register the passed listener to the invalidation of the specific key. When the cache's entry, with the specific key, will be invalidated, the listener function will be called.- Parameters:
key- the cache key to listen tolistener- the runnable task that will be called when the key will be invalidated
-
getKeyPrefix
- Returns:
- the prefix for all the keys admitted inside the cache
-
getRoot
- Returns:
- the instance root cache. If the current cache doesn't belong to any caches tree, it returns the current instance.
-
store
tells the cache that an entry has a new value and must be propagated in all the cache tree- Type Parameters:
T- the type of the raw data contained in the cache itemS- the type of the computed data contained in the cache item- Parameters:
entry- the entry to be stored
-
lose
Remove the entry from the current cache and all depending caches- Parameters:
key- the name of the entry to be removed
-
loosed
Remove the entry from all the depending caches- Parameters:
key- the name of the entry to be removed
-
loseAll
protected final void loseAll()Remove all the entries from the current cache and all depending caches -
loosedAll
protected final void loosedAll()Remove all the entries from all the depending caches -
hold
Insert the entry in the current cache and all depending caches- Type Parameters:
T- the type of the raw data contained in the cache itemS- the type of the computed data contained in the cache item- Parameters:
entry- the entry to be inserted
-