I am using Spring 3.1 Cache using EhCache currently to implement method caching. Consider the code fragment below:
@Cacheable("items")
public Item findByPK(int itemID) {
String sql = "SELECT * FROM ITEM WHERE ITEM_ID = ?";
Item item = getJdbcTemplate().queryForObject(sql, new Object[]{itemID}, new ItemRowMapper());
return item;
}
@Cacheable("items")
public List<Item> findAll() {
String sql = "SELECT * FROM ITEM";
List<Item> items = getJdbcTemplate().query(sql,new ItemRowMapper());
return items;
}
If I call findByPK() it hits the database first, then onwards it hits the cache, so the method caching works. findAll() ditto. However, is there any way to instruct spring to make findByPK() calls recognize the results returned by findAll()?
This is a major hack, but it would give you the functionality you want: