Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spring): ensure cache names are unique #217

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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<String> valueLoader = () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> valueLoader = () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}