Spring Boot With Ehcache

This commit is contained in:
Umesh Awasthi
2019-12-14 15:31:53 -08:00
parent e3a135ecd1
commit fc80382bb5
15 changed files with 50 additions and 2 deletions

0
Spring-Boot/spring-boot-ehcache/.gitignore vendored Normal file → Executable file
View File

View File

0
Spring-Boot/spring-boot-ehcache/.mvn/wrapper/maven-wrapper.jar vendored Normal file → Executable file
View File

View File

0
Spring-Boot/spring-boot-ehcache/mvnw.cmd vendored Normal file → Executable file
View File

0
Spring-Boot/spring-boot-ehcache/pom.xml Normal file → Executable file
View File

View File

@@ -8,6 +8,8 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/customers")
public class CustomerController {
@@ -19,4 +21,9 @@ public class CustomerController {
public Customer getCustomer(@PathVariable Long id){
return customerService.getCustomer(id);
}
@GetMapping
public List<Customer> getCustomers(){
return customerService.getCustomers();
}
}

View File

@@ -6,6 +6,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class CustomerService {
@@ -22,4 +25,19 @@ public class CustomerService {
customer.setEmail("contact-us@javadevjournlal.com");
return customer;
}
@Cacheable(cacheNames = "customerList",key = "'customerList'")
public List<Customer> getCustomers(){
List<Customer> customers = new ArrayList<>();
LOG.info("Returning customer list");
for(int i=0; i< 4; i++){
Customer customer = new Customer();
customer.setCustomerId(Long.valueOf(i));
customer.setFirstName("FirstName"+i);
customer.setLastName("LastName"+i);
customer.setEmail("contact-us@javadevjournlal.com"+i);
customers.add(customer);
}
return customers;
}
}

View File

@@ -8,8 +8,31 @@
</service>
<cache alias="customer">
<key-type>java.lang.Long</key-type>
<value-type>com.javadevjournal.data.Customer</value-type>
<key-type>java.lang.Long</key-type>
<value-type>com.javadevjournal.data.Customer</value-type>
<expiry>
<ttl unit="seconds">10</ttl>
</expiry>
<listeners>
<listener>
<class>com.javadevjournal.config.CustomCacheEventLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>UPDATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>REMOVED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap unit="entries">2000</heap>
<offheap unit="MB">100</offheap>
</resources>
</cache>
<cache alias="customerList">
<key-type>java.lang.String</key-type>
<value-type>java.util.ArrayList</value-type>
<expiry>
<ttl unit="seconds">10</ttl>
</expiry>