Skip to content

Commit 17d07ce

Browse files
patelilaRebeccaBakerankita-srivastava009
authored
Ccd 5483 enable caffeine cache stats (#2478)
* To get Caffeine specific stats, changes addedx to monitor thosecaches * To get Caffeine specific stats * registering custom cache * registering custom cache recordstats * added /metrics to app insights * added /metrics to app insights * empty commit to trigger build * empty commit to trigger build * empty commit to trigger build * empty commit to trigger build * empty commit to trigger build * empty commit to trigger build --------- Co-authored-by: RebeccaBaker <[email protected]> Co-authored-by: ankita-srivastava009 <[email protected]>
1 parent 7072eda commit 17d07ce

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ dependencies {
226226
implementation('org.springframework.boot:spring-boot-starter-web')
227227
implementation('org.springframework.retry:spring-retry')
228228
implementation('org.springframework.boot:spring-boot-starter-cache')
229-
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.6'
229+
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.8'
230230

231231
implementation group: 'javax.inject', name: 'javax.inject', version: '1'
232232
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
@@ -370,7 +370,7 @@ dependencies {
370370
contractTestImplementation 'com.github.hmcts:ccd-case-document-am-client:1.7.1'
371371
contractTestAnnotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.34'
372372
contractTestImplementation 'org.springframework.boot:spring-boot-starter-cache'
373-
contractTestImplementation 'com.github.ben-manes.caffeine:caffeine:3.1.6'
373+
contractTestImplementation 'com.github.ben-manes.caffeine:caffeine:3.1.8'
374374
aatImplementation group: 'commons-lang', name: 'commons-lang', version: '2.6'
375375
aatImplementation group: 'io.rest-assured', name: 'rest-assured', version: '4.3.0'
376376
aatImplementation group: 'org.projectlombok', name: 'lombok', version: '1.18.34'

lib/applicationinsights.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"key": "http.url",
1414
"value": "https?://[^/]+/health.*",
1515
"matchType": "regexp"
16+
},
17+
{
18+
"key": "http.url",
19+
"value": "https?://[^/]+/metrics.*",
20+
"matchType": "regexp"
1621
}
1722
],
1823
"percentage": 1

src/main/java/uk/gov/hmcts/ccd/config/CacheConfiguration.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import com.github.benmanes.caffeine.cache.Caffeine;
44
import lombok.extern.slf4j.Slf4j;
55
import org.springframework.cache.CacheManager;
6+
import org.springframework.cache.annotation.CachingConfigurerSupport;
67
import org.springframework.cache.annotation.EnableCaching;
78
import org.springframework.cache.caffeine.CaffeineCache;
9+
import org.springframework.cache.caffeine.CaffeineCacheManager;
810
import org.springframework.cache.support.SimpleCacheManager;
911
import org.springframework.context.annotation.Bean;
1012
import org.springframework.context.annotation.Configuration;
@@ -21,11 +23,14 @@
2123
@EnableCaching
2224
@EnableScheduling
2325
@Slf4j
24-
public class CacheConfiguration {
26+
public class CacheConfiguration extends CachingConfigurerSupport {
2527

2628
private static final int TTL_ZERO = 0;
2729
private final ApplicationParams applicationParams;
2830

31+
// create a Caffeine Cache manager
32+
private final CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
33+
2934
public CacheConfiguration(ApplicationParams applicationParams) {
3035
this.applicationParams = applicationParams;
3136
}
@@ -77,7 +82,9 @@ public CacheManager cacheManager() {
7782
newMapConfigWithTtl(SERVICES_CACHE, applicationParams.getRefDataCacheTtlInSec())
7883
));
7984

80-
return cacheManager;
85+
caffeineCacheManager.setCaffeine(Caffeine.newBuilder().recordStats());
86+
caffeineCacheManager.setCacheNames(cacheManager.getCacheNames());
87+
return caffeineCacheManager;
8188
}
8289

8390
private CaffeineCache newMapConfigWithMaxIdle(final String cacheName, final Integer maxIdle) {
@@ -99,6 +106,12 @@ private CaffeineCache buildCache(String cacheName, int ttl, int maxIdle) {
99106
}
100107

101108
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+
103116
}
104117
}

0 commit comments

Comments
 (0)