@@ -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