Caching with ehcache-spring-annotations API

This blog contains a simple example of spring ehcache. This cache is use to save static data in form of Object. This example has two components, service and model; here service contains basic curd operations, along with a cache operation.

Project Structure
spring-ehcach
  • src\main\java
    • com.wiki.dto
      • AccountDTO
      • AccountListDTO
    • com.wiki.service
      • AccountDataService
    • com.wiki.service.impl
      • AccountDataServiceImpl
  • src\main\resources
      • account-ehcache.xml
      • spring-ehcache-context.xml
  • src\test\java
    • com.wiki
      • AllTests
    • com.wiki.service
      • AccountDataServiceTest
  • src\test\resources
    • spring-ehcache-test-context.xml
Main Package
AccountDTO
Account DTO with basic account information like accountId, accountHolderName, accountEmail, accountContactNo, accountDOB, currency
AccountListDTO
List of Account DTO
AccountDataService
Interface of AccountDataService
AccountDataServiceImpl
Actual implantation of AccountDataService with curd oprations
account-ehcache.xml
Ehcache operations descriptor File
spring-ehcache-context.xml
Spring context descriptor File
Test Package
AllTests
All Test Cases Starting Point.
AccountDataServiceTest
Account Data Service Test Case
spring-ehcache-test-context.xml
Spring context descriptor File for test package.


  1. Create a maven project spring-ehcache
  2. Add below mention required dependencies in pom.(Pom.xml)
  3. Prepare AccountDTO, and AccountListDTO.
  4. Prepare an Interface for Service (AccountDataService).
  5. Implements of service interface (AccountDataServiceImpl). Prepare cached method cached account Data
  6. Prepare spring wiring for service classes and configure cache manager with service.
  7. Configure accountDataService bean an
  8. Configure cache Manager and refresh interval time, and cache configuration file.
Please build your project. To test your AccountDataService, we have AccountDataServiceTest. Through this we can check caching debug in dubug mode.
Request 1
[AccountDataServiceTest] => [AccountDataService] => [AccountDataServiceImpl] => [EhcacheInterCepter] => [CachedAccountData]

Request 2
[AccountDataServiceTest] => [AccountDataService] => [EhcacheInterCepter] => [CachedAccountData]

First request call actual service to retrieve data from AccountDataService and store it to cached via EhcacheInterCepter.

Second request call
EhcacheInterCepter to retrieve cached data from EhcacheInterCepter. Actual AccountDataService will call after given interval.

To simulate this poc, please checkout code from url (http://code.google.com/p/wikipoc/source/browse/trunk/spring-ehcache/)

Comments

Popular Posts