V - type of data in the cache entry.public abstract class AbstractCacheProvider<V> extends Object implements CacheProvider<V>
CacheProvider interface
that implements all the common checks and logic allowing the
extending classes to implement only the underlying caching
system related logic.| Constructor and Description |
|---|
AbstractCacheProvider()
Default constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all elements from the cache.
|
protected abstract void |
doClear()
Removes all keys and related entries from the cache.
|
protected abstract CacheEntry<V> |
doGet(CacheKey key)
Returns the cache entry related to the given key.
|
protected abstract void |
doPut(CacheKey key,
CacheEntry<V> entry,
long duration)
Binds the given entry to the given key and
put it into the underlying cache.
|
protected abstract void |
doRemove(CacheKey key)
Removes the given key and the related entry from the cache.
|
protected abstract boolean |
doTouch(CacheKey key,
long duration)
Defers the expiration time of the cache entry related to the given key.
|
CacheEntry<V> |
get(CacheKey key)
Returns the cache entry related to the given key.
|
protected CacheEntry<V> |
getEntry(V value,
long duration)
Returns a new cache entry with the given value that will
expire after the given amount of milliseconds
|
void |
put(CacheKey key,
V value,
long duration)
Inserts the given key and the related value into the cache.
|
void |
remove(CacheKey key)
Removes the given key from the cache.
|
boolean |
touch(CacheKey key,
long duration)
Defers the expiration time of the entry related to the given key.
|
public 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,
including the data that is going to be cached, you need to set the logging level to Level.FINE.
If you trust this class to work properly and you do not want your logs 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.
public CacheEntry<V> get(CacheKey key)
If the given key is never been cached null will be returned,
otherwise an object of type CacheEntry will be returned
containing the related value and expiration time.
The value in the CacheEntry can be null.
get in interface CacheProvider<V>key - the key to search for.null otherwise.public void put(CacheKey key, V value, long duration)
If the given key is already present, the related CacheEntry
will be replaced.
The duration is expressed in milliseconds. If the duration is 0
or less nothing will be done.
put in interface CacheProvider<V>key - key to be cached.value - value to be cached.duration - the amount of time the value will last in cache.public boolean touch(CacheKey key, long duration)
If the key is not present a CacheProviderException
will be thrown.
The duration is expressed in milliseconds. If the duration is 0
or less nothing will be done.
touch in interface CacheProvider<V>key - the key to search for.duration - number of milliseconds until expiration.true if the operation was successful, @code false} otherwise.public void remove(CacheKey key)
If the key is not present nothing will be done.
remove in interface CacheProvider<V>key - the key to be removed.public void clear()
This method will clear the underlying cache.
clear in interface CacheProvider<V>protected CacheEntry<V> getEntry(V value, long duration)
value - the value to put into the cache entry.duration - the amount of milliseconds before expiration.protected abstract CacheEntry<V> doGet(CacheKey key) throws Exception
If the given key was never being cached null
will be returned, otherwise will be returned a
CacheEntry containing the cached value
and the expiration time.
key - the cache key to search for.null otherwise.Exception - if the method execution fails.protected abstract void doPut(CacheKey key, CacheEntry<V> entry, long duration) throws Exception
If the given key is already present into the cache will be replaced.
key - key to be cached.entry - entry to be cached.duration - number of milliseconds until expiration.Exception - if the method execution fails.protected abstract boolean doTouch(CacheKey key, long duration) throws Exception
If no entry is present for the given key a RequirementFailure
should be thrown.
Only the first thread calling this method for the given key should be successful,
any other thread should fail. This method returns true if the operation
was successful and false otherwise.
key - key to update.duration - number of milliseconds until expiration.true if the related entry has been updated.Exception - if the method execution fails.protected abstract void doRemove(CacheKey key) throws Exception
If the key is not present nothing will be done.
key - key to be removed.Exception - if the method execution fails.Copyright © 2011–2020 Nerd4j. All rights reserved.