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.jcache;
18
19
20 import net.sf.ehcache.Statistics;
21
22 import net.sf.jsr107cache.CacheStatistics;
23 import java.io.Serializable;
24
25 /**
26 * A jsr107 CacheStatistics decorator for an ehcache Statistics class.
27 *
28 * An immutable Cache statistics implementation}
29 * <p/>
30 * This is like a value object, with the added ability to clear cache statistics on the cache.
31 * That ability does not survive any Serialization of this class. On deserialization the cache
32 * can be considered disconnected.
33 * <p/>
34 * The accuracy of these statistics are determined by the value of {#getStatisticsAccuracy()}
35 * at the time the statistic was computed. This can be changed by setting {@link net.sf.ehcache.Cache#setStatisticsAccuracy}.
36 * <p/>
37 * Because this class maintains a reference to an Ehcache, any references held to this class will precent the Ehcache
38 * from getting garbage collected.
39 *
40 * @author Greg Luck
41 * @version $Id: JCacheStatistics.java 512 2007-07-10 09:18:45Z gregluck $
42 */
43 public class JCacheStatistics implements CacheStatistics, Serializable {
44
45
46 private Statistics statistics;
47
48 /**
49 * Constructs an object from an ehcache statistics object
50 *
51 * @param statistics the Statistics object this object decorates.
52 */
53 public JCacheStatistics(Statistics statistics) {
54 this.statistics = statistics;
55 }
56
57 /**
58 * Accurately measuring statistics can be expensive. Returns the current accuracy setting used
59 * in the creation of these statistics.
60 *
61 * @return one of {@link #STATISTICS_ACCURACY_BEST_EFFORT}, {@link #STATISTICS_ACCURACY_GUARANTEED}, {@link #STATISTICS_ACCURACY_NONE}
62 */
63 public int getStatisticsAccuracy() {
64 return statistics.getStatisticsAccuracy();
65 }
66
67 /**
68 * Clears the statistic counters to 0 for the associated Cache.
69 */
70 public void clearStatistics() {
71 statistics.clearStatistics();
72 }
73
74 /**
75 * The number of times a requested item was found in the cache.
76 * <p/>
77 * Warning. This statistic is recorded as a long. If the statistic is large than Integer#MAX_VALUE
78 * precision will be lost.
79 * @return the number of times a requested item was found in the cache
80 */
81 public int getCacheHits() {
82 return (int) statistics.getCacheHits();
83 }
84
85 /**
86 * Warning. This statistic is recorded as a long. If the statistic is large than Integer#MAX_VALUE
87 * precision will be lost.
88 * @return the number of times a requested element was not found in the cache
89 */
90 public int getCacheMisses() {
91 return (int) statistics.getCacheMisses();
92
93 }
94
95 /**
96 * Gets the number of elements stored in the cache. Caclulating this can be expensive. Accordingly,
97 * this method will return three different values, depending on the statistics accuracy setting.
98 * <h3>Best Effort Size</h3>
99 * This result is returned when the statistics accuracy setting is {@link Statistics#STATISTICS_ACCURACY_BEST_EFFORT}.
100 * <p/>
101 * The size is the number of {@link net.sf.ehcache.Element}s in the {@link net.sf.ehcache.store.MemoryStore} plus
102 * the number of {@link net.sf.ehcache.Element}s in the {@link net.sf.ehcache.store.DiskStore}.
103 * <p/>
104 * This number is the actual number of elements, including expired elements that have
105 * not been removed. Any duplicates between stores are accounted for.
106 * <p/>
107 * Expired elements are removed from the the memory store when
108 * getting an expired element, or when attempting to spool an expired element to
109 * disk.
110 * <p/>
111 * Expired elements are removed from the disk store when getting an expired element,
112 * or when the expiry thread runs, which is once every five minutes.
113 * <p/>
114 * <h3>Guaranteed Accuracy Size</h3>
115 * This result is returned when the statistics accuracy setting is {@link Statistics#STATISTICS_ACCURACY_GUARANTEED}.
116 * <p/>
117 * This method accounts for elements which might be expired or duplicated between stores. It take approximately
118 * 200ms per 1000 elements to execute.
119 * <h3>Fast but non-accurate Size</h3>
120 * This result is returned when the statistics accuracy setting is {@link #STATISTICS_ACCURACY_NONE}.
121 * <p/>
122 * The number given may contain expired elements. In addition if the DiskStore is used it may contain some double
123 * counting of elements. It takes 6ms for 1000 elements to execute. Time to execute is O(log n). 50,000 elements take
124 * 36ms.
125 *
126 * @return the number of elements in the ehcache, with a varying degree of accuracy, depending on accuracy setting.
127 */
128 public int getObjectCount() {
129 return (int) statistics.getObjectCount();
130 }
131
132 }