27
27
import org .springframework .boot .actuate .metrics .Metric ;
28
28
import org .springframework .cache .Cache ;
29
29
import org .springframework .cache .CacheManager ;
30
+ import org .springframework .cache .transaction .TransactionAwareCacheDecorator ;
30
31
import org .springframework .core .ResolvableType ;
32
+ import org .springframework .util .ClassUtils ;
31
33
import org .springframework .util .LinkedMultiValueMap ;
32
34
import org .springframework .util .MultiValueMap ;
33
35
@@ -87,7 +89,7 @@ private void addMetrics(Collection<Metric<?>> metrics, String cacheName,
87
89
List <CacheManagerBean > cacheManagerBeans ) {
88
90
for (CacheManagerBean cacheManagerBean : cacheManagerBeans ) {
89
91
CacheManager cacheManager = cacheManagerBean .getCacheManager ();
90
- Cache cache = cacheManager .getCache (cacheName );
92
+ Cache cache = unwrapIfNecessary ( cacheManager .getCache (cacheName ) );
91
93
CacheStatistics statistics = getCacheStatistics (cache , cacheManager );
92
94
if (statistics != null ) {
93
95
String prefix = cacheName ;
@@ -100,6 +102,14 @@ private void addMetrics(Collection<Metric<?>> metrics, String cacheName,
100
102
}
101
103
}
102
104
105
+ private Cache unwrapIfNecessary (Cache cache ) {
106
+ if (ClassUtils .isPresent ("org.springframework.cache.transaction.TransactionAwareCacheDecorator" ,
107
+ getClass ().getClassLoader ())) {
108
+ return TransactionAwareCacheDecoratorHandler .unwrapIfNecessary (cache );
109
+ }
110
+ return cache ;
111
+ }
112
+
103
113
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
104
114
private CacheStatistics getCacheStatistics (Cache cache , CacheManager cacheManager ) {
105
115
if (this .statisticsProviders != null ) {
@@ -140,4 +150,19 @@ public CacheManager getCacheManager() {
140
150
141
151
}
142
152
153
+ private static class TransactionAwareCacheDecoratorHandler {
154
+
155
+ private static Cache unwrapIfNecessary (Cache cache ) {
156
+ try {
157
+ if (cache instanceof TransactionAwareCacheDecorator ) {
158
+ return ((TransactionAwareCacheDecorator ) cache ).getTargetCache ();
159
+ }
160
+ }
161
+ catch (NoClassDefFoundError ex ) {
162
+ // Ignore
163
+ }
164
+ return cache ;
165
+ }
166
+ }
167
+
143
168
}
0 commit comments