public abstract class AbstractCache<K,V> extends AbstractNurseryService implements Cache<K,V>, com.escenic.jmx.CacheMXBean, com.escenic.jmx.CacheStatisticsSourceMXBean
Cache
implementations. The class
onRemoval(CacheItem)
when the item is removed. This method will ensure the statistics is updated correctly.GenericService.StartCommand, GenericService.StopCommand
CLASS_VERSION
mLogger, state
Constructor and Description |
---|
AbstractCache()
Constructor
|
AbstractCache(int pMaxSize)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
addObject(K pKey,
V pValue)
Add a new object to the cache.
|
void |
expireObject(K pKey)
Expire an object in the cache.
|
void |
flushCache()
This method will flush all objects from the cache.
|
long |
getAdds() |
long |
getAges() |
long |
getAverageIdleTimeSinceLastThrow() |
long |
getAverageIdleTimeWhenThrown() |
protected String |
getCacheStatistics()
Returns cache statistics as a string.
|
String |
getCacheType() |
long |
getClears() |
long |
getExpires() |
double |
getHitRatio() |
long |
getHits() |
long |
getLastThrow() |
int |
getMaxSize()
Get the max number of cached objects in this cache.
|
long |
getMisses() |
V |
getObject(K pKey)
Get an object from the cache.
|
long |
getPushes() |
long |
getRemoves() |
String |
getReport(String type)
Return a report for this status
|
Number |
getStatistic(String pStatisticKey)
The different statistics metrics of the implementing class.
|
Set |
getStatisticKeys()
The keys that the metrics are available under.
|
long |
getTimeSinceLastThrow() |
long |
getTotalTime() |
int |
getValidSeconds()
Get the validSeconds for this cache.
|
boolean |
hasObject(K pKey)
Check if the cache contains the object while not changing any of
the cache statistics.
|
void |
remove(CacheFilter<K,V> f)
Iterates through all the items in the cache and removes all items that is not accepted by the given filter.
|
void |
reset()
Reset any counters
|
void |
setCacheType(String pCacheType) |
void |
setMaxSize(int pMaxSize) |
void |
setValidSeconds(int pValidSeconds) |
protected void |
startService()
Is called by
AbstractNurseryService.doStartService() when the NurseryService is being started. |
protected void |
stopService()
Is called by
AbstractNurseryService.doStopService() when the service is being shut down. |
String |
toString() |
doStartService, doStopService, stopIdleService
assertServiceRunning, getServiceDescription, getServiceMessage, getServiceState, isServiceEnabled, isServiceFailed, isServiceIdle, isServiceRunning, isServiceShutdownFailed, isServiceStopped, setServiceDescription, setServiceEnabled, start, stop, validateIsTrue, validateNotNull
public AbstractCache()
public AbstractCache(int pMaxSize)
pMaxSize
- the maximum size of the cacheprotected void startService() throws Exception
AbstractNurseryService
Is called by AbstractNurseryService.doStartService()
when the NurseryService
is being started. Exceptions thrown by this
method are logged by AbstractNurseryService.doStartService()
.
Subclasses must implement this method to bootstrap the service.
startService
in class AbstractNurseryService
Exception
- when something goes wrong during startup. GenericService.isServiceRunning()
will return false
and GenericService.isServiceFailed()
will return true
.IllegalArgumentException
- when the configuration of the service
is invalid. GenericService.isServiceRunning()
will return false
and GenericService.isServiceFailed()
will return true
.IllegalStateException
- when this service is in the wrong state for startup. Used to signal to AbstractNurseryService.doStartService()
that it is OK to try to starting
this service
later. GenericService.isServiceRunning()
returns false
and GenericService.isServiceIdle()
returns true
.AbstractNurseryService.doStartService()
protected void stopService() throws Exception
AbstractNurseryService
Is called by AbstractNurseryService.doStopService()
when the service
is being shut down. Exceptions thrown by this
method are logged by AbstractNurseryService.doStopService()
.
Subclasses must implement this method to shut down the service.
stopService
in class AbstractNurseryService
IllegalStateException
- when this service is in the wrong state for shutdown. Used to signal to AbstractNurseryService.doStopService()
that
it is OK to try stopping this service
later. GenericService.isServiceRunning()
returns true
.Exception
- when something goes during shutdown. GenericService.isServiceRunning()
returns false
and
GenericService.isServiceShutdownFailed()
returns true
.AbstractNurseryService.doStopService()
public final String getCacheType()
getCacheType
in interface com.escenic.jmx.CacheMXBean
getCacheType
in interface Cache<K,V>
public final int getMaxSize()
Cache
getMaxSize
in interface com.escenic.jmx.CacheMXBean
getMaxSize
in interface Cache<K,V>
public final int getValidSeconds()
Cache
getValidSeconds
in interface com.escenic.jmx.CacheMXBean
getValidSeconds
in interface Cache<K,V>
public final V getObject(K pKey)
Cache
public final boolean hasObject(K pKey)
Cache
public final void addObject(K pKey, V pValue)
Cache
public final void expireObject(K pKey)
Cache
expireObject
in interface Cache<K,V>
pKey
- The identifier of the object to expire.public final void flushCache()
Cache
flushCache
in interface com.escenic.jmx.CacheMXBean
flushCache
in interface Cache<K,V>
public final Number getStatistic(String pStatisticKey)
StatisticsSource
getStatistic
in interface StatisticsSource
pStatisticKey
- the key of the metric wanted. See StatisticsSource.getStatisticKeys()
for a list of valid keys.StatisticsSource.getStatisticKeys()
public final Set getStatisticKeys()
StatisticsSource
getStatisticKeys
in interface StatisticsSource
Set
of String
keys. It will
contain at least one key.StatisticsSource.getStatistic(String)
public final String getReport(String type)
StatusSource
getReport
in interface StatusSource
public final void reset()
StatusSource
reset
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
reset
in interface StatusSource
public final long getHits()
getHits
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getMisses()
getMisses
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getExpires()
getExpires
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getPushes()
getPushes
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getAdds()
getAdds
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getClears()
getClears
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getRemoves()
getRemoves
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getLastThrow()
getLastThrow
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getAges()
getAges
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getTotalTime()
getTotalTime
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final double getHitRatio()
getHitRatio
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getAverageIdleTimeWhenThrown()
getAverageIdleTimeWhenThrown
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getAverageIdleTimeSinceLastThrow()
getAverageIdleTimeSinceLastThrow
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final long getTimeSinceLastThrow()
getTimeSinceLastThrow
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public final void setMaxSize(int pMaxSize)
setMaxSize
in interface com.escenic.jmx.CacheMXBean
public void setCacheType(String pCacheType)
public final void setValidSeconds(int pValidSeconds)
setValidSeconds
in interface com.escenic.jmx.CacheMXBean
public final void remove(CacheFilter<K,V> f)
Cache
protected String getCacheStatistics()
toString()
. Subclasses might override this
method to provide additional statistics to the cache. The default implementation adds all statistics from
the CacheStatisticsSource
. If subclasses wants this information in their toString()
they
must include the result from the default implemetation in their return value© 1998- 2018 Stibo DX