caching java config
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package org.baeldung.caching.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.baeldung.caching.example.CustomerDataService;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CachingConfig {
|
||||
|
||||
@Bean
|
||||
public CustomerDataService customerDataService() {
|
||||
return new CustomerDataService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
final SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("directory"), new ConcurrentMapCache("addresses")));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package org.baeldung.caching.config;
|
||||
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class MyAppConfig {
|
||||
// Your configuration code goes here.
|
||||
}
|
||||
@@ -10,12 +10,18 @@ import org.springframework.cache.annotation.Caching;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@CacheConfig(cacheNames = { "addressDemo" })
|
||||
@CacheConfig(cacheNames = { "addresses" })
|
||||
public class CustomerDataService {
|
||||
|
||||
@Autowired
|
||||
CacheManager cacheManager;
|
||||
|
||||
// this method configuration is equivalent to xml configuration
|
||||
@Cacheable(value = "addresses", key = "#customer.name")
|
||||
public String getAddress(final Customer customer) {
|
||||
return customer.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* The method returns the customer's address,
|
||||
only it doesn't find it the cache- addresses and directory.
|
||||
|
||||
Reference in New Issue
Block a user