Skip to content

Commit

Permalink
#376 changed the way we set system variables to use in ehcache.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Peglow-C authored and KlausRicharz committed Jul 19, 2024
1 parent 3e5d486 commit d95bc96
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-jcache -->
<dependency>
<groupId>org.hibernate.orm</groupId>
Expand All @@ -235,7 +231,6 @@
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<scope>runtime</scope>
<classifier>jakarta</classifier>
</dependency>
</dependencies>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/tb/SalatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
public class SalatApplication {

public static void main(String[] args) {
// To use environment variables in the ehcache.xml as replacements
System.getenv().forEach(System::setProperty);
SpringApplication.run(SalatApplication.class, args);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package org.tb.common.configuration;

import org.hibernate.cache.jcache.ConfigSettings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;

@Configuration(proxyBeanMethods = false)
@EnableCaching
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import java.io.IOException;
import java.net.URI;


@Configuration
public class HibernateSecondLevelCacheConfiguration {

@Bean
public HibernatePropertiesCustomizer hibernateSecondLevelCacheCustomizer(JCacheCacheManager cacheManager) {
return (properties) -> properties.put(ConfigSettings.CACHE_MANAGER, cacheManager.getCacheManager());
public HibernatePropertiesCustomizer hibernateSecondLevelCacheCustomizer(ResourceLoader resourceLoader,
@Value("${salat.cache.max-entries}") String maxCacheEntries,
@Value("${salat.cache.expiry-tti}") String expiryTti) {
return (properties) -> {
try {
URI uri = resourceLoader.getResource("classpath:ehcache.xml").getURI();
CachingProvider cachingProvider = Caching.getCachingProvider();
System.setProperty("EHCACHE_EXPIRY_TTI", expiryTti);
System.setProperty("EHCACHE_RESOURCES_HEAP_ENTRIES", maxCacheEntries);
CacheManager cacheManager = cachingProvider.getCacheManager(uri, cachingProvider.getDefaultClassLoader());
properties.put(ConfigSettings.CACHE_MANAGER, cacheManager);
} catch (IOException e) {
throw new RuntimeException(e);
}
};
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ salat:
mail-host: localhost
auth-service:
cache-expiry: 1s
cache:
max-entries: 1000
expiry-tti: 10
springdoc:
swagger-ui:
path: /api/doc/
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

<cache-template name="defaultTemplate">
<expiry>
<tti unit="minutes">10</tti>
<tti unit="minutes">${EHCACHE_EXPIRY_TTI}</tti>
</expiry>
<resources>
<heap unit="entries">${MAX_CACHE_ENTRIES}</heap>
<heap unit="entries">${EHCACHE_RESOURCES_HEAP_ENTRIES}</heap>
</resources>
</cache-template>
</config>

0 comments on commit d95bc96

Please sign in to comment.