diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java new file mode 100644 index 0000000000..1a48092451 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java @@ -0,0 +1,24 @@ +package com.baeldung.caffeine; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class AddressController +{ + @Autowired + private AddressService addressService; + + @GetMapping("/address/{id}") + public ResponseEntity getAddress(@PathVariable("id") long customerId) { + return ResponseEntity.ok(addressService.getAddress(customerId)); + } + + @GetMapping("/address2/{id}") + public ResponseEntity getAddress2(@PathVariable("id") long customerId) { + return ResponseEntity.ok(addressService.getAddress2(customerId)); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java new file mode 100644 index 0000000000..595e7f410a --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java @@ -0,0 +1,41 @@ +package com.baeldung.caffeine; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +/** + * Service that uses caching. + */ +@Service +public class AddressService +{ + private final static Logger LOG = LoggerFactory.getLogger(AddressService.class); + + @Autowired + private CacheManager cacheManager; + + @Cacheable(cacheNames = "addresses") + public String getAddress(long customerId) { + LOG.info("Method getAddress is invoked for customer {}", customerId); + + return "123 Main St"; + } + + public String getAddress2(long customerId) { + if(cacheManager.getCache("addresses2").get(customerId) != null) { + return cacheManager.getCache("addresses2").get(customerId).get().toString(); + } + + LOG.info("Method getAddress2 is invoked for customer {}", customerId); + + String address = "123 Main St"; + + cacheManager.getCache("addresses2").put(customerId, address); + + return address; + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java new file mode 100644 index 0000000000..575ae4a720 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java @@ -0,0 +1,28 @@ +package com.baeldung.caffeine; + +import com.github.benmanes.caffeine.cache.Caffeine; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.concurrent.TimeUnit; + +@EnableCaching +@Configuration +public class CaffeineConfiguration { + @Bean + public Caffeine caffeineConfig() { + return Caffeine.newBuilder() + .expireAfterWrite(60, TimeUnit.MINUTES); + } + + @Bean + public CacheManager cacheManager(Caffeine caffeine) { + CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(); + caffeineCacheManager.getCache("addresses"); + caffeineCacheManager.setCaffeine(caffeine); + return caffeineCacheManager; + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java new file mode 100644 index 0000000000..3828e48fee --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java @@ -0,0 +1,11 @@ +package com.baeldung.caffeine; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +@SpringBootApplication +public class CaffieneTutorialApp { + public static void main(String[] args) { + new SpringApplicationBuilder(CaffieneTutorialApp.class).run(args); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java new file mode 100644 index 0000000000..7f3ad7988f --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java @@ -0,0 +1,24 @@ +package com.baeldung.caffeine; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +/** + * Because the POM imports Spring Security, we need a simple security + * configuration for this example application to allow all HTTP requests. + */ +@Configuration +@EnableWebSecurity +public class SecurityConfiguration extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.csrf().disable(); + + http.authorizeRequests() + .antMatchers("/**") + .permitAll(); + } +}