Skip to content

Commit f83d3dc

Browse files
author
eugenp
committed
caching work
1 parent 378d0c0 commit f83d3dc

6 files changed

Lines changed: 159 additions & 41 deletions

File tree

‎spring-all/src/main/java/org/baeldung/caching/config/CachingConfig.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
import java.util.Arrays;
44

5-
import org.baeldung.caching.example.CustomerDataService;
65
import org.springframework.cache.CacheManager;
76
import org.springframework.cache.annotation.EnableCaching;
87
import org.springframework.cache.concurrent.ConcurrentMapCache;
98
import org.springframework.cache.support.SimpleCacheManager;
109
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.ComponentScan;
1111
import org.springframework.context.annotation.Configuration;
1212

1313
@Configuration
1414
@EnableCaching
15+
@ComponentScan("org.baeldung.caching.example")
1516
public class CachingConfig {
1617

17-
@Bean
18-
public CustomerDataService customerDataService() {
19-
return new CustomerDataService();
20-
}
21-
2218
@Bean
2319
public CacheManager cacheManager() {
2420
final SimpleCacheManager cacheManager = new SimpleCacheManager();
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.baeldung.caching.example;
2+
3+
import org.springframework.cache.annotation.CacheEvict;
4+
import org.springframework.cache.annotation.CachePut;
5+
import org.springframework.cache.annotation.Cacheable;
6+
import org.springframework.cache.annotation.Caching;
7+
8+
public abstract class AbstractService {
9+
10+
// this method configuration is equivalent to xml configuration
11+
@Cacheable(value = "addresses", key = "#customer.name")
12+
public String getAddress(final Customer customer) {
13+
return customer.getAddress();
14+
}
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(final 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(final 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(final 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
59+
// parameter not required as we have declared it using @CacheConfig
60+
public String getAddress4(final Customer customer) {
61+
return customer.getAddress();
62+
}
63+
64+
/**
65+
* The method selectively caches the results that meet the predefined criteria.
66+
*
67+
* @param customer the customer
68+
* @return the address
69+
*/
70+
@CachePut(value = "addresses", condition = "#customer.name=='Tom'")
71+
// @CachePut(value = "addresses", unless = "#result.length>64")
72+
public String getAddress5(final Customer customer) {
73+
return customer.getAddress();
74+
}
75+
76+
}

‎spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.baeldung.caching.example;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.cache.CacheManager;
53
import org.springframework.cache.annotation.CacheConfig;
64
import org.springframework.cache.annotation.CacheEvict;
75
import org.springframework.cache.annotation.CachePut;
@@ -13,9 +11,6 @@
1311
@CacheConfig(cacheNames = { "addresses" })
1412
public class CustomerDataService {
1513

16-
@Autowired
17-
CacheManager cacheManager;
18-
1914
// this method configuration is equivalent to xml configuration
2015
@Cacheable(value = "addresses", key = "#customer.name")
2116
public String getAddress(final Customer customer) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.caching.example;
2+
3+
import org.springframework.cache.annotation.CacheConfig;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@CacheConfig(cacheNames = { "addresses" })
8+
public class CustomerServiceWithParent extends AbstractService {
9+
//
10+
}

‎spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java‎

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.baeldung.caching.test;
2+
3+
import org.baeldung.caching.config.CachingConfig;
4+
import org.baeldung.caching.example.Customer;
5+
import org.baeldung.caching.example.CustomerDataService;
6+
import org.baeldung.caching.example.CustomerServiceWithParent;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.test.context.ContextConfiguration;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
13+
14+
@RunWith(SpringJUnit4ClassRunner.class)
15+
@ContextConfiguration(classes = { CachingConfig.class }, loader = AnnotationConfigContextLoader.class)
16+
public class SpringCachingIntegrationTest {
17+
18+
@Autowired
19+
private CustomerDataService service;
20+
21+
@Autowired
22+
private CustomerServiceWithParent serviceWithParent;
23+
24+
//
25+
26+
@Test
27+
public void whenGettingAddress_thenCorrect() {
28+
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
29+
service.getAddress(cust);
30+
service.getAddress(cust);
31+
32+
service.getAddress1(cust);
33+
service.getAddress1(cust);
34+
35+
service.getAddress2(cust);
36+
service.getAddress2(cust);
37+
38+
service.getAddress3(cust);
39+
service.getAddress3(cust);
40+
41+
service.getAddress4(cust);
42+
service.getAddress4(cust);
43+
44+
service.getAddress5(cust);
45+
service.getAddress5(cust);
46+
}
47+
48+
@Test
49+
public void givenUsingServiceWithParent_whenGettingAddress_thenCorrect() {
50+
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
51+
52+
serviceWithParent.getAddress(cust);
53+
serviceWithParent.getAddress(cust);
54+
55+
serviceWithParent.getAddress1(cust);
56+
serviceWithParent.getAddress1(cust);
57+
58+
serviceWithParent.getAddress2(cust);
59+
serviceWithParent.getAddress2(cust);
60+
61+
serviceWithParent.getAddress3(cust);
62+
serviceWithParent.getAddress3(cust);
63+
64+
// serviceWithParent.getAddress4(cust);
65+
// serviceWithParent.getAddress4(cust);
66+
67+
serviceWithParent.getAddress5(cust);
68+
serviceWithParent.getAddress5(cust);
69+
}
70+
71+
}

0 commit comments

Comments
 (0)