1 /**
2 * Copyright Terracotta, Inc.
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.statistics;
18
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.Ehcache;
21 import net.sf.ehcache.Element;
22
23 /**
24 * An implementation of {@link LiveCacheStatistics} and also implements {@link LiveCacheStatisticsData}. Uses separate delegates depending
25 * on whether
26 * statistics is enabled or not.
27 * <p />
28 * To collect statistics element put/update/remove/expired data, instances of this class must be registered as a CacheEventListener to a
29 * Cache
30 *
31 * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
32 * @since 1.7
33 */
34 public class LiveCacheStatisticsWrapper implements LiveCacheStatistics, LiveCacheStatisticsData {
35
36 private static final LiveCacheStatistics NULL_LIVE_CACHE_STATISTICS = new NullLiveCacheStatisticsData();
37
38 private final LiveCacheStatisticsImpl liveDelegate;
39 private volatile LiveCacheStatistics delegate;
40
41 /**
42 * Constructor accepting the backing cache.
43 *
44 * @param cache
45 */
46 public LiveCacheStatisticsWrapper(Ehcache cache) {
47 liveDelegate = new LiveCacheStatisticsImpl(cache);
48 // enable statistics by default
49 setStatisticsEnabled(true);
50 }
51
52 /**
53 * {@inheritDoc}
54 *
55 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#setStatisticsEnabled(boolean)
56 */
57 public void setStatisticsEnabled(boolean enableStatistics) {
58 if (enableStatistics) {
59 delegate = liveDelegate;
60 } else {
61 delegate = NULL_LIVE_CACHE_STATISTICS;
62 }
63 }
64
65 /**
66 * {@inheritDoc}
67 *
68 * @see net.sf.ehcache.statistics.LiveCacheStatistics#isStatisticsEnabled()
69 */
70 public boolean isStatisticsEnabled() {
71 return delegate instanceof LiveCacheStatisticsImpl;
72 }
73
74 /**
75 * {@inheritDoc}
76 *
77 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#registerCacheUsageListener(net.sf.ehcache.statistics.CacheUsageListener)
78 */
79 public void registerCacheUsageListener(CacheUsageListener cacheUsageListener) throws IllegalStateException {
80 // always register on the live delegate
81 liveDelegate.registerCacheUsageListener(cacheUsageListener);
82 }
83
84 /**
85 * {@inheritDoc}
86 *
87 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#removeCacheUsageListener(net.sf.ehcache.statistics.CacheUsageListener)
88 */
89 public void removeCacheUsageListener(CacheUsageListener cacheUsageListener) throws IllegalStateException {
90 // always use the live delegate for this
91 liveDelegate.removeCacheUsageListener(cacheUsageListener);
92 }
93
94 /**
95 * {@inheritDoc}
96 *
97 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#setStatisticsAccuracy(int)
98 */
99 public void setStatisticsAccuracy(int statisticsAccuracy) {
100 // always use the live delegate for this
101 liveDelegate.setStatisticsAccuracy(statisticsAccuracy);
102 }
103
104 /**
105 * {@inheritDoc}
106 *
107 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getStatisticsAccuracy()
108 */
109 public int getStatisticsAccuracy() {
110 // always use live delegate for this
111 return liveDelegate.getStatisticsAccuracy();
112 }
113
114 /**
115 * {@inheritDoc}
116 *
117 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getStatisticsAccuracyDescription()
118 */
119 public String getStatisticsAccuracyDescription() {
120 // always use live delegate for this
121 return liveDelegate.getStatisticsAccuracyDescription();
122 }
123
124 /**
125 * {@inheritDoc}
126 *
127 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheName()
128 */
129 public String getCacheName() {
130 // always use the live delegate for this
131 return liveDelegate.getCacheName();
132 }
133
134 /**
135 * {@inheritDoc}
136 *
137 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getSize()
138 */
139 public long getSize() {
140 return delegate.getSize();
141 }
142
143 /**
144 * {@inheritDoc}
145 *
146 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getInMemorySize()
147 * @deprecated see {@link #getLocalHeapSize()}
148 */
149 @Deprecated
150 public long getInMemorySize() {
151 return delegate.getInMemorySize();
152 }
153
154 /**
155 * {@inheritDoc}
156 *
157 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOffHeapSize()
158 * @deprecated see {@link #getLocalOffHeapSize()}
159 */
160 @Deprecated
161 public long getOffHeapSize() {
162 return delegate.getOffHeapSize();
163 }
164
165 /**
166 * {@inheritDoc}
167 *
168 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOnDiskSize()
169 * @deprecated see {@link #getLocalDiskSize()}
170 */
171 @Deprecated
172 public long getOnDiskSize() {
173 return delegate.getOnDiskSize();
174 }
175
176 /**
177 * {@inheritDoc}
178 */
179 public long getLocalHeapSize() {
180 return delegate.getLocalHeapSize();
181 }
182
183 /**
184 * {@inheritDoc}
185 */
186 public long getLocalOffHeapSize() {
187 return delegate.getLocalOffHeapSize();
188 }
189
190 /**
191 * {@inheritDoc}
192 */
193 public long getLocalDiskSize() {
194 return delegate.getLocalDiskSize();
195 }
196
197 /**
198 * Returns the usage of local heap in bytes
199 * @return memory usage in bytes
200 */
201 public long getLocalHeapSizeInBytes() {
202 return delegate.getLocalHeapSizeInBytes();
203 }
204
205 /**
206 * Returns the usage of local off heap in bytes
207 * @return memory usage in bytes
208 */
209 public long getLocalOffHeapSizeInBytes() {
210 return delegate.getLocalOffHeapSizeInBytes();
211 }
212
213 /**
214 * Returns the usage of local disks in bytes
215 * @return memory usage in bytes
216 */
217 public long getLocalDiskSizeInBytes() {
218 return delegate.getLocalDiskSizeInBytes();
219 }
220
221 /**
222 * {@inheritDoc}
223 *
224 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getAverageGetTimeMillis()
225 */
226 public float getAverageGetTimeMillis() {
227 return delegate.getAverageGetTimeMillis();
228 }
229
230 /**
231 * {@inheritDoc}
232 *
233 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getAverageGetTimeNanos()
234 */
235 public long getAverageGetTimeNanos() {
236 return delegate.getAverageGetTimeNanos();
237 }
238
239 /**
240 * {@inheritDoc}
241 *
242 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheHitCount()
243 */
244 public long getCacheHitCount() {
245 return delegate.getCacheHitCount();
246 }
247
248 /**
249 * {@inheritDoc}
250 *
251 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheMissCount()
252 */
253 public long getCacheMissCount() {
254 return delegate.getCacheMissCount();
255 }
256
257 /**
258 * {@inheritDoc}
259 *
260 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getInMemoryMissCount()
261 */
262 public long getInMemoryMissCount() {
263 return delegate.getInMemoryMissCount();
264 }
265
266 /**
267 * {@inheritDoc}
268 *
269 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOffHeapMissCount()
270 */
271 public long getOffHeapMissCount() {
272 return delegate.getOffHeapMissCount();
273 }
274
275 /**
276 * {@inheritDoc}
277 *
278 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOnDiskMissCount()
279 */
280 public long getOnDiskMissCount() {
281 return delegate.getOnDiskMissCount();
282 }
283
284 /**
285 * {@inheritDoc}
286 *
287 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheMissCountExpired()
288 */
289 public long getCacheMissCountExpired() {
290 return delegate.getCacheMissCountExpired();
291 }
292
293 /**
294 * {@inheritDoc}
295 *
296 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheHitRatio()
297 */
298 public int getCacheHitRatio() {
299 return delegate.getCacheHitRatio();
300 }
301
302 /**
303 * {@inheritDoc}
304 *
305 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getEvictedCount()
306 */
307 public long getEvictedCount() {
308 return delegate.getEvictedCount();
309 }
310
311 /**
312 * {@inheritDoc}
313 *
314 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getExpiredCount()
315 */
316 public long getExpiredCount() {
317 return delegate.getExpiredCount();
318 }
319
320 /**
321 * {@inheritDoc}
322 *
323 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getInMemoryHitCount()
324 */
325 public long getInMemoryHitCount() {
326 return delegate.getInMemoryHitCount();
327 }
328
329 /**
330 * {@inheritDoc}
331 *
332 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOffHeapHitCount()
333 */
334 public long getOffHeapHitCount() {
335 return delegate.getOffHeapHitCount();
336 }
337
338 /**
339 * {@inheritDoc}
340 *
341 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOnDiskHitCount()
342 */
343 public long getOnDiskHitCount() {
344 return delegate.getOnDiskHitCount();
345 }
346
347 /**
348 * {@inheritDoc}
349 *
350 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getPutCount()
351 */
352 public long getPutCount() {
353 return delegate.getPutCount();
354 }
355
356 /**
357 * {@inheritDoc}
358 *
359 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getRemovedCount()
360 */
361 public long getRemovedCount() {
362 return delegate.getRemovedCount();
363 }
364
365 /**
366 * {@inheritDoc}
367 *
368 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getUpdateCount()
369 */
370 public long getUpdateCount() {
371 return delegate.getUpdateCount();
372 }
373
374 private LiveCacheStatisticsData getDelegateAsLiveStatisticsData() {
375 return (LiveCacheStatisticsData) delegate;
376 }
377
378 /**
379 * {@inheritDoc}
380 *
381 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#addGetTimeMillis(long)
382 */
383 public void addGetTimeMillis(long millis) {
384 getDelegateAsLiveStatisticsData().addGetTimeMillis(millis);
385 }
386
387 /**
388 * {@inheritDoc}
389 *
390 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#addGetTimeNanos(long)
391 */
392 public void addGetTimeNanos(long nanos) {
393 getDelegateAsLiveStatisticsData().addGetTimeNanos(nanos);
394 }
395
396 /**
397 * {@inheritDoc}
398 *
399 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMaxGetTimeMillis()
400 * @deprecated
401 */
402 @Deprecated
403 public long getMaxGetTimeMillis() {
404 return delegate.getMaxGetTimeMillis();
405 }
406
407 /**
408 * {@inheritDoc}
409 *
410 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMinGetTimeMillis()
411 * @deprecated
412 */
413 @Deprecated
414 public long getMinGetTimeMillis() {
415 return delegate.getMinGetTimeMillis();
416 }
417
418 /**
419 * {@inheritDoc}
420 *
421 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMaxGetTimeNanos()
422 */
423 public long getMaxGetTimeNanos() {
424 return delegate.getMaxGetTimeNanos();
425 }
426
427 /**
428 * {@inheritDoc}
429 *
430 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMinGetTimeNanos()
431 */
432 public long getMinGetTimeNanos() {
433 return delegate.getMinGetTimeNanos();
434 }
435
436 /**
437 * {@inheritDoc}
438 *
439 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getXaCommitCount()
440 */
441 public long getXaCommitCount() {
442 return delegate.getXaCommitCount();
443 }
444
445 /**
446 * {@inheritDoc}
447 *
448 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getXaRollbackCount()
449 */
450 public long getXaRollbackCount() {
451 return delegate.getXaRollbackCount();
452 }
453
454 /**
455 * {@inheritDoc}
456 *
457 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getXaRecoveredCount()
458 */
459 public long getXaRecoveredCount() {
460 return delegate.getXaRecoveredCount();
461 }
462
463 /**
464 * {@inheritDoc}
465 */
466 public long getWriterQueueLength() {
467 return delegate.getWriterQueueLength();
468 }
469
470 /**
471 * {@inheritDoc}
472 *
473 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheHitInMemory()
474 */
475 public void cacheHitInMemory() {
476 getDelegateAsLiveStatisticsData().cacheHitInMemory();
477 }
478
479 /**
480 * {@inheritDoc}
481 *
482 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheHitOffHeap()
483 */
484 public void cacheHitOffHeap() {
485 getDelegateAsLiveStatisticsData().cacheHitOffHeap();
486 }
487
488 /**
489 * {@inheritDoc}
490 *
491 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheHitOnDisk()
492 */
493 public void cacheHitOnDisk() {
494 getDelegateAsLiveStatisticsData().cacheHitOnDisk();
495 }
496
497 /**
498 * {@inheritDoc}
499 *
500 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissExpired()
501 */
502 public void cacheMissExpired() {
503 getDelegateAsLiveStatisticsData().cacheMissExpired();
504 }
505
506 /**
507 * {@inheritDoc}
508 *
509 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#xaCommit()
510 */
511 public void xaCommit() {
512 getDelegateAsLiveStatisticsData().xaCommit();
513 }
514
515 /**
516 * {@inheritDoc}
517 *
518 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#xaRollback()
519 */
520 public void xaRollback() {
521 getDelegateAsLiveStatisticsData().xaRollback();
522 }
523
524 /**
525 * {@inheritDoc}
526 *
527 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#xaRecovered(int)
528 */
529 public void xaRecovered(int count) {
530 getDelegateAsLiveStatisticsData().xaRecovered(count);
531 }
532
533 /**
534 * {@inheritDoc}
535 *
536 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissNotFound()
537 */
538 public void cacheMissNotFound() {
539 getDelegateAsLiveStatisticsData().cacheMissNotFound();
540 }
541
542 /**
543 * {@inheritDoc}
544 *
545 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissNotFound()
546 */
547 public void cacheMissInMemory() {
548 getDelegateAsLiveStatisticsData().cacheMissInMemory();
549 }
550
551 /**
552 * {@inheritDoc}
553 *
554 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissNotFound()
555 */
556 public void cacheMissOffHeap() {
557 getDelegateAsLiveStatisticsData().cacheMissOffHeap();
558 }
559
560 /**
561 * {@inheritDoc}
562 *
563 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissNotFound()
564 */
565 public void cacheMissOnDisk() {
566 getDelegateAsLiveStatisticsData().cacheMissOnDisk();
567 }
568
569 /**
570 * {@inheritDoc}
571 *
572 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#clearStatistics()
573 */
574 public void clearStatistics() {
575 getDelegateAsLiveStatisticsData().clearStatistics();
576 }
577
578 /**
579 * {@inheritDoc}
580 *
581 * @see net.sf.ehcache.event.CacheEventListener#dispose()
582 */
583 public void dispose() {
584 getDelegateAsLiveStatisticsData().dispose();
585 }
586
587 /**
588 * {@inheritDoc}
589 *
590 * @see net.sf.ehcache.event.CacheEventListener#notifyElementEvicted(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
591 */
592 public void notifyElementEvicted(Ehcache cache, Element element) {
593 getDelegateAsLiveStatisticsData().notifyElementEvicted(cache, element);
594 }
595
596 /**
597 * {@inheritDoc}
598 *
599 * @see net.sf.ehcache.event.CacheEventListener#notifyElementExpired(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
600 */
601 public void notifyElementExpired(Ehcache cache, Element element) {
602 getDelegateAsLiveStatisticsData().notifyElementExpired(cache, element);
603 }
604
605 /**
606 * {@inheritDoc}
607 *
608 * @see net.sf.ehcache.event.CacheEventListener#notifyElementPut(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
609 */
610 public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
611 getDelegateAsLiveStatisticsData().notifyElementPut(cache, element);
612 }
613
614 /**
615 * {@inheritDoc}
616 *
617 * @see net.sf.ehcache.event.CacheEventListener#notifyElementRemoved(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
618 */
619 public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {
620 getDelegateAsLiveStatisticsData().notifyElementRemoved(cache, element);
621 }
622
623 /**
624 * {@inheritDoc}
625 *
626 * @see net.sf.ehcache.event.CacheEventListener#notifyElementUpdated(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
627 */
628 public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
629 getDelegateAsLiveStatisticsData().notifyElementUpdated(cache, element);
630 }
631
632 /**
633 * {@inheritDoc}
634 *
635 * @see net.sf.ehcache.event.CacheEventListener#notifyRemoveAll(net.sf.ehcache.Ehcache)
636 */
637 public void notifyRemoveAll(Ehcache cache) {
638 getDelegateAsLiveStatisticsData().notifyRemoveAll(cache);
639 }
640
641 /**
642 * {@inheritDoc}
643 *
644 * @see java.lang.Object#clone()
645 */
646 @Override
647 public Object clone() throws CloneNotSupportedException {
648 super.clone();
649 throw new CloneNotSupportedException();
650 }
651
652 }
653