From dab11100a2195d6b727c31757e275377976cc247 Mon Sep 17 00:00:00 2001 From: iProdigy Date: Wed, 27 Dec 2023 21:06:59 -0800 Subject: [PATCH 1/2] fix(spring): ensure cache names are unique --- .../cache/springjdk17/XanthicSpringCacheManager.java | 10 +++++++--- .../cache/spring/XanthicSpringCacheManager.java | 12 ++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/spring-java17/src/main/java/io/github/xanthic/cache/springjdk17/XanthicSpringCacheManager.java b/spring-java17/src/main/java/io/github/xanthic/cache/springjdk17/XanthicSpringCacheManager.java index 2071b8cb..4ca68211 100644 --- a/spring-java17/src/main/java/io/github/xanthic/cache/springjdk17/XanthicSpringCacheManager.java +++ b/spring-java17/src/main/java/io/github/xanthic/cache/springjdk17/XanthicSpringCacheManager.java @@ -49,7 +49,7 @@ public XanthicSpringCacheManager(Consumer> spec, @N if (cacheNames != null) { this.dynamic = false; for (String name : cacheNames) { - this.cacheMap.put(name, createCache(name, this.spec)); + this.cacheMap.computeIfAbsent(name, s -> createCache(s, this.spec)); } } else { this.dynamic = true; @@ -77,12 +77,16 @@ public Cache getCache(@NotNull String name) { * * @param name the name of the cache * @param spec configuration for the specified cache + * @throws IllegalStateException if the cache manager is not in dynamic mode or a cache with the same name was already registered */ public void registerCache(String name, Consumer> spec) { if (!this.dynamic) throw new IllegalStateException("CacheManager has a fixed set of cache keys and does not allow creation of new caches."); - this.cacheMap.put(name, createCache(name, spec)); - this.customCacheNames.add(name); + if (this.customCacheNames.add(name)) { + this.cacheMap.put(name, createCache(name, spec)); + } else { + throw new IllegalStateException("CacheManager already has a cache registered with the name: " + name); + } } private Cache createCache(String name, Consumer> spec) { diff --git a/spring/src/main/java/io/github/xanthic/cache/spring/XanthicSpringCacheManager.java b/spring/src/main/java/io/github/xanthic/cache/spring/XanthicSpringCacheManager.java index 6d2eda84..5eaa4ea1 100644 --- a/spring/src/main/java/io/github/xanthic/cache/spring/XanthicSpringCacheManager.java +++ b/spring/src/main/java/io/github/xanthic/cache/spring/XanthicSpringCacheManager.java @@ -49,7 +49,7 @@ public XanthicSpringCacheManager(Consumer> spec, @N if (cacheNames != null) { this.dynamic = false; for (String name : cacheNames) { - this.cacheMap.put(name, createCache(name, this.spec)); + this.cacheMap.computeIfAbsent(name, s -> createCache(s, this.spec)); } } else { this.dynamic = true; @@ -77,13 +77,17 @@ public Cache getCache(@NotNull String name) { * * @param name the name of the cache * @param spec configuration for the specified cache + * @throws IllegalStateException if the cache manager is not in dynamic mode or a cache with the same name was already registered */ public void registerCache(String name, Consumer> spec) { if (!this.dynamic) throw new IllegalStateException("CacheManager has a fixed set of cache keys and does not allow creation of new caches."); - this.cacheMap.put(name, createCache(name, spec)); - this.customCacheNames.add(name); - } + if (this.customCacheNames.add(name)) { + this.cacheMap.put(name, createCache(name, spec)); + } else { + throw new IllegalStateException("CacheManager already has a cache registered with the name: " + name); + } + } private Cache createCache(String name, Consumer> spec) { return new XanthicSpringCache(name, CacheApi.create(spec)); From df7a424b94cde42b48cc3178c6061a975d0ebb5a Mon Sep 17 00:00:00 2001 From: iProdigy Date: Wed, 27 Dec 2023 21:11:36 -0800 Subject: [PATCH 2/2] chore: update tests --- .../github/xanthic/cache/springjdk17/SpringCacheTest.java | 4 ++-- .../cache/springjdk17/config/CacheConfiguration.java | 6 +----- .../io/github/xanthic/cache/spring/SpringCacheTest.java | 4 ++-- .../xanthic/cache/spring/config/CacheConfiguration.java | 7 +------ 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/spring-java17/src/test/java/io/github/xanthic/cache/springjdk17/SpringCacheTest.java b/spring-java17/src/test/java/io/github/xanthic/cache/springjdk17/SpringCacheTest.java index e046c124..3cf9b5c7 100644 --- a/spring-java17/src/test/java/io/github/xanthic/cache/springjdk17/SpringCacheTest.java +++ b/spring-java17/src/test/java/io/github/xanthic/cache/springjdk17/SpringCacheTest.java @@ -126,10 +126,10 @@ public void valueLoaderTest() { @DisplayName("Tests the eviction of entries based on max size") public void valueLoaderConcurrentTest() throws InterruptedException { XanthicSpringCacheManager xanthicSpringCacheManager = (XanthicSpringCacheManager) cacheManager; - xanthicSpringCacheManager.registerCache("value-cache", spec -> { + xanthicSpringCacheManager.registerCache("value-cache-concurrent", spec -> { spec.maxSize(100L); }); - Cache cache = Objects.requireNonNull(cacheManager.getCache("value-cache")); + Cache cache = Objects.requireNonNull(cacheManager.getCache("value-cache-concurrent")); AtomicInteger callCounter = new AtomicInteger(0); Callable valueLoader = () -> { diff --git a/spring-java17/src/test/java/io/github/xanthic/cache/springjdk17/config/CacheConfiguration.java b/spring-java17/src/test/java/io/github/xanthic/cache/springjdk17/config/CacheConfiguration.java index 13cef3cd..c375c5dc 100644 --- a/spring-java17/src/test/java/io/github/xanthic/cache/springjdk17/config/CacheConfiguration.java +++ b/spring-java17/src/test/java/io/github/xanthic/cache/springjdk17/config/CacheConfiguration.java @@ -11,13 +11,9 @@ public class CacheConfiguration { @Bean public CacheManager cacheManager() { - XanthicSpringCacheManager cacheManager = new XanthicSpringCacheManager(spec -> { + return new XanthicSpringCacheManager(spec -> { spec.expiryType(ExpiryType.POST_ACCESS); }); - cacheManager.registerCache("my-custom-cache", spec -> { - spec.maxSize(10L); - }); - return cacheManager; } } diff --git a/spring/src/test/java/io/github/xanthic/cache/spring/SpringCacheTest.java b/spring/src/test/java/io/github/xanthic/cache/spring/SpringCacheTest.java index 9d458d03..33a86379 100644 --- a/spring/src/test/java/io/github/xanthic/cache/spring/SpringCacheTest.java +++ b/spring/src/test/java/io/github/xanthic/cache/spring/SpringCacheTest.java @@ -113,10 +113,10 @@ public void valueLoaderTest() { @DisplayName("Tests the eviction of entries based on max size") public void valueLoaderConcurrentTest() throws InterruptedException { XanthicSpringCacheManager xanthicSpringCacheManager = (XanthicSpringCacheManager) cacheManager; - xanthicSpringCacheManager.registerCache("value-cache", spec -> { + xanthicSpringCacheManager.registerCache("value-cache-concurrent", spec -> { spec.maxSize(100L); }); - Cache cache = Objects.requireNonNull(cacheManager.getCache("value-cache")); + Cache cache = Objects.requireNonNull(cacheManager.getCache("value-cache-concurrent")); AtomicInteger callCounter = new AtomicInteger(0); Callable valueLoader = () -> { diff --git a/spring/src/test/java/io/github/xanthic/cache/spring/config/CacheConfiguration.java b/spring/src/test/java/io/github/xanthic/cache/spring/config/CacheConfiguration.java index 3e95cd86..e338438c 100644 --- a/spring/src/test/java/io/github/xanthic/cache/spring/config/CacheConfiguration.java +++ b/spring/src/test/java/io/github/xanthic/cache/spring/config/CacheConfiguration.java @@ -11,14 +11,9 @@ public class CacheConfiguration { @Bean public CacheManager cacheManager() { - XanthicSpringCacheManager cacheManager = new XanthicSpringCacheManager(spec -> { + return new XanthicSpringCacheManager(spec -> { spec.expiryType(ExpiryType.POST_ACCESS); }); - cacheManager.registerCache("my-custom-cache", spec -> { - spec.maxSize(10L); - }); - - return cacheManager; } }