[SIS-xxx] fixing cache setup to add additional caches instead of overwriting the provided configuration

This commit is contained in:
Sheldan
2023-11-23 01:01:40 +01:00
parent de8bbdcbee
commit 170ddd9c33
3 changed files with 16 additions and 19 deletions

View File

@@ -1,15 +1,12 @@
package dev.sheldan.sissi.module.debra.api;
import dev.sheldan.sissi.module.debra.model.api.*;
import dev.sheldan.sissi.module.debra.model.commands.DonationItemModel;
import dev.sheldan.sissi.module.debra.model.commands.DonationsModel;
import dev.sheldan.sissi.module.debra.service.DonationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import static dev.sheldan.sissi.module.debra.config.DebraFeatureConfig.DEBRA_DONATION_NOTIFICATION_SERVER_ID_ENV_NAME;

View File

@@ -1,33 +1,33 @@
package dev.sheldan.sissi.module.debra.config;
import lombok.extern.slf4j.Slf4j;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.jsr107.EhcacheCachingProvider;
import org.ehcache.jsr107.Eh107Configuration;
import org.ehcache.xml.XmlConfiguration;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.cache.CacheManager;
import javax.cache.Caching;
import java.net.URL;
@Configuration
@Slf4j
@EnableCaching
public class CacheConfig {
@Bean("donationCacheManager")
public JCacheCacheManager jCacheCacheManager() {
return new JCacheCacheManager(getDonationCacheManager());
}
@Bean
public CacheManager getDonationCacheManager() {
public JCacheManagerCustomizer cacheManagerCustomizer() {
URL myUrl = getClass().getResource("/donation-cache-config.xml");
XmlConfiguration xmlConfig = new XmlConfiguration(myUrl);
org.ehcache.CacheManager myCacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider");
return provider.getCacheManager(provider.getDefaultURI(), myCacheManager.getRuntimeConfiguration());
return cm -> {
myCacheManager.getRuntimeConfiguration().getCacheConfigurations().entrySet().forEach(cacheConfiguration -> {
javax.cache.configuration.Configuration<?, ?> jConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration.getValue());
log.info("Creating custom cache: " + cacheConfiguration.getKey());
cm.createCache(cacheConfiguration.getKey(), jConfiguration);
});
};
}
}

View File

@@ -134,7 +134,7 @@ public class DonationService {
.collect(Collectors.toList());
}
@Cacheable(value = "donation-cache", cacheManager = "donationCacheManager")
@Cacheable(value = "donation-cache")
public DonationsResponse getCachedDonationAmount(Long serverId) {
return fetchCurrentDonationAmount(serverId);
}