3
3
import com .github .benmanes .caffeine .cache .Caffeine ;
4
4
import lombok .extern .slf4j .Slf4j ;
5
5
import org .springframework .cache .CacheManager ;
6
+ import org .springframework .cache .annotation .CachingConfigurerSupport ;
6
7
import org .springframework .cache .annotation .EnableCaching ;
7
8
import org .springframework .cache .caffeine .CaffeineCache ;
9
+ import org .springframework .cache .caffeine .CaffeineCacheManager ;
8
10
import org .springframework .cache .support .SimpleCacheManager ;
9
11
import org .springframework .context .annotation .Bean ;
10
12
import org .springframework .context .annotation .Configuration ;
21
23
@ EnableCaching
22
24
@ EnableScheduling
23
25
@ Slf4j
24
- public class CacheConfiguration {
26
+ public class CacheConfiguration extends CachingConfigurerSupport {
25
27
26
28
private static final int TTL_ZERO = 0 ;
27
29
private final ApplicationParams applicationParams ;
28
30
31
+ // create a Caffeine Cache manager
32
+ private final CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager ();
33
+
29
34
public CacheConfiguration (ApplicationParams applicationParams ) {
30
35
this .applicationParams = applicationParams ;
31
36
}
@@ -77,7 +82,9 @@ public CacheManager cacheManager() {
77
82
newMapConfigWithTtl (SERVICES_CACHE , applicationParams .getRefDataCacheTtlInSec ())
78
83
));
79
84
80
- return cacheManager ;
85
+ caffeineCacheManager .setCaffeine (Caffeine .newBuilder ().recordStats ());
86
+ caffeineCacheManager .setCacheNames (cacheManager .getCacheNames ());
87
+ return caffeineCacheManager ;
81
88
}
82
89
83
90
private CaffeineCache newMapConfigWithMaxIdle (final String cacheName , final Integer maxIdle ) {
@@ -99,6 +106,12 @@ private CaffeineCache buildCache(String cacheName, int ttl, int maxIdle) {
99
106
}
100
107
101
108
cacheBuilder .maximumSize (applicationParams .getDefaultCacheMaxSize ());
102
- return new CaffeineCache (cacheName , cacheBuilder .build ());
109
+ log .debug ("creating custom cache: name='{}'" , cacheName );
110
+ CaffeineCache caffeineCache = new CaffeineCache (cacheName , cacheBuilder .recordStats ().build ());
111
+ caffeineCacheManager .registerCustomCache (cacheName , cacheBuilder .build ());
112
+ log .debug ("registering custom cache: name='{}'" , cacheName );
113
+
114
+ return caffeineCache ;
115
+
103
116
}
104
117
}
0 commit comments