K
- the key of the object we want to cacheV
- the value of the object we want to cachepublic class DelegatingCache<K,V> extends AbstractNurseryService implements Cache<K,V>, com.escenic.jmx.CacheMXBean, com.escenic.jmx.CacheStatisticsSourceMXBean
DelegatingCache is an implementation of the Cache
interface that will not store anything itself, that is delegated to the Cache
configured in
getCache()
.
This implementation is useful when creating a sort of Hierarchical Cache Management systems. No internal storage, everything delegated up. Keeps statistics of local usage in this class.
If GenericService.isServiceRunning()
returns false
will getObject(Object)
and hasObject(Object)
always return null
Modifier and Type | Class and Description |
---|---|
class |
DelegatingCache.Item<V> |
static class |
DelegatingCache.Key<K>
A class depicting a key suitable for use in an
Map . |
GenericService.StartCommand, GenericService.StopCommand
Modifier and Type | Field and Description |
---|---|
static String |
CLASS_VERSION
The version of this class.
|
mLogger, state
Constructor and Description |
---|
DelegatingCache() |
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() |
Cache<DelegatingCache.Key<K>,DelegatingCache.Item<V>> |
getCache() |
String |
getCacheType() |
long |
getClears() |
long |
getExpires() |
double |
getHitRatio() |
long |
getHits() |
Iterable<K> |
getKeys()
Returns all the keys in the cache
|
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
|
int |
getSize()
Returns the current size of the cache.
|
Number |
getStatistic(String pStatisticKey)
The different statistics metrics of the implementing class.
|
Set<String> |
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> pFilter)
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 |
setCache(Cache<DelegatingCache.Key<K>,DelegatingCache.Item<V>> pCache) |
void |
setCacheType(String pType)
Sets the type of
Cache this is, will be returned by getCacheType() |
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 static final String CLASS_VERSION
public String getCacheType()
getCacheType
in interface com.escenic.jmx.CacheMXBean
getCacheType
in interface Cache<K,V>
public void setCacheType(String pType)
Cache
this is, will be returned by getCacheType()
pType
- the name of this Cache
public void setMaxSize(int pMaxSize)
setMaxSize
in interface com.escenic.jmx.CacheMXBean
public int getMaxSize()
Cache
getMaxSize
in interface com.escenic.jmx.CacheMXBean
getMaxSize
in interface Cache<K,V>
-1
, as we don't set our own size but delegate to getCache()
public void setValidSeconds(int pValidSeconds)
setValidSeconds
in interface com.escenic.jmx.CacheMXBean
public int getValidSeconds()
Cache
getValidSeconds
in interface com.escenic.jmx.CacheMXBean
getValidSeconds
in interface Cache<K,V>
-1
, as we don't set our own size but delegate to getCache()
public V getObject(K pKey)
Cache
public boolean hasObject(K pKey)
Cache
public void addObject(K pKey, V pValue)
Cache
public void expireObject(K pKey)
Cache
expireObject
in interface Cache<K,V>
pKey
- The identifier of the object to expire.public void flushCache()
This implementation will always throw an UnsupportedOperationException
flushCache
in interface com.escenic.jmx.CacheMXBean
flushCache
in interface Cache<K,V>
public int getSize()
Cache
getMaxSize
public 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 Set<String> getStatisticKeys()
StatisticsSource
getStatisticKeys
in interface StatisticsSource
Set
of String
keys. It will
contain at least one key.StatisticsSource.getStatistic(String)
public String getReport(String type)
StatusSource
getReport
in interface StatusSource
public void reset()
StatusSource
reset
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
reset
in interface StatusSource
public long getHits()
getHits
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getMisses()
getMisses
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getExpires()
getExpires
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getPushes()
getPushes
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getAdds()
getAdds
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getClears()
getClears
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getRemoves()
getRemoves
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getLastThrow()
getLastThrow
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getAges()
getAges
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getTotalTime()
getTotalTime
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public double getHitRatio()
getHitRatio
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getAverageIdleTimeWhenThrown()
getAverageIdleTimeWhenThrown
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getAverageIdleTimeSinceLastThrow()
getAverageIdleTimeSinceLastThrow
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
public long getTimeSinceLastThrow()
getTimeSinceLastThrow
in interface com.escenic.jmx.CacheStatisticsSourceMXBean
protected void startService() throws IllegalStateException, IllegalArgumentException, Exception
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
IllegalArgumentException
- when getCacheType()
returns null
IllegalArgumentException
- when getCache()
returns null
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
.Exception
- when something goes wrong during startup. GenericService.isServiceRunning()
will return false
and GenericService.isServiceFailed()
will return true
.AbstractNurseryService.doStartService()
protected void stopService() throws IllegalStateException, 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 void setCache(Cache<DelegatingCache.Key<K>,DelegatingCache.Item<V>> pCache)
pCache
- the cache to setpublic Cache<DelegatingCache.Key<K>,DelegatingCache.Item<V>> getCache()
public void remove(CacheFilter<K,V> pFilter)
Cache
© 1998- 2021 Escenic AS