View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.io.Serializable;
23  
24  /**
25   * An immutable Cache statistics implementation}
26   * <p/>
27   * This is like a value object, with the added ability to clear cache statistics on the cache.
28   * That ability does not survive any Serialization of this class. On deserialization the cache
29   * can be considered disconnected.
30   * <p/>
31   * The accuracy of these statistics are determined by the value of {#getStatisticsAccuracy()}
32   * at the time the statistic was computed. This can be changed by setting {@link Cache#setStatisticsAccuracy}.
33   * <p/>
34   * Because this class maintains a reference to an Ehcache, any references held to this class will precent the Ehcache
35   * from getting garbage collected.
36   * <p/>
37   * The {@link #STATISTICS_ACCURACY_BEST_EFFORT}, {@link #STATISTICS_ACCURACY_GUARANTEED} and {@link #STATISTICS_ACCURACY_NONE}
38   * constants have the same values as in JSR107.
39   *
40   * @author Greg Luck
41   * @version $Id: Statistics.java 512 2007-07-10 09:18:45Z gregluck $
42   */
43  public class Statistics implements Serializable {
44  
45      /**
46       * Fast but not accurate setting.
47       */
48      public static final int STATISTICS_ACCURACY_NONE = 0;
49  
50      /**
51       * Best efforts accuracy setting.
52       */
53      public static final int STATISTICS_ACCURACY_BEST_EFFORT = 1;
54  
55      /**
56       * Guaranteed accuracy setting.
57       */
58      public static final int STATISTICS_ACCURACY_GUARANTEED = 2;
59  
60      private static final Log LOG = LogFactory.getLog(Statistics.class.getName());
61  
62      private static final long serialVersionUID = 3606940454221918725L;
63  
64      private transient Ehcache cache;
65  
66      private final int statisticsAccuracy;
67  
68      private final long cacheHits;
69  
70      private final long onDiskHits;
71  
72      private final long inMemoryHits;
73  
74      private final long misses;
75  
76      private final long size;
77  
78  
79      /**
80       * Creates a new statistics object, associated with a Cache
81       *
82       * @param cache              The cache that {@link #clearStatistics()} will call, if not disconnected
83       * @param statisticsAccuracy
84       * @param cacheHits
85       * @param onDiskHits
86       * @param inMemoryHits
87       * @param misses
88       * @param size
89       */
90      public Statistics(Ehcache cache, int statisticsAccuracy, long cacheHits, long onDiskHits, long inMemoryHits,
91                        long misses, long size) {
92          this.statisticsAccuracy = statisticsAccuracy;
93          this.cacheHits = cacheHits;
94          this.onDiskHits = onDiskHits;
95          this.inMemoryHits = inMemoryHits;
96          this.misses = misses;
97          this.cache = cache;
98          this.size = size;
99      }
100 
101     /**
102      * Clears the statistic counters to 0 for the associated Cache.
103      */
104     public void clearStatistics() {
105         if (cache == null) {
106             throw new IllegalStateException("This statistics object no longer references a Cache.");
107         }
108         try {
109             cache.clearStatistics();
110         } catch (IllegalStateException e) {
111             if (LOG.isDebugEnabled()) {
112                 LOG.info("Ignoring call because " + e.getMessage());
113             }
114         }
115     }
116 
117     /**
118      * The number of times a requested item was found in the cache.
119      *
120      * @return the number of times a requested item was found in the cache
121      */
122     public long getCacheHits() {
123             return cacheHits;
124     }
125 
126     /**
127      * Number of times a requested item was found in the Memory Store.
128      *
129      * @return the number of times a requested item was found in memory
130      */
131     public long getInMemoryHits() {
132         return inMemoryHits;
133     }
134 
135     /**
136      * Number of times a requested item was found in the Disk Store.
137      *
138      * @return the number of times a requested item was found on Disk, or 0 if there is no disk storage configured.
139      */
140     public long getOnDiskHits() {
141         return onDiskHits;
142     }
143 
144     /**
145      * @return the number of times a requested element was not found in the cache
146      */
147     public long getCacheMisses() {
148         return misses;
149 
150     }
151 
152     /**
153      * Gets the number of elements stored in the cache. Caclulating this can be expensive. Accordingly,
154      * this method will return three different values, depending on the statistics accuracy setting.
155      * <h3>Best Effort Size</h3>
156      * This result is returned when the statistics accuracy setting is {@link Statistics#STATISTICS_ACCURACY_BEST_EFFORT}.
157      * <p/>
158      * The size is the number of {@link Element}s in the {@link net.sf.ehcache.store.MemoryStore} plus
159      * the number of {@link Element}s in the {@link net.sf.ehcache.store.DiskStore}.
160      * <p/>
161      * This number is the actual number of elements, including expired elements that have
162      * not been removed. Any duplicates between stores are accounted for.
163      * <p/>
164      * Expired elements are removed from the the memory store when
165      * getting an expired element, or when attempting to spool an expired element to
166      * disk.
167      * <p/>
168      * Expired elements are removed from the disk store when getting an expired element,
169      * or when the expiry thread runs, which is once every five minutes.
170      * <p/>
171      * <h3>Guaranteed Accuracy Size</h3>
172      * This result is returned when the statistics accuracy setting is {@link Statistics#STATISTICS_ACCURACY_GUARANTEED}.
173      * <p/>
174      * This method accounts for elements which might be expired or duplicated between stores. It take approximately
175      * 200ms per 1000 elements to execute.
176      * <h3>Fast but non-accurate Size</h3>
177      * This result is returned when the statistics accuracy setting is {@link #STATISTICS_ACCURACY_NONE}.
178      * <p/>
179      * The number given may contain expired elements. In addition if the DiskStore is used it may contain some double
180      * counting of elements. It takes 6ms for 1000 elements to execute. Time to execute is O(log n). 50,000 elements take
181      * 36ms.
182      *
183      * @return the number of elements in the ehcache, with a varying degree of accuracy, depending on accuracy setting.
184      */
185     public long getObjectCount() {
186         return size;
187     }
188 
189     /**
190      * Accurately measuring statistics can be expensive. Returns the current accuracy setting.
191      *
192      * @return one of {@link #STATISTICS_ACCURACY_BEST_EFFORT}, {@link #STATISTICS_ACCURACY_GUARANTEED}, {@link #STATISTICS_ACCURACY_NONE}
193      */
194     public int getStatisticsAccuracy() {
195         return statisticsAccuracy;
196     }
197 
198     /**
199      * Accurately measuring statistics can be expensive. Returns the current accuracy setting.
200      * @return a human readable description of the accuracy setting. One of "None", "Best Effort" or "Guaranteed".
201      */
202     public String getStatisticsAccuracyDescription() {
203         if (statisticsAccuracy == 0) {
204             return "None";
205         } else if (statisticsAccuracy == 1) {
206             return "Best Effort";
207         } else {
208             return "Guaranteed";
209         }
210     }
211 
212     /**
213      * @return the name of the Ehcache, or null if a reference is no longer held to the cache,
214      * as, it would be after deserialization.
215      */
216     public String getAssociatedCacheName() {
217         if (cache != null) {
218             return cache.getName();
219         } else {
220             return null;
221         }
222     }
223 
224     /**
225      * @return the name of the Ehcache, or null if a reference is no longer held to the cache,
226      * as, it would be after deserialization.
227      */
228     public Ehcache getAssociatedCache() {
229         if (cache != null) {
230             return cache;
231         } else {
232             return null;
233         }
234     }
235 
236     /**
237      * Returns a {@link String} representation of the {@link Ehcache} statistics.
238      */
239     public final String toString() {
240         StringBuffer dump = new StringBuffer();
241 
242         dump.append("[ ")
243                 .append(" name = ").append(getAssociatedCacheName())
244                 .append(" cacheHits = ").append(cacheHits)
245                 .append(" onDiskHits = ").append(onDiskHits)
246                 .append(" inMemoryHits = ").append(inMemoryHits)
247                 .append(" misses = ").append(misses)
248                 .append(" size = ").append(size)
249                 .append(" ]");
250 
251         return dump.toString();
252     }
253 
254 }