Skip to content

Commit 7c99c04

Browse files
committed
fix(spring): ensure cache names are unique
1 parent 2b7dd60 commit 7c99c04

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

spring-java17/src/main/java/io/github/xanthic/cache/springjdk17/XanthicSpringCacheManager.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public XanthicSpringCacheManager(Consumer<CacheApiSpec<Object, Object>> spec, @N
4949
if (cacheNames != null) {
5050
this.dynamic = false;
5151
for (String name : cacheNames) {
52-
this.cacheMap.put(name, createCache(name, this.spec));
52+
this.cacheMap.computeIfAbsent(name, s -> createCache(s, this.spec));
5353
}
5454
} else {
5555
this.dynamic = true;
@@ -77,12 +77,16 @@ public Cache getCache(@NotNull String name) {
7777
*
7878
* @param name the name of the cache
7979
* @param spec configuration for the specified cache
80+
* @throws IllegalStateException if the cache manager is not in dynamic mode or a cache with the same name was already registered
8081
*/
8182
public void registerCache(String name, Consumer<CacheApiSpec<Object, Object>> spec) {
8283
if (!this.dynamic) throw new IllegalStateException("CacheManager has a fixed set of cache keys and does not allow creation of new caches.");
8384

84-
this.cacheMap.put(name, createCache(name, spec));
85-
this.customCacheNames.add(name);
85+
if (this.customCacheNames.add(name)) {
86+
this.cacheMap.put(name, createCache(name, spec));
87+
} else {
88+
throw new IllegalStateException("CacheManager already has a cache registered with the name: " + name);
89+
}
8690
}
8791

8892
private Cache createCache(String name, Consumer<CacheApiSpec<Object, Object>> spec) {

spring/src/main/java/io/github/xanthic/cache/spring/XanthicSpringCacheManager.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public XanthicSpringCacheManager(Consumer<CacheApiSpec<Object, Object>> spec, @N
4949
if (cacheNames != null) {
5050
this.dynamic = false;
5151
for (String name : cacheNames) {
52-
this.cacheMap.put(name, createCache(name, this.spec));
52+
this.cacheMap.computeIfAbsent(name, s -> createCache(s, this.spec));
5353
}
5454
} else {
5555
this.dynamic = true;
@@ -77,13 +77,17 @@ public Cache getCache(@NotNull String name) {
7777
*
7878
* @param name the name of the cache
7979
* @param spec configuration for the specified cache
80+
* @throws IllegalStateException if the cache manager is not in dynamic mode or a cache with the same name was already registered
8081
*/
8182
public void registerCache(String name, Consumer<CacheApiSpec<Object, Object>> spec) {
8283
if (!this.dynamic) throw new IllegalStateException("CacheManager has a fixed set of cache keys and does not allow creation of new caches.");
8384

84-
this.cacheMap.put(name, createCache(name, spec));
85-
this.customCacheNames.add(name);
86-
}
85+
if (this.customCacheNames.add(name)) {
86+
this.cacheMap.put(name, createCache(name, spec));
87+
} else {
88+
throw new IllegalStateException("CacheManager already has a cache registered with the name: " + name);
89+
}
90+
}
8791

8892
private Cache createCache(String name, Consumer<CacheApiSpec<Object, Object>> spec) {
8993
return new XanthicSpringCache(name, CacheApi.create(spec));

0 commit comments

Comments
 (0)