Skip to content

Commit

Permalink
feat: add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuzynow committed Nov 24, 2023
1 parent 7003f0f commit 7eaa78d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.caritas.cob.mailservice.api.cache;

import lombok.extern.slf4j.Slf4j;
import org.ehcache.event.CacheEvent;
import org.ehcache.event.CacheEventListener;

@Slf4j
public class CacheEventLogger implements CacheEventListener<Object, Object> {

@Override
public void onEvent(CacheEvent<? extends Object, ? extends Object> cacheEvent) {
log.info(
"caching event: ", cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import springfox.documentation.annotations.Cacheable;

@Service
@Slf4j
Expand All @@ -43,6 +43,7 @@ public TranslationService(TranlationMangementServiceApiClient tranlationMangemen
@Cacheable(value = "translations")
public Map<String, String> fetchTranslations(String languageCode) {
try {

return fetchTranslationAsMap(languageCode);
} catch (JsonProcessingException ex) {
throw new TranslationServiceException(String.format(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.caritas.cob.mailservice.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -13,6 +14,7 @@
*/
@Configuration
@ComponentScan(basePackages = {"de.caritas.cob.mailservice"})
@EnableCaching
public class AppConfig {

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ management.endpoint.health.enabled=true
management.endpoint.health.show-details=never
management.endpoints.web.exposure.include=health
management.health.probes.enabled=true
spring.cache.jcache.config=classpath:ehcache.xml

logging.level.net.sf.ehcache=info
38 changes: 38 additions & 0 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="
http://www.ehcache.org/v3
http://www.ehcache.org/schema/ehcache-core-3.7.xsd">

<cache alias="translations">
<expiry>
<ttl unit="seconds">60</ttl>
</expiry>

<resources>
<offheap unit="MB">10</offheap>
</resources>
</cache>

<cache-template name="default">
<expiry>
<ttl unit="seconds">60</ttl>
</expiry>
<listeners>
<listener>
<class>de.caritas.cob.mailservice.api.cache.CacheEventLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap>1000</heap>
<offheap unit="MB">10</offheap>
<disk persistent="true" unit="MB">20</disk>
</resources>
</cache-template>
</config>

0 comments on commit 7eaa78d

Please sign in to comment.