Caching behavior code added with code cleanup

This commit is contained in:
Abhi Bavishi 2015-06-15 20:19:00 +05:30
parent 2916432e90
commit 729b521890
5 changed files with 205 additions and 0 deletions

View File

@ -0,0 +1,9 @@
@Configuration
@EnableCaching
public class MyAppConfig {
// Your configuration code goes here.
}

View File

@ -0,0 +1,42 @@
public class Customer {
private int id;
private String name;
private String customerAddress;
Customer(String name, String address){
this.name = name;
this.customerAddress = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public String getCustomerAddress() {
return this.customerAddress;
}
public void setName(String name) {
this.name = name;
}
public void setCustomerAddress(String address) {
this.customerAddress = this.name + "," + address;
}
}

View File

@ -0,0 +1,96 @@
package org.baeldung.caching.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
/**
* The Class CustomerDataService.
*/
@Component
@CacheConfig("addressDemo")
public class CustomerDataService {
/** The cache manager. */
@Autowired
CacheManager cacheManager;
/**
* 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(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(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(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(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(Customer customer) {
return customer.getAddress();
}
}

View File

@ -0,0 +1,30 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
<context:annotation-config/>
<bean class="org.baeldung.caching.config.myAppConfig"/>
<!-- the service that you wish to make cacheable.-->
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
<!-- define caching behavior -->
<cache:advice id="cachingBehavior" cache-manager="cacheManager">
<cache:caching cache="addresses">
<cache:cacheable method="getAddress" key="#customer.name"/>
</cache:caching>
</cache:advice>
<!-- apply the behavior to all the implementations of CustomerDataService interface -->
<aop:config>
<aop:advisor advice-ref="cachingBehavior"
pointcut="execution(*org.baeldung.caching.example.CustomerDataService.*(..))"/>
</aop:config>
</beans>

View File

@ -0,0 +1,28 @@
package org.baeldung.caching.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import static org.junit.Assert.*;
import org.junit.Test;
@Component
public class SpringCachingBehaviorTest {
@Test
public void testCaching() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
example = context.getBean(CustomerDataService.class);
Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
example.getAddress(cust);
fail("Unable to instantiate the CustomerDataService");
}
}