V - type of the data model to cache.public abstract class AbstractSelfLoadingCache<V> extends Object implements SelfLoadingCache<V>
SelfLoadingCache.
This class provides most of the common logic and
some useful extension hooks. Any custom implementation
of the SelfLoadingCache interface should
extend this class.
| Modifier and Type | Field and Description |
|---|---|
protected CacheProvider<V> |
cacheProvider
Underlying caching system provider.
|
protected CacheConfig |
config
Configurations to be used to define the cache behavior.
|
protected boolean |
disabledThis
Tells if this
SelfLoadingCache is disabled. |
protected static ExecutorService |
executorService
The
ExecutorService to use to insert or update
cache entries in an asynchronous way. |
static Logger |
logger
Logging system.
|
| Constructor and Description |
|---|
AbstractSelfLoadingCache(CacheConfig cacheConfig,
CacheProvider<V> cacheProvider)
Constructor with parameters.
|
| Modifier and Type | Method and Description |
|---|---|
void |
disableThis(boolean disable)
Invoking this method with
true will disable this SelfLoadingCache
instance. |
void |
evict(CacheKey key)
Removes the given key from the underlying cache system
forcing to reload it the next time
SelfLoadingCache.get(CacheKey, DataProvider)
is invoked. |
protected CacheEntry<V> |
get(CacheKey key)
Returns the value from the cache if available
and
null otherwise. |
V |
get(CacheKey key,
DataProvider<V> dataProvider)
Returns the value related to the given key.
|
protected V |
insert(CacheKey key,
DataProvider<V> dataProvider)
Inserts the given key and the related value into the cache.
|
protected V |
load(CacheKey key,
DataProvider<V> dataProvider)
Loads the value from the
DataProvider. |
protected V |
loadAndPut(String operation,
CacheKey key,
DataProvider<V> dataProvider)
Performs the loading of te data related to the given
key and puts the
<key-value> pair into the cache. |
protected void |
put(CacheKey key,
V value)
Puts the given
key-value pair into the cache. |
protected V |
update(CacheKey key,
CacheEntry<V> entry,
DataProvider<V> dataProvider)
Updates the cache for the given key and the related value.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitdisableAllpublic static final Logger logger
By default the logging level is Level.WARNING and will only log exceptions.
If you want to log any caching operation you need to set the logging level to Level.INFO.
If you want to dig into deeper detail about cache loading and updating operations,
you need to set the logging level to Level.FINE.
If you want to log everything is happening in the cache, including the data that is going to be
cached, you need to set the logging level to Level.FINEST.
If you trust this class to work properly and you do not want your console output to get dirty
you can turn off this log by setting the logging level to Level.OFF.
To change the Logger levels and outputs follow the
Java documentation.
protected static final ExecutorService executorService
ExecutorService to use to insert or update
cache entries in an asynchronous way.protected final CacheConfig config
protected final CacheProvider<V> cacheProvider
protected boolean disabledThis
SelfLoadingCache is disabled.
If this flag is set to true this instance
will skip any caching operation and all data will
be loaded on each request.
This option should be used to debugging purposes only, using this option in production can be very dangerous.
public AbstractSelfLoadingCache(CacheConfig cacheConfig, CacheProvider<V> cacheProvider)
cacheConfig - configurations to be used to define the cache behavior.cacheProvider - underlying caching system provider.public void disableThis(boolean disable)
true will disable this SelfLoadingCache
instance. All the caching will be skipped and all data will be loaded from the
DataProvider on each request.
This option should be used to debugging purposes only, using this option in production can be very dangerous.
Invoking this method with false will restore the caching process.
disableThis in interface SelfLoadingCache<V>disable - tells if this SelfLoadingCache instance needs to stop
all caching operations.public V get(CacheKey key, DataProvider<V> dataProvider)
If the given key is not present in the cache or
the related entry is expired the given
DataProvider will be used to retrieve
the data to store into the cache.
get in interface SelfLoadingCache<V>key - the key to search for.dataProvider - the provider of the information to cache.null.public void evict(CacheKey key)
SelfLoadingCache.get(CacheKey, DataProvider)
is invoked.evict in interface SelfLoadingCache<V>key - key to be evicted.protected CacheEntry<V> get(CacheKey key)
null otherwise.
This method returns null in both cases:
null.key - the key to search for.null.CacheProviderException - if an error occurred during
cache operations and the flag
CacheConfig.isThrowCacheProviderExceptions()
is true.protected void put(CacheKey key, V value)
key-value pair into the cache.key - the key to cache.value - the value to cache.CacheProviderException - if an error occurred during
cache operations and the flag
CacheConfig.isThrowCacheProviderExceptions()
is true.protected V load(CacheKey key, DataProvider<V> dataProvider)
DataProvider.key - the key to search for.dataProvider - the DataProvider to invoke.DataProviderException - if there is a failure
in the data loading.protected V loadAndPut(String operation, CacheKey key, DataProvider<V> dataProvider)
<key-value> pair into the cache.operation - the operation to log.key - key to cache.dataProvider - provider of the values to be cached.CacheProviderException - if an error occurred during
cache operations and the flag
CacheConfig.isThrowCacheProviderExceptions()
is true.DataProviderException - if there is a failure
in the data loading.protected V insert(CacheKey key, DataProvider<V> dataProvider)
If this cache is configured to perform inserts synchronously
(the default) then this method will invoke loadAndPut(String,CacheKey,DataProvider),
otherwise the insert will be performed by another thread while
this method returns null.
key - key to cache.dataProvider - provider of the values to be cached.null.CacheProviderException - if an error occurred during
cache operations and the flag
CacheConfig.isThrowCacheProviderExceptions()
is true.DataProviderException - if there is a failure
in the data loading.protected V update(CacheKey key, CacheEntry<V> entry, DataProvider<V> dataProvider)
If this cache is configured to perform updates asynchronously (the default) then the update will be performed by another thread while this method returns the current cached value, otherwise this method will invoke {#loadAndPut(String,CacheKey,DataProvider)} and return the fresh loaded value.
If multiple threads try to update the same key only one will succeed, all the other will return the currently cached value.
key - key to cache.entry - the expired cache entry.dataProvider - provider of the values to be cached.CacheProviderException - if an error occurred during
cache operations and the flag
CacheConfig.isThrowCacheProviderExceptions()
is true.DataProviderException - if there is a failure
in the data loading.Copyright © 2011–2020 Nerd4j. All rights reserved.