From a36b4d88b75aee0f5884e13339c7835bd7353c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Musia=C5=82?= Date: Wed, 10 May 2023 13:38:19 +0200 Subject: [PATCH] BAEL-6250: Example code for Get All Caffeine Cache Keys article (#13806) --- .../allkeys/config/AllKeysConfig.java | 25 +++++++++++ .../allkeys/service/SlowServiceWithCache.java | 11 +++++ .../boot/GetAllCacheKeysIntegrationTest.java | 44 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 spring-boot-modules/spring-caching/src/main/java/com/baeldung/allkeys/config/AllKeysConfig.java create mode 100644 spring-boot-modules/spring-caching/src/main/java/com/baeldung/allkeys/service/SlowServiceWithCache.java create mode 100644 spring-boot-modules/spring-caching/src/test/java/com/baeldung/caching/allkeys/boot/GetAllCacheKeysIntegrationTest.java diff --git a/spring-boot-modules/spring-caching/src/main/java/com/baeldung/allkeys/config/AllKeysConfig.java b/spring-boot-modules/spring-caching/src/main/java/com/baeldung/allkeys/config/AllKeysConfig.java new file mode 100644 index 0000000000..723a782a6a --- /dev/null +++ b/spring-boot-modules/spring-caching/src/main/java/com/baeldung/allkeys/config/AllKeysConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.allkeys.config; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.allkeys.service.SlowServiceWithCache; + +@Configuration +@EnableCaching +public class AllKeysConfig { + + @Bean + SlowServiceWithCache slowServiceWithCache() { + return new SlowServiceWithCache(); + } + + @Bean + CacheManager cacheManager() { + return new CaffeineCacheManager(); + } + +} diff --git a/spring-boot-modules/spring-caching/src/main/java/com/baeldung/allkeys/service/SlowServiceWithCache.java b/spring-boot-modules/spring-caching/src/main/java/com/baeldung/allkeys/service/SlowServiceWithCache.java new file mode 100644 index 0000000000..74223c4231 --- /dev/null +++ b/spring-boot-modules/spring-caching/src/main/java/com/baeldung/allkeys/service/SlowServiceWithCache.java @@ -0,0 +1,11 @@ +package com.baeldung.allkeys.service; + +import org.springframework.cache.annotation.CachePut; + +public class SlowServiceWithCache { + + @CachePut(cacheNames = "slowServiceCache", key = "#name") + public String save(String name, String details) { + return details; + } +} diff --git a/spring-boot-modules/spring-caching/src/test/java/com/baeldung/caching/allkeys/boot/GetAllCacheKeysIntegrationTest.java b/spring-boot-modules/spring-caching/src/test/java/com/baeldung/caching/allkeys/boot/GetAllCacheKeysIntegrationTest.java new file mode 100644 index 0000000000..2104eee8de --- /dev/null +++ b/spring-boot-modules/spring-caching/src/test/java/com/baeldung/caching/allkeys/boot/GetAllCacheKeysIntegrationTest.java @@ -0,0 +1,44 @@ +package com.baeldung.caching.allkeys.boot; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCache; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.allkeys.config.AllKeysConfig; +import com.baeldung.allkeys.service.SlowServiceWithCache; +import com.github.benmanes.caffeine.cache.Cache; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ContextConfiguration(classes = AllKeysConfig.class) +public class GetAllCacheKeysIntegrationTest { + + @Autowired + private SlowServiceWithCache slowServiceWithCache; + @Autowired + private CacheManager cacheManager; + + @Test + public void givenCaffeineCacheCachingSlowCalls_whenCacheManagerProperlyCasted_thenAllKeysAreAccessible() { + slowServiceWithCache.save("first", "some-value-first"); + slowServiceWithCache.save("second", "other-value-second"); + + Cache caffeine = getNativeCaffeineCacheForSlowService(); + + assertThat(caffeine.asMap().keySet()).containsOnly("first", "second"); + } + + private Cache getNativeCaffeineCacheForSlowService() { + CaffeineCacheManager caffeineCacheManager = (CaffeineCacheManager) cacheManager; + CaffeineCache cache = (CaffeineCache) caffeineCacheManager.getCache("slowServiceCache"); + return cache.getNativeCache(); + } +} \ No newline at end of file