I have a Model object like this -
class ProductDTO {
int id;
String code;
String description;
//getters and setters go here
}
I am trying to use Spring 4 with ehcache in a web service with code like this -
class ProductServiceImpl implements ProductService {
List<ProductDTO> getAllProducts() {
//Query to data layer getting all products
}
String getProductDescriptionById(int id) {
//Query to data layer getting product by id
}
@Cacheable("prodCache")
String getProductDescriptionByCode(String code) {
//Query to data layer getting product by code
}
}
The caching works fine on the method getProductDescriptionByCode() which has the cacheable annotation. Every time getProductDescriptionById() or getProductDescriptionByCode() is called, if there is a cache miss, I want to get all products (may be using getAllProducts() but not necessarily) and cache them so that the next time, I can retrieve any of the products. What additions or changes should I make to the annotations or code?