Skip to content

Commit 729b521

Browse files
committed
Caching behavior code added with code cleanup
1 parent 2916432 commit 729b521

5 files changed

Lines changed: 205 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
@Configuration
3+
@EnableCaching
4+
5+
public class MyAppConfig {
6+
7+
// Your configuration code goes here.
8+
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
public class Customer {
3+
4+
5+
private int id;
6+
7+
8+
private String name;
9+
10+
11+
private String customerAddress;
12+
13+
Customer(String name, String address){
14+
this.name = name;
15+
this.customerAddress = address;
16+
}
17+
18+
19+
public int getId() {
20+
return id;
21+
}
22+
23+
public void setId(int id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public String getCustomerAddress() {
32+
return this.customerAddress;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public void setCustomerAddress(String address) {
40+
this.customerAddress = this.name + "," + address;
41+
}
42+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.baeldung.caching.example;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cache.CacheManager;
5+
import org.springframework.cache.annotation.Cacheable;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* The Class CustomerDataService.
11+
*/
12+
@Component
13+
@CacheConfig("addressDemo")
14+
public class CustomerDataService {
15+
16+
/** The cache manager. */
17+
@Autowired
18+
CacheManager cacheManager;
19+
20+
/**
21+
* The method returns the customer's address,
22+
only it doesn't find it the cache- addresses and directory.
23+
*
24+
* @param customer the customer
25+
* @return the address
26+
*/
27+
28+
@Cacheable("addresses", “directory”)
29+
30+
public String getAddress1(Customer customer) {
31+
32+
return customer.getAddress();
33+
}
34+
35+
/**
36+
* The method returns the customer's address,
37+
but refreshes all the entries in the cache to load new ones.
38+
*
39+
* @param customer the customer
40+
* @return the address
41+
*/
42+
43+
@CacheEvict(value="addresses", allEntries=true)
44+
45+
public String getAddress2(Customer customer) {
46+
47+
return customer.getAddress();
48+
}
49+
50+
/**
51+
* The method returns the customer's address,
52+
but not before selectively evicting the cache as per specified paramters.
53+
*
54+
* @param customer the customer
55+
* @return the address
56+
*/
57+
58+
@Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") })
59+
60+
public String getAddress3(Customer customer) {
61+
62+
return customer.getAddress();
63+
}
64+
65+
/**
66+
* The method uses the class level cache to look up for entries.
67+
*
68+
* @param customer the customer
69+
* @return the address
70+
*/
71+
72+
@Cacheable // parameter not required as we have declared it using @CacheConfig
73+
74+
public String getAddress4(Customer customer) {
75+
76+
return customer.getAddress();
77+
}
78+
79+
/**
80+
* The method selectively caches the results that meet the predefined criteria.
81+
*
82+
* @param customer the customer
83+
* @return the address
84+
*/
85+
86+
87+
@CachePut(value="addresses", condition=”#customer.name=’Tom’”)
88+
@CachePut(value="addresses", unless=”#result.length>64”)
89+
90+
public String getAddress5(Customer customer) {
91+
92+
return customer.getAddress();
93+
}
94+
95+
96+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:cache="http://www.springframework.org/schema/cache"
4+
xsi:schemaLocation="
5+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
7+
8+
<cache:annotation-driven />
9+
10+
<context:annotation-config/>
11+
<bean class="org.baeldung.caching.config.myAppConfig"/>
12+
13+
14+
<!-- the service that you wish to make cacheable.-->
15+
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
16+
17+
<!-- define caching behavior -->
18+
<cache:advice id="cachingBehavior" cache-manager="cacheManager">
19+
<cache:caching cache="addresses">
20+
<cache:cacheable method="getAddress" key="#customer.name"/>
21+
</cache:caching>
22+
</cache:advice>
23+
24+
<!-- apply the behavior to all the implementations of CustomerDataService interface -->
25+
<aop:config>
26+
<aop:advisor advice-ref="cachingBehavior"
27+
pointcut="execution(*org.baeldung.caching.example.CustomerDataService.*(..))"/>
28+
</aop:config>
29+
30+
</beans>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
package org.baeldung.caching.test;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.support.ClassPathXmlApplicationContext;
8+
import org.springframework.stereotype.Component;
9+
import static org.junit.Assert.*;
10+
import org.junit.Test;
11+
12+
13+
14+
@Component
15+
16+
public class SpringCachingBehaviorTest {
17+
18+
@Test
19+
public void testCaching() {
20+
@SuppressWarnings("resource")
21+
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
22+
example = context.getBean(CustomerDataService.class);
23+
Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
24+
example.getAddress(cust);
25+
fail("Unable to instantiate the CustomerDataService");
26+
}
27+
28+
}

0 commit comments

Comments
 (0)