From dab11100a2195d6b727c31757e275377976cc247 Mon Sep 17 00:00:00 2001 From: iProdigy Date: Wed, 27 Dec 2023 21:06:59 -0800 Subject: [PATCH] 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));