ehcache

net.sf.ehcache.statistics
Class LiveCacheStatisticsImpl

java.lang.Object
  extended by net.sf.ehcache.statistics.LiveCacheStatisticsImpl
All Implemented Interfaces:
java.lang.Cloneable, CacheEventListener, LiveCacheStatistics, LiveCacheStatisticsData

public class LiveCacheStatisticsImpl
extends java.lang.Object
implements LiveCacheStatistics, LiveCacheStatisticsData

Implementation which can be used both as a LiveCacheStatistics and LiveCacheStatisticsData

Since:
1.7
Author:
Abhishek Sanoujam

Constructor Summary
LiveCacheStatisticsImpl(Ehcache cache)
          Constructor that accepts the backing Ehcache, needed for getSize()
 
Method Summary
 void addGetTimeMillis(long millis)
          Adds time taken for a get operation in the cache
 void cacheHitInMemory()
          Called on a cache hit in the MemoryStore
 void cacheHitOnDisk()
          Called on a cache hit in the DiskStore
 void cacheMissExpired()
          Called when an element is found in the cache but already expired
 void cacheMissNotFound()
          Called when an element is not found in the cache
 void clearStatistics()
          Clears statistics of this cache
 java.lang.Object clone()
          Creates a clone of this listener.
 void dispose()
          Give the listener a chance to cleanup and free resources when no longer needed
 float getAverageGetTimeMillis()
          Average time in milli seconds taken to get an element from the cache.
 long getCacheHitCount()
          The number of times a requested item was found in the cache.
 long getCacheMissCount()
          
 long getCacheMissCountExpired()
          
 java.lang.String getCacheName()
          
 long getEvictedCount()
          Number of elements evicted from the cache
 long getExpiredCount()
          Number of elements expired since creation or last clear
 long getInMemoryHitCount()
          Number of times a requested item was found in the Memory Store.
 long getInMemorySize()
          Number of elements in the MemoryStore
 long getMaxGetTimeMillis()
          Return maximum time taken for a get operation in the cache in milliseconds
 long getMinGetTimeMillis()
          Return minimum time taken for a get operation in the cache in milliseconds
 long getOnDiskHitCount()
          Number of times a requested item was found in the Disk Store.
 long getOnDiskSize()
          Number of elements in the DiskStore
 long getPutCount()
          Number of puts that has happened in the cache
 long getRemovedCount()
          Number of elements removed since creation or last clear
 long getSize()
          Size of the cache based on current accuracy settings.
 int getStatisticsAccuracy()
          Accurately measuring statistics can be expensive.
 java.lang.String getStatisticsAccuracyDescription()
          Accurately measuring statistics can be expensive.
 long getUpdateCount()
          Number of updates that as happened in the cache
 boolean isStatisticsEnabled()
          Returns true if statistics is enabled
 void notifyElementEvicted(Ehcache cache, Element element)
          Called immediately after an element is evicted from the cache.
 void notifyElementExpired(Ehcache cache, Element element)
          Called immediately after an element is found to be expired.
 void notifyElementPut(Ehcache cache, Element element)
          Called immediately after an element has been put into the cache.
 void notifyElementRemoved(Ehcache cache, Element element)
          Called immediately after an attempt to remove an element.
 void notifyElementUpdated(Ehcache cache, Element element)
          Called immediately after an element has been put into the cache and the element already existed in the cache.
 void notifyRemoveAll(Ehcache cache)
          Called during Ehcache.removeAll() to indicate that the all elements have been removed from the cache in a bulk operation.
 void registerCacheUsageListener(CacheUsageListener cacheUsageListener)
          Registers a CacheUsageListener which will be notified of the cache usage.
 void removeCacheUsageListener(CacheUsageListener cacheUsageListener)
          Remove an already registered CacheUsageListener, if any.
 void setStatisticsAccuracy(int statisticsAccuracy)
          Sets the statistics accuracy.
 void setStatisticsEnabled(boolean enableStatistics)
          Enabled/Disabled statistics
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LiveCacheStatisticsImpl

public LiveCacheStatisticsImpl(Ehcache cache)
Constructor that accepts the backing Ehcache, needed for getSize()

Parameters:
cache -
Method Detail

clearStatistics

public void clearStatistics()
Clears statistics of this cache

Specified by:
clearStatistics in interface LiveCacheStatistics
Specified by:
clearStatistics in interface LiveCacheStatisticsData

isStatisticsEnabled

public boolean isStatisticsEnabled()
Returns true if statistics is enabled

Specified by:
isStatisticsEnabled in interface LiveCacheStatistics
Returns:
true if statistics is enabled

setStatisticsEnabled

public void setStatisticsEnabled(boolean enableStatistics)
Enabled/Disabled statistics

Specified by:
setStatisticsEnabled in interface LiveCacheStatisticsData

addGetTimeMillis

public void addGetTimeMillis(long millis)
Adds time taken for a get operation in the cache

Specified by:
addGetTimeMillis in interface LiveCacheStatisticsData

cacheHitInMemory

public void cacheHitInMemory()
Called on a cache hit in the MemoryStore

