Skip to content

Commit e4a301d

Browse files
author
Eugen
committed
Merge pull request eugenp#218 from abhibavishi/spring-caching
Spring caching
2 parents c4f3a1d + 9eb3faa commit e4a301d

5 files changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
@Configuration
3+
@EnableCaching
4+
public class MyAppConfig {
5+
// Your configuration code goes here.
6+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
public class Customer {
3+
4+
private int id;
5+
private String name;
6+
private String customerAddress;
7+
8+
public Customer(){
9+
}
10+
11+
Customer(String name, String address){
12+
this.name = name;
13+
this.customerAddress = address;
14+
}
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public String getCustomerAddress() {
33+
return this.customerAddress;
34+
}
35+
36+
public void setCustomerAddress(String address) {
37+
this.customerAddress = this.name + "," + address;
38+
}
39+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
@Component
10+
@CacheConfig("addressDemo")
11+
public class CustomerDataService {
12+
13+
@Autowired
14+
CacheManager cacheManager;
15+
16+
/**
17+
* The method returns the customer's address,
18+
only it doesn't find it the cache- addresses and directory.
19+
*
20+
* @param customer the customer
21+
* @return the address
22+
*/
23+
@Cacheable("addresses", “directory”)
24+
public String getAddress1(Customer customer) {
25+
return customer.getAddress();
26+
}
27+
28+
/**
29+
* The method returns the customer's address,
30+
but refreshes all the entries in the cache to load new ones.
31+
*
32+
* @param customer the customer
33+
* @return the address
34+
*/
35+
@CacheEvict(value="addresses", allEntries=true)
36+
public String getAddress2(Customer customer) {
37+
return customer.getAddress();
38+
}
39+
40+
/**
41+
* The method returns the customer's address,
42+
but not before selectively evicting the cache as per specified paramters.
43+
*
44+
* @param customer the customer
45+
* @return the address
46+
*/
47+
@Caching(evict = { @CacheEvict("addresses"), @CacheEvict(value="directory", key="customer.name") })
48+
public String getAddress3(Customer customer) {
49+
return customer.getAddress();
50+
}
51+
52+
/**
53+
* The method uses the class level cache to look up for entries.
54+
*
55+
* @param customer the customer
56+
* @return the address
57+
*/
58+
@Cacheable // parameter not required as we have declared it using @CacheConfig
59+
public String getAddress4(Customer customer) {
60+
return customer.getAddress();
61+
}
62+
63+
/**
64+
* The method selectively caches the results that meet the predefined criteria.
65+
*
66+
* @param customer the customer
67+
* @return the address
68+
*/
69+
@CachePut(value="addresses", condition=”#customer.name=’Tom’”)
70+
@CachePut(value="addresses", unless=”#result.length>64”)
71+
public String getAddress5(Customer customer) {
72+
return customer.getAddress();
73+
}
74+
}
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.caching.test;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
import org.springframework.stereotype.Component;
7+
import static org.junit.Assert.*;
8+
import org.junit.Test;
9+
10+
@Component
11+
public class SpringCachingBehaviorTest {
12+
@Test
13+
public void testCaching() {
14+
@SuppressWarnings("resource")
15+
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
16+
example = context.getBean(CustomerDataService.class);
17+
Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
18+
example.getAddress(cust);
19+
fail("Unable to instantiate the CustomerDataService");
20+
}
21+
}

0 commit comments

Comments
 (0)