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.management;
18  
19  import net.sf.ehcache.CacheException;
20  import net.sf.ehcache.Ehcache;
21  import net.sf.ehcache.Statistics;
22  
23  import javax.management.MalformedObjectNameException;
24  import javax.management.ObjectName;
25  
26  
27  /**
28   * A JMX CacheStatistics decorator for an ehcache Statistics class.
29   * <p/>
30   * An immutable Cache statistics implementation}
31   * <p/>
32   * This is like a value object, with the added ability to clear cache statistics on the cache.
33   * That ability does not survive any Serialization of this class. On deserialization the cache
34   * can be considered disconnected.
35   * <p/>
36   * The accuracy of these statistics are determined by the value of {#getStatisticsAccuracy()}
37   * at the time the statistic was computed. This can be changed by setting {@link net.sf.ehcache.Cache#setStatisticsAccuracy}.
38   * <p/>
39   * Because this class maintains a reference to an Ehcache, any references held to this class will precent the Ehcache
40   * from getting garbage collected.
41   *
42   * @author Greg Luck
43   * @version $Id: CacheStatistics.java 512 2007-07-10 09:18:45Z gregluck $
44   * @since 1.3
45   */
46  public class CacheStatistics implements CacheStatisticsMBean {
47  
48      private static final long serialVersionUID = 8085302752781762030L;
49  
50      private Ehcache ehcache;
51      private Statistics statistics;
52  
53      private ObjectName objectName;
54      private long lastUpdated;
55  
56      /**
57       * Constructs an object from an ehcache statistics object
58       *
59       * @param ehcache the backing ehcache
60       */
61      public CacheStatistics(Ehcache ehcache) {
62          this.ehcache = ehcache;
63          objectName = createObjectName(ehcache.getCacheManager().getName(),
64                  ehcache.getName());
65      }
66  
67      /**
68       * Creates an object name using the scheme "net.sf.ehcache:type=CacheStatistics,CacheManager=<cacheManagerName>,name=<cacheName>"
69       */
70      static ObjectName createObjectName(String cacheManagerName, String cacheName) {
71          ObjectName objectName;
72          try {
73              objectName = new ObjectName("net.sf.ehcache:type=CacheStatistics,CacheManager="
74                      + cacheManagerName + ",name=" + cacheName);
75          } catch (MalformedObjectNameException e) {
76              throw new CacheException(e);
77          }
78          return objectName;
79      }
80  
81  
82      /**
83       * Accurately measuring statistics can be expensive. Returns the current accuracy setting used
84       * in the creation of these statistics.
85       *
86       * @return one of {@link Statistics#STATISTICS_ACCURACY_BEST_EFFORT}, {@link Statistics#STATISTICS_ACCURACY_GUARANTEED}, {@link Statistics#STATISTICS_ACCURACY_NONE}
87       */
88      public int getStatisticsAccuracy() {
89          updateIfNeeded();
90          return statistics.getStatisticsAccuracy();
91      }
92  
93      private void updateIfNeeded() {
94          if (System.currentTimeMillis() != lastUpdated) {
95              statistics = ehcache.getStatistics();
96              lastUpdated = System.currentTimeMillis();
97          }
98      }
99  
100     /**
101      * Accurately measuring statistics can be expensive. Returns the current accuracy setting.
102      * @return a human readable description of the accuracy setting. One of "None", "Best Effort" or "Guaranteed".
103      */
104     public String getStatisticsAccuracyDescription() {
105         updateIfNeeded();
106         return statistics.getStatisticsAccuracyDescription();
107     }
108 
109     /**
110      * @return the name of the Ehcache, or null is there no associated cache
111      */
112     public String getAssociatedCacheName() {
113         return statistics.getAssociatedCacheName();
114     }
115 
116     /**
117      * Clears the statistic counters to 0 for the associated Cache.
118      */
119     public void clearStatistics() {
120         statistics.clearStatistics();
121     }
122 
123     /**
124      * The number of times a requested item was found in the cache.
125      * <p/>
126      * Warning. This statistic is recorded as a long. If the statistic is large than Integer#MAX_VALUE
127      * precision will be lost.
128      *
129      * @return the number of times a requested item was found in the cache
130      */
131     public long getCacheHits() {
132         updateIfNeeded();
133         return statistics.getCacheHits();
134     }
135 
136     /**
137      * Number of times a requested item was found in the Memory Store.
138      *
139      * @return the number of times a requested item was found in memory
140      */
141     public long getInMemoryHits() {
142         updateIfNeeded();
143         return statistics.getInMemoryHits();
144     }
145 
146     /**
147      * Number of times a requested item was found in the Disk Store.
148      *
149      * @return the number of times a requested item was found on Disk, or 0 if there is no disk storage configured.
150      */
151     public long getOnDiskHits() {
152         updateIfNeeded();
153         return statistics.getOnDiskHits();
154     }
155 
156     /**
157      * Warning. This statistic is recorded as a long. If the statistic is large than Integer#MAX_VALUE
158      * precision will be lost.
159      *
160      * @return the number of times a requested element was not found in the cache
161      */
162     public long getCacheMisses() {
163         updateIfNeeded();
164         return statistics.getCacheMisses();
165 
166     }
167 
168     /**
169      * Gets the number of elements stored in the cache. Caclulating this can be expensive. Accordingly,
170      * this method will return three different values, depending on the statistics accuracy setting.
171      * <h3>Best Effort Size</h3>
172      * This result is returned when the statistics accuracy setting is {@link net.sf.ehcache.Statistics#STATISTICS_ACCURACY_BEST_EFFORT}.
173      * <p/>
174      * The size is the number of {@link net.sf.ehcache.Element}s in the {@link net.sf.ehcache.store.MemoryStore} plus
175      * the number of {@link net.sf.ehcache.Element}s in the {@link net.sf.ehcache.store.DiskStore}.
176      * <p/>
177      * This number is the actual number of elements, including expired elements that have
178      * not been removed. Any duplicates between stores are accounted for.
179      * <p/>
180      * Expired elements are removed from the the memory store when
181      * getting an expired element, or when attempting to spool an expired element to
182      * disk.
183      * <p/>
184      * Expired elements are removed from the disk store when getting an expired element,
185      * or when the expiry thread runs, which is once every five minutes.
186      * <p/>
187      * <h3>Guaranteed Accuracy Size</h3>
188      * This result is returned when the statistics accuracy setting is {@link net.sf.ehcache.Statistics#STATISTICS_ACCURACY_GUARANTEED}.
189      * <p/>
190      * This method accounts for elements which might be expired or duplicated between stores. It take approximately
191      * 200ms per 1000 elements to execute.
192      * <h3>Fast but non-accurate Size</h3>
193      * This result is returned when the statistics accuracy setting is {@link Statistics#STATISTICS_ACCURACY_NONE}.
194      * <p/>
195      * The number given may contain expired elements. In addition if the DiskStore is used it may contain some double
196      * counting of elements. It takes 6ms for 1000 elements to execute. Time to execute is O(log n). 50,000 elements take
197      * 36ms.
198      *
199      * @return the number of elements in the ehcache, with a varying degree of accuracy, depending on accuracy setting.
200      */
201     public long getObjectCount() {
202         updateIfNeeded();
203         return statistics.getObjectCount();
204     }
205 
206 
207     /**
208      * @return the object name for this MBean
209      */
210     ObjectName getObjectName() {
211         return objectName;
212     }
213 
214 }