Specified by:
cacheHitInMemory in interface LiveCacheStatisticsData

cacheHitOnDisk

public void cacheHitOnDisk()
Called on a cache hit in the DiskStore

Specified by:
cacheHitOnDisk in interface LiveCacheStatisticsData

cacheMissExpired

public void cacheMissExpired()
Called when an element is found in the cache but already expired

Specified by:
cacheMissExpired in interface LiveCacheStatisticsData

cacheMissNotFound

public void cacheMissNotFound()
Called when an element is not found in the cache

Specified by:
cacheMissNotFound in interface LiveCacheStatisticsData

setStatisticsAccuracy

public void setStatisticsAccuracy(int statisticsAccuracy)
Sets the statistics accuracy.

Specified by:
setStatisticsAccuracy in interface LiveCacheStatisticsData
Parameters:
statisticsAccuracy - one of Statistics#STATISTICS_ACCURACY_BEST_EFFORT, Statistics#STATISTICS_ACCURACY_GUARANTEED, Statistics#STATISTICS_ACCURACY_NONE

dispose

public void dispose()
Give the listener a chance to cleanup and free resources when no longer needed

Specified by:
dispose in interface CacheEventListener

notifyElementEvicted

public void notifyElementEvicted(Ehcache cache,
                                 Element element)
Called immediately after an element is evicted from the cache. Evicted in this sense means evicted from one store and not moved to another, so that it exists nowhere in the local cache.

In a sense the Element has been removed from the cache, but it is different, thus the separate notification.

Specified by:
notifyElementEvicted in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element that has just been evicted

notifyElementExpired

public void notifyElementExpired(Ehcache cache,
                                 Element element)
Called immediately after an element is found to be expired. The Cache.remove(Object) method will block until this method returns.

As the Element has been expired, only what was the key of the element is known.

Elements are checked for expiry in ehcache at the following times:

If an element is found to be expired, it is deleted and this method is notified.

Specified by:
notifyElementExpired in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element that has just expired

Deadlock Warning: expiry will often come from the DiskStore expiry thread. It holds a lock to the DiskStorea the time the notification is sent. If the implementation of this method calls into a synchronized Cache method and that subsequently calls into DiskStore a deadlock will result. Accordingly implementers of this method should not call back into Cache.


notifyElementPut

public void notifyElementPut(Ehcache cache,
                             Element element)
                      throws CacheException
Called immediately after an element has been put into the cache. The Cache.put(net.sf.ehcache.Element) method will block until this method returns.

Implementers may wish to have access to the Element's fields, including value, so the element is provided. Implementers should be careful not to modify the element. The effect of any modifications is undefined.

Specified by:
notifyElementPut in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element which was just put into the cache.
Throws:
CacheException

notifyElementRemoved

public void notifyElementRemoved(Ehcache cache,
                                 Element element)
                          throws CacheException
Called immediately after an attempt to remove an element. The remove method will block until this method returns.

This notification is received regardless of whether the cache had an element matching the removal key or not. If an element was removed, the element is passed to this method, otherwise a synthetic element, with only the key set is passed in.

This notification is not called for the following special cases:

  1. removeAll was called. See CacheEventListener.notifyRemoveAll(net.sf.ehcache.Ehcache)
  2. An element was evicted from the cache. See CacheEventListener.notifyElementEvicted(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)

Specified by:
notifyElementRemoved in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element just deleted, or a synthetic element with just the key set if no element was removed.
Throws:
CacheException

notifyElementUpdated

public void notifyElementUpdated(Ehcache cache,
                                 Element element)
                          throws CacheException
Called immediately after an element has been put into the cache and the element already existed in the cache. This is thus an update.

The Cache.put(net.sf.ehcache.Element) method will block until this method returns.

Implementers may wish to have access to the Element's fields, including value, so the element is provided. Implementers should be careful not to modify the element. The effect of any modifications is undefined.

Specified by:
notifyElementUpdated in interface CacheEventListener
Parameters:
cache - the cache emitting the notification
element - the element which was just put into the cache.
Throws:
CacheException

notifyRemoveAll

public void notifyRemoveAll(Ehcache cache)
Called during Ehcache.removeAll() to indicate that the all elements have been removed from the cache in a bulk operation. The usual CacheEventListener.notifyElementRemoved(net.sf.ehcache.Ehcache, net.sf.ehcache.Element) is not called.

This notification exists because clearing a cache is a special case. It is often not practical to serially process notifications where potentially millions of elements have been bulk deleted.

Specified by:
notifyRemoveAll in interface CacheEventListener
Parameters:
cache - the cache emitting the notification

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Creates a clone of this listener. This method will only be called by ehcache before a cache is initialized.

This may not be possible for listeners after they have been initialized. Implementations should throw CloneNotSupportedException if they do not support clone.

Specified by:
clone in interface CacheEventListener
Overrides:
clone in class java.lang.Object
Returns:
a clone
Throws:
java.lang.CloneNotSupportedException - if the listener could not be cloned.

getAverageGetTimeMillis

public float getAverageGetTimeMillis()
Average time in milli seconds taken to get an element from the cache.

