From df2f1cbaf89e927b6025b38ddb3791ed85d6bbdc Mon Sep 17 00:00:00 2001 From: Abhi Bavishi Date: Sat, 30 May 2015 15:25:10 +0530 Subject: [PATCH] Updated the caching behavior code: --- .../baeldung/caching/config/myAppConfig.java | 9 ++ .../baeldung/caching/example/Customer.java | 74 +++++++++++++++ .../caching/example/CustomerDataService.java | 93 +++++++++++++++++++ .../org/baeldung/caching/resouces/config.xml | 30 ++++++ .../test/SpringCachingBehaviorTest.java | 31 +++++++ 5 files changed, 237 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..b5313c6eea --- /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..ee8c736141 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/Customer.java @@ -0,0 +1,74 @@ +/** + * The Class Customer. + */ +public class Customer { + + /** The id. */ + private int id; + + /** The name. */ + private String name; + + /** The address. */ + private String customerAddress; + + Customer(String name, String address){ + this.name = name; + this.address = address; + } + + + /** + * Gets the id. + * + * @return the id + */ + public int getId() { + return id; + } + + /** + * Sets the id. + * + * @param id the new id + */ + public void setId(int id) { + this.id = id; + } + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Gets the address. + * + * @return the address + */ + public String getCustomerAddress() { + return this.customerAddress; + } + + /** + * Sets the name. + * + * @param name the new name + */ + public void setName(String name) { + this.name = name; + } + + /** + * Sets the address. + * + * @param name the new address + */ + 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..e773cddec6 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java @@ -0,0 +1,93 @@ +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; + + /** + * Gets the address. + * + * @param customer the customer + * @return the address + */ + + @Cacheable("addresses", “directory”) + + public String getAddress1(Customer customer) { + + return customer.getAddress(); + } + + /** + * Gets the address. + * + * @param customer the customer + * @return the address + */ + + @CacheEvict(value="addresses", allEntries=true) + + public String getAddress2(Customer customer) { + + return customer.getAddress(); + } + + /** + * Gets the address. + * + * @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(); + } + + /** + * Gets the address. + * + * @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(); + } + + /** + * Gets the address. + * + * @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..5097177803 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java @@ -0,0 +1,31 @@ + + +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; + + + +/** + * The Class CustomerDataService. + */ +@Component + +public class SpringCachingBehaviorTest { +/** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + @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); + } + +}