From 729b5218900ed57b2fd12b3e11c666ef0eb5f1f7 Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Mon, 15 Jun 2015 20:19:00 +0530 Subject: [PATCH] Caching behavior code added with code cleanup --- .../baeldung/caching/config/MyAppConfig.java | 9 ++ .../baeldung/caching/example/Customer.java | 42 ++++++++ .../caching/example/CustomerDataService.java | 96 +++++++++++++++++++ .../org/baeldung/caching/resouces/config.xml | 30 ++++++ .../test/SpringCachingBehaviorTest.java | 28 ++++++ 5 files changed, 205 insertions(+) create mode 100644 spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/example/Customer.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java create mode 100644 spring-all/src/main/java/org/baeldung/caching/resouces/config.xml create mode 100644 spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java diff --git a/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java b/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java new file mode 100644 index 0000000000..042e57bc35 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java @@ -0,0 +1,9 @@ + +@Configuration +@EnableCaching + +public class MyAppConfig { + + // Your configuration code goes here. + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/Customer.java b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java new file mode 100644 index 0000000000..c796cc9c60 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -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; + } +} diff --git a/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java new file mode 100644 index 0000000000..4c72d83245 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -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(); + } + + +} diff --git a/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml new file mode 100644 index 0000000000..2ca26f4468 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/resouces/config.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java new file mode 100644 index 0000000000..259f7719eb --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -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"); + } + +}