caching work
This commit is contained in:
@@ -2,23 +2,19 @@ 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.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
@ComponentScan("org.baeldung.caching.example")
|
||||
public class CachingConfig {
|
||||
|
||||
@Bean
|
||||
public CustomerDataService customerDataService() {
|
||||
return new CustomerDataService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
final SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.baeldung.caching.example;
|
||||
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
|
||||
public abstract class AbstractService {
|
||||
|
||||
// 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.
|
||||
*
|
||||
* @param customer the customer
|
||||
* @return the address
|
||||
*/
|
||||
@Cacheable({ "addresses", "directory" })
|
||||
public String getAddress1(final Customer customer) {
|
||||
return customer.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* The method returns the customer's address,
|
||||
but refreshes all the entries in the cache to load new ones.
|
||||
*
|
||||
* @param customer the customer
|
||||
* @return the address
|
||||
*/
|
||||
@CacheEvict(value = "addresses", allEntries = true)
|
||||
public String getAddress2(final Customer customer) {
|
||||
return customer.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* The method returns the customer's address,
|
||||
but not before selectively evicting the cache as per specified paramters.
|
||||
*
|
||||
* @param customer the customer
|
||||
* @return the address
|
||||
*/
|
||||
@Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value = "directory", key = "#customer.name") })
|
||||
public String getAddress3(final Customer customer) {
|
||||
return customer.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* The method uses the class level cache to look up for entries.
|
||||
*
|
||||
* @param customer the customer
|
||||
* @return the address
|
||||
*/
|
||||
@Cacheable
|
||||
// parameter not required as we have declared it using @CacheConfig
|
||||
public String getAddress4(final Customer customer) {
|
||||
return customer.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* The method selectively caches the results that meet the predefined criteria.
|
||||
*
|
||||
* @param customer the customer
|
||||
* @return the address
|
||||
*/
|
||||
@CachePut(value = "addresses", condition = "#customer.name=='Tom'")
|
||||
// @CachePut(value = "addresses", unless = "#result.length>64")
|
||||
public String getAddress5(final Customer customer) {
|
||||
return customer.getAddress();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.baeldung.caching.example;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
@@ -13,9 +11,6 @@ import org.springframework.stereotype.Component;
|
||||
@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) {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.caching.example;
|
||||
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@CacheConfig(cacheNames = { "addresses" })
|
||||
public class CustomerServiceWithParent extends AbstractService {
|
||||
//
|
||||
}
|
||||
Reference in New Issue
Block a user