Add examples for BAEL-4098
This commit is contained in:
parent
8f20c9cca4
commit
52bd7fb2b6
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine
26
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java
Normal file
26
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java
Normal file
@ -0,0 +1,26 @@
|
||||
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<String> getAddress(@PathVariable("id") long customerId)
|
||||
{
|
||||
return ResponseEntity.ok(addressService.getAddress(customerId));
|
||||
}
|
||||
|
||||
@GetMapping("/address2/{id}")
|
||||
public ResponseEntity<String> getAddress2(@PathVariable("id") long customerId)
|
||||
{
|
||||
return ResponseEntity.ok(addressService.getAddress2(customerId));
|
||||
}
|
||||
}
|
44
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java
Normal file
44
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java
Normal file
@ -0,0 +1,44 @@
|
||||
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;
|
||||
}
|
||||
}
|
30
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java
Normal file
30
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java
Normal file
@ -0,0 +1,30 @@
|
||||
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;
|
||||
}
|
||||
}
|
11
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java
Normal file
11
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
23
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java
Normal file
23
spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.baeldung.caffeine;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
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;
|
||||
import org.zalando.problem.spring.web.advice.security.SecurityProblemSupport;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@Import(SecurityProblemSupport.class)
|
||||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.csrf().disable();
|
||||
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/**")
|
||||
.permitAll();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user