K
- the keyV
- the valuepublic class DefaultLRUCachingMap<K,V> extends Object implements LRUCachingMap<K,V>, Serializable
LRUCachingMap
,
Serialized FormConstructor and Description |
---|
DefaultLRUCachingMap()
Default constructor, using DefaultLRUCachingMapConfiguration and DefaultMemoryValueManager.
|
DefaultLRUCachingMap(LRUCachingMapConfiguration configuration,
ValueManager<K,V> valueManager)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
cleanup()
Cleanup the cache to uphold expire time and maxSize.
If maxSize is exceeded than the oldest must be remove until maxSize threshold is below limit. This method must call the value manager flush method when is done. |
boolean |
exists(K key)
Validates if the key exists in the map, if its expired the key/value pair is removed.
|
void |
extend(K key)
Extends a key expire time with "getExtendTime()"
|
void |
extend(K key,
long extendTime)
Extends a key expire time with an extendTime still, and the "getMaxExtendCount()" will have no effect.
|
protected void |
extend(K key,
long extendTime,
int maxExtendCount) |
V |
get(K key)
Gets a Value for a corresponding Key.
|
LRUCachingMapConfiguration |
getConfiguration()
Gets the configuration instance
|
Date |
getExpirationDateTime(K key)
When do the key expire.
|
TimeSpan |
getExpirationTimeSpan()
The timespan in milliseconds, between the oldest and youngest key
|
TimeSpan |
getExpirationTimeSpan(K key)
Gets the expiration timespan for a key.
|
Iterator<K> |
getKeys()
Get the keys in a iterator.
|
Date |
getLastCleanupDateTime()
Gets the last time the cleanup method was performed
|
Logger |
getLogger()
A logger for getting metrics on how the caching is performing.
|
K |
getNextToExpire()
Gets the next key which has the lowest expiration date time.
|
Date |
getStoreDateTime(K key)
When was the Key stored in the cache.
|
ValueManager<K,V> |
getValueManager()
The manager class which handles the wrapped values, so it either can be stored in memory, file, db, etc...
|
protected AbstractValueWrapper<K,V> |
getValueWrapper(K key)
Gets the "AbstractValueWrapper<K,V>" for the value.
|
boolean |
isExpired(K key)
Is the key is expired.
|
boolean |
isPersisted()
Determine if ValueManager persists the the values.
|
V |
peek(K key)
Peek at the value with the key, but it do not remove the key/value if it is expired or extend its expiration datetime.
|
V |
put(K key,
V value)
Puts a key value pair in the cache.
|
int |
reload()
Will perform a complete reload of the cached elements if they are persisted, if not persisted nothing will happend.
|
V |
remove(K key)
Removes the key and its value form the cache.
|
int |
removeAll()
Remove all key/values from the cache, and delete the values if persisted.
|
void |
restoreCacheElement(K key,
AbstractValueWrapper<K,V> valueWrapper,
Date expireDateTime)
Used by ValueManager.initialize(LRUCachingMap) to initialize the caching structure, after an reload.
|
int |
size()
The number of unique keys in the cache.
|
public DefaultLRUCachingMap()
public DefaultLRUCachingMap(LRUCachingMapConfiguration configuration, ValueManager<K,V> valueManager) throws NullPointerException
getConfiguration().getCleanupIntervalTime()>0
than an CleanupTimerRunnable will be created inside a thread to do periodically cleanup. configuration
- the confiuration.valueManager
- valueManager the instance of a ValueManager which handles how the values is stored.NullPointerException
- if either configuration or valueManager is null
.LRUCachingMapConfiguration.getCleanupIntervalTime()
,
CleanupTimerRunnable
public LRUCachingMapConfiguration getConfiguration()
LRUCachingMap
getConfiguration
in interface LRUCachingMap<K,V>
DefaultLRUCachingMapConfiguration
public final Logger getLogger()
LRUCachingMap
getLogger
in interface LRUCachingMap<K,V>
public final ValueManager<K,V> getValueManager()
LRUCachingMap
getValueManager
in interface LRUCachingMap<K,V>
public final int size()
LRUCachingMap
size
in interface LRUCachingMap<K,V>
public final int removeAll()
LRUCachingMap
removeAll
in interface LRUCachingMap<K,V>
LRUCachingMap.size()
public final int reload()
LRUCachingMap
reload
in interface LRUCachingMap<K,V>
LRUCachingMap.isPersisted()
public V put(K key, V value) throws NullPointerException
LRUCachingMap
put
in interface LRUCachingMap<K,V>
key
- the keyvalue
- the value if the value is null
the key is removed.null
.NullPointerException
- if the key is null
.LRUCachingMapConfiguration.getExtendTime()
,
LRUCachingMapConfiguration.getMaxExtendCount()
,
LRUCachingMapConfiguration.getMaxSize()
,
ValueManager.storeValue(AbstractValueWrapper, Object, Object, Date)
,
ValueManager.constructWrapper(Object, Object)
public final void restoreCacheElement(K key, AbstractValueWrapper<K,V> valueWrapper, Date expireDateTime)
LRUCachingMap
restoreCacheElement
in interface LRUCachingMap<K,V>
key
- the reconstructed keyvalueWrapper
- the value wrapped.expireDateTime
- the datetime for when the key expires.ValueManager.restore(LRUCachingMap)
public final V get(K key) throws NullPointerException
LRUCachingMap
null
returned.get
in interface LRUCachingMap<K,V>
key
- the keynull
is returned.NullPointerException
- if key is null
.LRUCachingMapConfiguration
public final V peek(K key) throws NullPointerException
LRUCachingMap
peek
in interface LRUCachingMap<K,V>
key
- the keynull
.NullPointerException
- if key is null
.public final boolean isExpired(K key) throws NullPointerException
LRUCachingMap
isExpired
in interface LRUCachingMap<K,V>
key
- the keytrue
if the key is expired or the key is not found, otherwise false
.NullPointerException
- if key is null
.public final Date getStoreDateTime(K key) throws NullPointerException
LRUCachingMap
getStoreDateTime
in interface LRUCachingMap<K,V>
key
- the keynull
is returned, otherwise the new Date(long) for when the key was registered in the cache.NullPointerException
- if key is null
.public final V remove(K key) throws NullPointerException
LRUCachingMap
remove
in interface LRUCachingMap<K,V>
key
- the keyNullPointerException
- if key is null
.ValueManager.removeValue(AbstractValueWrapper)
public final boolean exists(K key) throws NullPointerException
LRUCachingMap
exists
in interface LRUCachingMap<K,V>
key
- the keytrue
if key is exists and is not expired, otherwise false
.NullPointerException
- if key is null
.LRUCachingMap.get(Object)
public final Iterator<K> getKeys()
LRUCachingMap
getKeys
in interface LRUCachingMap<K,V>
Iterator
public void extend(K key, long extendTime) throws NullPointerException, IllegalArgumentException
LRUCachingMap
extend
in interface LRUCachingMap<K,V>
key
- the keyextendTime
- how many milliseconds the keys expire time must be extended, must be greather than zero, otherwise nothing happens.NullPointerException
- if key is null
.IllegalArgumentException
LRUCachingMapConfiguration.getMaxExtendCount()
public void extend(K key) throws NullPointerException, IllegalArgumentException
LRUCachingMap
extend
in interface LRUCachingMap<K,V>
key
- the keyNullPointerException
- if key is null
.IllegalArgumentException
LRUCachingMapConfiguration.getMaxExtendCount()
,
LRUCachingMapConfiguration.getExtendTime()
protected final void extend(K key, long extendTime, int maxExtendCount) throws NullPointerException, IllegalArgumentException
public final Date getExpirationDateTime(K key) throws NullPointerException
LRUCachingMap
getExpirationDateTime
in interface LRUCachingMap<K,V>
key
- the keynull
.NullPointerException
- if key is null
.public final TimeSpan getExpirationTimeSpan(K key) throws NullPointerException
LRUCachingMap
getExpirationTimeSpan
in interface LRUCachingMap<K,V>
key
- theTimeSpan.ZERO
is returned.NullPointerException
- if key s null
.TimeSpan.ZERO
public final int cleanup()
LRUCachingMap
cleanup
in interface LRUCachingMap<K,V>
LRUCachingMapConfiguration.getExpireTime()
,
LRUCachingMapConfiguration.getMaxSize()
,
LRUCachingMap.getNextToExpire()
,
ValueManager.flush()
,
LRUCachingMap.getValueManager()
public final TimeSpan getExpirationTimeSpan()
LRUCachingMap
getExpirationTimeSpan
in interface LRUCachingMap<K,V>
public final K getNextToExpire()
LRUCachingMap
getNextToExpire
in interface LRUCachingMap<K,V>
null
is returned.public final boolean isPersisted()
LRUCachingMap
isPersisted
in interface LRUCachingMap<K,V>
true
if value is stored in a persistence storage an can be recovered after "reboot", otherwise false
.ValueManager.isPersisted()
public final Date getLastCleanupDateTime()
LRUCachingMap
getLastCleanupDateTime
in interface LRUCachingMap<K,V>
protected final AbstractValueWrapper<K,V> getValueWrapper(K key)
key
- the keyCopyright © 2016. All rights reserved.