Merge pull request #9483 from michael-pratt/BAEL-4098

Add examples for BAEL-4098
This commit is contained in:
Eric Martin 2020-06-20 22:46:09 -05:00 committed by GitHub
commit 2dd5aa5614
5 changed files with 128 additions and 0 deletions

View File

@ -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<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));
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View 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);
}
}

View File

@ -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();
}
}