net.sf.ehcache

Class Statistics

public class Statistics extends Object implements Serializable

An immutable Cache statistics implementation}

This is like a value object, with the added ability to clear cache statistics on the cache. That ability does not survive any Serialization of this class. On deserialization the cache can be considered disconnected.

The accuracy of these statistics are determined by the value of {#getStatisticsAccuracy()} at the time the statistic was computed. This can be changed by setting Cache.

Because this class maintains a reference to an Ehcache, any references held to this class will precent the Ehcache from getting garbage collected.

Version: $Id: Statistics.java 191 2006-09-03 22:41:48Z gregluck $

Author: Greg Luck

Field Summary
static intSTATISTICS_ACCURACY_BEST_EFFORT
Best efforts accuracy setting.
static intSTATISTICS_ACCURACY_GUARANTEED
Guaranteed accuracy setting.
static intSTATISTICS_ACCURACY_NONE
Fast but not accurate setting.
Constructor Summary
Statistics(Ehcache cache, int statisticsAccuracy, int cacheHits, int onDiskHits, int inMemoryHits, int misses, int size)
Creates a new statistics object, associated with a Cache
Method Summary
voidclearStatistics()
Clears the statistic counters to 0 for the associated Cache.
StringgetAssociatedCacheName()
intgetCacheHits()
The number of times a requested item was found in the cache.
intgetCacheMisses()
intgetInMemoryHits()
Number of times a requested item was found in the Memory Store.
intgetObjectCount()
Gets the number of elements stored in the cache.
intgetOnDiskHits()
Number of times a requested item was found in the Disk Store.
intgetStatisticsAccuracy()
Accurately measuring statistics can be expensive.
StringtoString()
Returns a String representation of the Ehcache statistics.

Field Detail

STATISTICS_ACCURACY_BEST_EFFORT

public static final int STATISTICS_ACCURACY_BEST_EFFORT
Best efforts accuracy setting.

STATISTICS_ACCURACY_GUARANTEED

public static final int STATISTICS_ACCURACY_GUARANTEED
Guaranteed accuracy setting.

STATISTICS_ACCURACY_NONE

public static final int STATISTICS_ACCURACY_NONE
Fast but not accurate setting.

Constructor Detail

Statistics

public Statistics(Ehcache cache, int statisticsAccuracy, int cacheHits, int onDiskHits, int inMemoryHits, int misses, int size)
Creates a new statistics object, associated with a Cache

Parameters: cache The cache that clearStatistics will call, if not disconnected statisticsAccuracy cacheHits onDiskHits inMemoryHits misses size

Method Detail

clearStatistics

public void clearStatistics()
Clears the statistic counters to 0 for the associated Cache.

getAssociatedCacheName

public String getAssociatedCacheName()

Returns: the name of the Ehcache, or null is there no associated cache

getCacheHits

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

Returns: the number of times a requested item was found in the cache

getCacheMisses

public int getCacheMisses()

Returns: the number of times a requested element was not found in the cache

getInMemoryHits

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

Returns: the number of times a requested item was found in memory

getObjectCount

public int getObjectCount()
Gets the number of elements stored in the cache. Caclulating this can be expensive. Accordingly, this method will return three different values, depending on the statistics accuracy setting.

Best Effort Size

This result is returned when the statistics accuracy setting is STATISTICS_ACCURACY_BEST_EFFORT.

The size is the number of Elements in the MemoryStore plus the number of Elements in the DiskStore.

This number is the actual number of elements, including expired elements that have not been removed. Any duplicates between stores are accounted for.

Expired elements are removed from the the memory store when getting an expired element, or when attempting to spool an expired element to disk.

Expired elements are removed from the disk store when getting an expired element, or when the expiry thread runs, which is once every five minutes.

Guaranteed Accuracy Size

This result is returned when the statistics accuracy setting is STATISTICS_ACCURACY_GUARANTEED.

This method accounts for elements which might be expired or duplicated between stores. It take approximately 200ms per 1000 elements to execute.

Fast but non-accurate Size

This result is returned when the statistics accuracy setting is STATISTICS_ACCURACY_NONE.

The number given may contain expired elements. In addition if the DiskStore is used it may contain some double counting of elements. It takes 6ms for 1000 elements to execute. Time to execute is O(log n). 50,000 elements take 36ms.

Returns: the number of elements in the ehcache, with a varying degree of accuracy, depending on accuracy setting.

getOnDiskHits

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

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

getStatisticsAccuracy

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

Returns: one of STATISTICS_ACCURACY_BEST_EFFORT, STATISTICS_ACCURACY_GUARANTEED, STATISTICS_ACCURACY_NONE

toString

public final String toString()
Returns a String representation of the Ehcache statistics.