From 06dd07b8bad13866c0f3fcc506a2d38fb6a51548 Mon Sep 17 00:00:00 2001 From: Sven Carrillo Castillo Date: Sun, 14 Apr 2024 16:39:17 +0200 Subject: [PATCH] add one and multiple key Application Caches + config --- .../java/com/recom/api/CacheController.java | 7 +- .../configuration/EHCacheConfiguration.java | 5 +- .../service/cache/ApplicationCacheTester.java | 64 +++++++++---------- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/services/recom-backend/src/main/java/com/recom/api/CacheController.java b/services/recom-backend/src/main/java/com/recom/api/CacheController.java index 16de1192..b2320b70 100644 --- a/services/recom-backend/src/main/java/com/recom/api/CacheController.java +++ b/services/recom-backend/src/main/java/com/recom/api/CacheController.java @@ -93,13 +93,14 @@ public ResponseEntity> testAppCache() { log.debug("Requested GET /api/v1/cache/test-app-cache"); final CacheStatisticsDto testWithoutKey = applicationCacheTester.testWithoutKey(); -// final CacheStatisticsDto testWithOneKey = applicationCacheTester.testWithOneKey("key1"); -// final CacheStatisticsDto testWithMultipleKEys = applicationCacheTester.testWithMultipleKeys("key1", "key2"); + for (int i = 0; i < 1000; i++) { + applicationCacheTester.testWithOneKey("key" + i); + applicationCacheTester.testWithMultipleKeys("key1" + i, "key2" + i); + } return ResponseEntity.status(HttpStatus.OK) .cacheControl(CacheControl.noCache()) .body(List.of(testWithoutKey)); -// .body(List.of(testWithoutKey, testWithOneKey, testWithMultipleKEys)); } } \ No newline at end of file diff --git a/services/recom-backend/src/main/java/com/recom/configuration/EHCacheConfiguration.java b/services/recom-backend/src/main/java/com/recom/configuration/EHCacheConfiguration.java index f30a9253..9949ae69 100644 --- a/services/recom-backend/src/main/java/com/recom/configuration/EHCacheConfiguration.java +++ b/services/recom-backend/src/main/java/com/recom/configuration/EHCacheConfiguration.java @@ -9,6 +9,7 @@ import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.jsr107.Eh107Configuration; import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer; +import org.springframework.cache.interceptor.SimpleKey; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -52,7 +53,9 @@ private void addCaches(@NotNull final CacheManager eh107CacheManager) { private void addEHCaches(@NotNull final CacheManager eh107CacheManager) { - final Cache myCache = eh107CacheManager.createCache("com.recom.service.cache.ApplicationCacheTester.testWithoutKey()", Eh107Configuration.fromEhcacheCacheConfiguration(applicationEHCacheConfiguration(Object.class, CacheStatisticsDto.class))); + eh107CacheManager.createCache("com.recom.service.cache.ApplicationCacheTester.testWithoutKey", Eh107Configuration.fromEhcacheCacheConfiguration(applicationEHCacheConfiguration(Object.class, CacheStatisticsDto.class))); + eh107CacheManager.createCache("com.recom.service.cache.ApplicationCacheTester.testWithOneKey", Eh107Configuration.fromEhcacheCacheConfiguration(applicationEHCacheConfiguration(String.class, CacheStatisticsDto.class))); + eh107CacheManager.createCache("com.recom.service.cache.ApplicationCacheTester.testWithMultipleKeys", Eh107Configuration.fromEhcacheCacheConfiguration(applicationEHCacheConfiguration(SimpleKey.class, CacheStatisticsDto.class))); } @NonNull diff --git a/services/recom-backend/src/main/java/com/recom/service/cache/ApplicationCacheTester.java b/services/recom-backend/src/main/java/com/recom/service/cache/ApplicationCacheTester.java index 28cdb41c..02dd9118 100644 --- a/services/recom-backend/src/main/java/com/recom/service/cache/ApplicationCacheTester.java +++ b/services/recom-backend/src/main/java/com/recom/service/cache/ApplicationCacheTester.java @@ -19,7 +19,7 @@ public class ApplicationCacheTester { @NonNull - @CacheResult + @CacheResult(cacheName = "com.recom.service.cache.ApplicationCacheTester.testWithoutKey") public CacheStatisticsDto testWithoutKey() { return CacheStatisticsDto.builder() .caches(List.of( @@ -31,36 +31,36 @@ public CacheStatisticsDto testWithoutKey() { )) .build(); } -// -// @NonNull -// @CacheResult() -// public CacheStatisticsDto testWithOneKey(@NonNull final String key) { -// return CacheStatisticsDto.builder() -// .caches(List.of( -// CacheInfoDto.builder() -// .name("test-with-one-key: " + key) -// .size(1L) -// .stats("test") -// .build() -// )) -// .build(); -// } -// -// @NonNull -// @CacheResult -// public CacheStatisticsDto testWithMultipleKeys( -// @NonNull final String key1, -// @NonNull final String key2 -// ) { -// return CacheStatisticsDto.builder() -// .caches(List.of( -// CacheInfoDto.builder() -// .name("test-with-multiple-keys: " + key1 + ", " + key2) -// .size(1L) -// .stats("test") -// .build() -// )) -// .build(); -// } + + @NonNull + @CacheResult(cacheName = "com.recom.service.cache.ApplicationCacheTester.testWithOneKey") + public CacheStatisticsDto testWithOneKey(@NonNull final String key) { + return CacheStatisticsDto.builder() + .caches(List.of( + CacheInfoDto.builder() + .name("test-with-one-key: " + key) + .size(1L) + .stats("test") + .build() + )) + .build(); + } + + @NonNull + @CacheResult(cacheName = "com.recom.service.cache.ApplicationCacheTester.testWithMultipleKeys") + public CacheStatisticsDto testWithMultipleKeys( + @NonNull final String key1, + @NonNull final String key2 + ) { + return CacheStatisticsDto.builder() + .caches(List.of( + CacheInfoDto.builder() + .name("test-with-multiple-keys: " + key1 + ", " + key2) + .size(1L) + .stats("test") + .build() + )) + .build(); + } } \ No newline at end of file