Skip to content

Commit

Permalink
fix(spring): ensure cache names are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy authored and PhilippHeuer committed Jul 28, 2024
1 parent 2dc6229 commit dab1110
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public XanthicSpringCacheManager(Consumer<CacheApiSpec<Object, Object>> 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;
Expand Down Expand Up @@ -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<CacheApiSpec<Object, Object>> 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<CacheApiSpec<Object, Object>> spec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public XanthicSpringCacheManager(Consumer<CacheApiSpec<Object, Object>> 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;
Expand Down Expand Up @@ -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<CacheApiSpec<Object, Object>> 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<CacheApiSpec<Object, Object>> spec) {
return new XanthicSpringCache(name, CacheApi.create(spec));
Expand Down

0 comments on commit dab1110

Please sign in to comment.