1
16
17 package net.sf.ehcache.statistics.sampled;
18
19 import net.sf.ehcache.statistics.CacheUsageListener;
20 import net.sf.ehcache.util.FailSafeTimer;
21 import net.sf.ehcache.util.counter.sampled.SampledCounter;
22 import net.sf.ehcache.util.counter.sampled.SampledCounterConfig;
23 import net.sf.ehcache.util.counter.sampled.SampledRateCounter;
24 import net.sf.ehcache.util.counter.sampled.SampledRateCounterConfig;
25
26
37 public class SampledCacheStatisticsWrapper implements CacheUsageListener, CacheStatisticsSampler {
38
39 private static final NullSampledCacheStatistics NULL_SAMPLED_CACHE_STATISTICS = new NullSampledCacheStatistics();
40
41 private volatile SampledCacheStatistics delegate;
42
43 private volatile CacheStatisticsSampler samplerDelegate;
44
45
48 public SampledCacheStatisticsWrapper() {
49 delegate = new NullSampledCacheStatistics();
50 }
51
52
57 public void enableSampledStatistics(FailSafeTimer timer) {
58 enableSampledStatistics(new SampledCacheStatisticsImpl(timer));
59 }
60
61
69 public void enableSampledStatistics(FailSafeTimer timer,
70 SampledCounterConfig config,
71 SampledRateCounterConfig rateGetConfig,
72 SampledRateCounterConfig rateSearchConfig) {
73 enableSampledStatistics(new SampledCacheStatisticsImpl(timer, config, rateGetConfig, rateSearchConfig));
74 }
75
76
81 private void enableSampledStatistics(SampledCacheStatisticsImpl sampledCacheStats) {
82 delegate.dispose();
83 samplerDelegate = sampledCacheStats;
84 delegate = sampledCacheStats;
85 }
86
87
90 public void disableSampledStatistics() {
91 delegate.dispose();
92 delegate = NULL_SAMPLED_CACHE_STATISTICS;
93 samplerDelegate = null;
94 }
95
96
99 public boolean isSampledStatisticsEnabled() {
100 return delegate instanceof SampledCacheStatisticsImpl;
101 }
102
103
107 public void dispose() {
108 delegate.dispose();
109 }
110
111
115 public long getAverageGetTimeMostRecentSample() {
116 return delegate.getAverageGetTimeMostRecentSample();
117 }
118
119
120 @Override
121 public long getAverageGetTimeNanosMostRecentSample() {
122 return delegate.getAverageGetTimeNanosMostRecentSample();
123 }
124
125
129 public long getCacheElementEvictedMostRecentSample() {
130 return delegate.getCacheElementEvictedMostRecentSample();
131 }
132
133
137 public long getCacheElementExpiredMostRecentSample() {
138 return delegate.getCacheElementExpiredMostRecentSample();
139 }
140
141
145 public long getCacheElementPutMostRecentSample() {
146 return delegate.getCacheElementPutMostRecentSample();
147 }
148
149
153 public long getCacheElementRemovedMostRecentSample() {
154 return delegate.getCacheElementRemovedMostRecentSample();
155 }
156
157
161 public long getCacheElementUpdatedMostRecentSample() {
162 return delegate.getCacheElementUpdatedMostRecentSample();
163 }
164
165
169 public long getCacheHitInMemoryMostRecentSample() {
170 return delegate.getCacheHitInMemoryMostRecentSample();
171 }
172
173
177 public long getCacheHitOffHeapMostRecentSample() {
178 return delegate.getCacheHitOffHeapMostRecentSample();
179 }
180
181
185 public long getCacheHitMostRecentSample() {
186 return delegate.getCacheHitMostRecentSample();
187 }
188
189
193 public long getCacheHitOnDiskMostRecentSample() {
194 return delegate.getCacheHitOnDiskMostRecentSample();
195 }
196
197
201 public long getCacheMissExpiredMostRecentSample() {
202 return delegate.getCacheMissExpiredMostRecentSample();
203 }
204
205
209 public long getCacheMissMostRecentSample() {
210 return delegate.getCacheMissMostRecentSample();
211 }
212
213
217 public long getCacheMissInMemoryMostRecentSample() {
218 return delegate.getCacheMissInMemoryMostRecentSample();
219 }
220
221
225 public long getCacheMissOffHeapMostRecentSample() {
226 return delegate.getCacheMissOffHeapMostRecentSample();
227 }
228
229
233 public long getCacheMissOnDiskMostRecentSample() {
234 return delegate.getCacheMissOnDiskMostRecentSample();
235 }
236
237
241 public long getCacheMissNotFoundMostRecentSample() {
242 return delegate.getCacheMissNotFoundMostRecentSample();
243 }
244
245
249 public int getStatisticsAccuracy() {
250 return delegate.getStatisticsAccuracy();
251 }
252
253
258 public void clearStatistics() {
259 delegate.clearStatistics();
260 }
261
262
266 public String getStatisticsAccuracyDescription() {
267 return delegate.getStatisticsAccuracyDescription();
268 }
269
270 private CacheUsageListener getDelegateAsListener() {
271 return (CacheUsageListener) delegate;
272 }
273
274
278 public void notifyCacheElementEvicted() {
279 getDelegateAsListener().notifyCacheElementEvicted();
280 }
281
282
286 public void notifyCacheElementExpired() {
287 getDelegateAsListener().notifyCacheElementExpired();
288 }
289
290
294 public void notifyCacheElementPut() {
295 getDelegateAsListener().notifyCacheElementPut();
296 }
297
298
302 public void notifyCacheElementRemoved() {
303 getDelegateAsListener().notifyCacheElementRemoved();
304 }
305
306
310 public void notifyCacheElementUpdated() {
311 getDelegateAsListener().notifyCacheElementUpdated();
312 }
313
314
318 public void notifyCacheHitInMemory() {
319 getDelegateAsListener().notifyCacheHitInMemory();
320 }
321
322
325 public void notifyCacheHitOffHeap() {
326 getDelegateAsListener().notifyCacheHitOffHeap();
327 }
328
329
333 public void notifyCacheHitOnDisk() {
334 getDelegateAsListener().notifyCacheHitOnDisk();
335 }
336
337
341 public void notifyCacheMissedWithExpired() {
342 getDelegateAsListener().notifyCacheMissedWithExpired();
343 }
344
345
349 public void notifyCacheMissedWithNotFound() {
350 getDelegateAsListener().notifyCacheMissedWithNotFound();
351 }
352
353
357 public void notifyCacheMissInMemory() {
358 getDelegateAsListener().notifyCacheMissInMemory();
359 }
360
361
365 public void notifyCacheMissOffHeap() {
366 getDelegateAsListener().notifyCacheMissOffHeap();
367 }
368
369
373 public void notifyCacheMissOnDisk() {
374 getDelegateAsListener().notifyCacheMissOnDisk();
375 }
376
377
381 public void notifyRemoveAll() {
382 getDelegateAsListener().notifyRemoveAll();
383 }
384
385
389 public void notifyStatisticsAccuracyChanged(int statisticsAccuracy) {
390 getDelegateAsListener().notifyStatisticsAccuracyChanged(statisticsAccuracy);
391 }
392
393
397 public void notifyStatisticsCleared() {
398 getDelegateAsListener().notifyStatisticsCleared();
399 }
400
401
405 public void notifyStatisticsEnabledChanged(boolean enableStatistics) {
406 getDelegateAsListener().notifyStatisticsEnabledChanged(enableStatistics);
407 }
408
409
413 public void notifyTimeTakenForGet(long millis) {
414
415 }
416
417
421 public void notifyGetTimeNanos(long nanos) {
422 getDelegateAsListener().notifyGetTimeNanos(nanos);
423 }
424
425
428 public long getAverageSearchTime() {
429 return delegate.getAverageSearchTime();
430 }
431
432
435 public long getSearchesPerSecond() {
436 return delegate.getSearchesPerSecond();
437 }
438
439
442 public void notifyCacheSearch(long executeTime) {
443 getDelegateAsListener().notifyCacheSearch(executeTime);
444 }
445
446
449 public void notifyXaCommit() {
450 getDelegateAsListener().notifyXaCommit();
451 }
452
453
456 public void notifyXaRollback() {
457 getDelegateAsListener().notifyXaRollback();
458 }
459
460
463 public long getCacheXaCommitsMostRecentSample() {
464 return delegate.getCacheXaCommitsMostRecentSample();
465 }
466
467
470 public long getCacheXaRollbacksMostRecentSample() {
471 return delegate.getCacheXaRollbacksMostRecentSample();
472 }
473
474 @Override
475 public SampledCounter getCacheHitSample() {
476 return samplerDelegate == null ? null : samplerDelegate.getCacheHitSample();
477 }
478
479 @Override
480 public SampledCounter getCacheHitInMemorySample() {
481 return samplerDelegate == null ? null : samplerDelegate.getCacheHitInMemorySample();
482 }
483
484 @Override
485 public SampledCounter getCacheHitOffHeapSample() {
486 return samplerDelegate == null ? null : samplerDelegate.getCacheHitOffHeapSample();
487 }
488
489 @Override
490 public SampledCounter getCacheHitOnDiskSample() {
491 return samplerDelegate == null ? null : samplerDelegate.getCacheHitOnDiskSample();
492 }
493
494 @Override
495 public SampledCounter getCacheMissSample() {
496 return samplerDelegate == null ? null : samplerDelegate.getCacheMissSample();
497 }
498
499 @Override
500 public SampledCounter getCacheMissInMemorySample() {
501 return samplerDelegate == null ? null : samplerDelegate.getCacheHitInMemorySample();
502 }
503
504 @Override
505 public SampledCounter getCacheMissOffHeapSample() {
506 return samplerDelegate == null ? null : samplerDelegate.getCacheMissOffHeapSample();
507 }
508
509 @Override
510 public SampledCounter getCacheMissOnDiskSample() {
511 return samplerDelegate == null ? null : samplerDelegate.getCacheMissOnDiskSample();
512 }
513
514 @Override
515 public SampledCounter getCacheMissExpiredSample() {
516 return samplerDelegate == null ? null : samplerDelegate.getCacheMissExpiredSample();
517 }
518
519 @Override
520 public SampledCounter getCacheMissNotFoundSample() {
521 return samplerDelegate == null ? null : samplerDelegate.getCacheMissNotFoundSample();
522 }
523
524 @Override
525 public SampledCounter getCacheElementEvictedSample() {
526 return samplerDelegate == null ? null : samplerDelegate.getCacheElementEvictedSample();
527 }
528
529 @Override
530 public SampledCounter getCacheElementRemovedSample() {
531 return samplerDelegate == null ? null : samplerDelegate.getCacheElementRemovedSample();
532 }
533
534 @Override
535 public SampledCounter getCacheElementExpiredSample() {
536 return samplerDelegate == null ? null : samplerDelegate.getCacheElementExpiredSample();
537 }
538
539 @Override
540 public SampledCounter getCacheElementPutSample() {
541 return samplerDelegate == null ? null : samplerDelegate.getCacheElementPutSample();
542 }
543
544 @Override
545 public SampledCounter getCacheElementUpdatedSample() {
546 return samplerDelegate == null ? null : samplerDelegate.getCacheElementUpdatedSample();
547 }
548
549 @Override
550 public SampledRateCounter getAverageGetTimeSample() {
551 return samplerDelegate == null ? null : samplerDelegate.getAverageGetTimeSample();
552 }
553
554 @Override
555 public SampledRateCounter getAverageGetTimeNanosSample() {
556 return samplerDelegate == null ? null : samplerDelegate.getAverageGetTimeNanosSample();
557 }
558
559 @Override
560 public SampledRateCounter getAverageSearchTimeSample() {
561 return samplerDelegate == null ? null : samplerDelegate.getAverageSearchTimeSample();
562 }
563
564 @Override
565 public SampledCounter getSearchesPerSecondSample() {
566 return samplerDelegate == null ? null : samplerDelegate.getSearchesPerSecondSample();
567 }
568
569 @Override
570 public SampledCounter getCacheXaCommitsSample() {
571 return samplerDelegate == null ? null : samplerDelegate.getCacheXaCommitsSample();
572 }
573
574 @Override
575 public SampledCounter getCacheXaRollbacksSample() {
576 return samplerDelegate == null ? null : samplerDelegate.getCacheXaRollbacksSample();
577 }
578
579 @Override
580 public int getCacheHitRatioMostRecentSample() {
581 return delegate.getCacheHitRatioMostRecentSample();
582 }
583
584 @Override
585 public SampledCounter getCacheHitRatioSample() {
586 return samplerDelegate == null ? null : samplerDelegate.getCacheHitRatioSample();
587 }
588 }
589