Specified by:
getAverageGetTimeMillis in interface LiveCacheStatistics
Returns:
Average time taken for a get operation in milliseconds

registerCacheUsageListener

public void registerCacheUsageListener(CacheUsageListener cacheUsageListener)
                                throws java.lang.IllegalStateException
Registers a CacheUsageListener which will be notified of the cache usage. Implementations of CacheUsageListener should override the Object.equals(Object) and Object.hashCode() methods as it is used for equality check

Specified by:
registerCacheUsageListener in interface LiveCacheStatisticsData
Throws:
java.lang.IllegalStateException

removeCacheUsageListener

public void removeCacheUsageListener(CacheUsageListener cacheUsageListener)
                              throws java.lang.IllegalStateException
Remove an already registered CacheUsageListener, if any. Depends on the Object.equals(Object) method.

Specified by:
removeCacheUsageListener in interface LiveCacheStatisticsData
Throws:
java.lang.IllegalStateException

getCacheHitCount

public long getCacheHitCount()
The number of times a requested item was found in the cache.

Specified by:
getCacheHitCount in interface LiveCacheStatistics
Returns:
the number of times a requested item was found in the cache

getCacheMissCount

public long getCacheMissCount()

Specified by:
getCacheMissCount in interface LiveCacheStatistics
Returns:
the number of times a requested element was not found in the cache

getCacheMissCountExpired

public long getCacheMissCountExpired()

Specified by:
getCacheMissCountExpired in interface LiveCacheStatistics
Returns:
the number of times a requested element was not found in the cache and the reason being the element already expired

getEvictedCount

public long getEvictedCount()
Number of elements evicted from the cache

Specified by:
getEvictedCount in interface LiveCacheStatistics
Returns:
Number of elements evicted from the cache

getInMemoryHitCount

public long getInMemoryHitCount()
Number of times a requested item was found in the Memory Store.

Specified by:
getInMemoryHitCount in interface LiveCacheStatistics
Returns:
the number of times a requested item was found in memory

getOnDiskHitCount

public long getOnDiskHitCount()
Number of times a requested item was found in the Disk Store.

Specified by:
getOnDiskHitCount in interface LiveCacheStatistics
Returns:
the number of times a requested item was found on Disk, or 0 if there is no disk storage configured.

getSize

public long getSize()
Size of the cache based on current accuracy settings.

Specified by:
getSize in interface LiveCacheStatistics
Returns:
The size of the cache based on currect accuracy setting

getInMemorySize

public long getInMemorySize()
Number of elements in the MemoryStore

Specified by:
getInMemorySize in interface LiveCacheStatistics
Returns:
the number of elements in memory

getOnDiskSize

public long getOnDiskSize()
Number of elements in the DiskStore

Specified by:
getOnDiskSize in interface LiveCacheStatistics
Returns:
number of elements on disk

getCacheName

public java.lang.String getCacheName()

Specified by:
getCacheName in interface LiveCacheStatistics
Returns:
the name of the Ehcache

getStatisticsAccuracy

public int getStatisticsAccuracy()
Accurately measuring statistics can be expensive. Returns the current accuracy setting.

Specified by:
getStatisticsAccuracy in interface LiveCacheStatistics
Returns:
one of Statistics.STATISTICS_ACCURACY_BEST_EFFORT, Statistics.STATISTICS_ACCURACY_GUARANTEED, Statistics.STATISTICS_ACCURACY_NONE

getExpiredCount

public long getExpiredCount()
Number of elements expired since creation or last clear

Specified by:
getExpiredCount in interface LiveCacheStatistics
Returns:
Number of expired elements

getPutCount

public long getPutCount()
Number of puts that has happened in the cache

Specified by:
getPutCount in interface LiveCacheStatistics
Returns:
Number of puts

getRemovedCount

public long getRemovedCount()
Number of elements removed since creation or last clear

Specified by:
getRemovedCount in interface LiveCacheStatistics
Returns:
Number of elements removed

getUpdateCount

public long getUpdateCount()
Number of updates that as happened in the cache

Specified by:
getUpdateCount in interface LiveCacheStatistics
Returns:
Number of updates

getStatisticsAccuracyDescription

public java.lang.String getStatisticsAccuracyDescription()
Accurately measuring statistics can be expensive. Returns the current accuracy setting.

Specified by:
getStatisticsAccuracyDescription in interface LiveCacheStatistics
Returns:
a human readable description of the accuracy setting. One of "None", "Best Effort" or "Guaranteed".

getMaxGetTimeMillis

public long getMaxGetTimeMillis()
Return maximum time taken for a get operation in the cache in milliseconds

Specified by:
getMaxGetTimeMillis in interface LiveCacheStatistics
Returns:
maximum time taken for a get operation in the cache in milliseconds
See Also:
LiveCacheStatistics.getMaxGetTimeMillis()

getMinGetTimeMillis

public long getMinGetTimeMillis()
Return minimum time taken for a get operation in the cache in milliseconds

Specified by:
getMinGetTimeMillis in interface LiveCacheStatistics
Returns:
minimum time taken for a get operation in the cache in milliseconds
See Also:
LiveCacheStatistics.getMinGetTimeMillis()

ehcache

true