Jav*_*ire 8 spring ehcache jdbctemplate
我在Spring 4.1.4应用程序中使用了最新的Ehcache.我有的是:
class Contact{
int id;
int revision;
}
@Cacheable("contacts")
public List<Contact> getContactList(List<Integer> contactIdList) {
return namedJdbc.queryForList("select * from contact where id in (:idlist)", Collections.singletonMap("idlist", contactIdList));
}
@CachePut(value="contact", key = "id")
public void updateContact(Contact toUpdate) {
jdbctemplate.update("update contact set revision = ? where id = ?", contact.getRevision(), contact.getId());
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是,联系人存储在缓存中,当我getContactList再次调用该方法时,id从缓存中检索已缓存的所有联系人,并且应该正常查询其他联系人然后缓存.然后,此缓存应在更新时更新缓存的联系人实体.
我使用普通的Spring JDBC和Ehcache,没有JPA,也没有Hibernate.
不要认为这是可能的。 List<Integer>将针对返回值的键getContactList将保存在Cache中。
因此,除非输入到您的 ID 列表包含getContactList与之前的调用之一完全相同的 ID,否则这将是缓存未命中,并且将从数据库中获取数据。(注意:如果两个列表完全相同包含相同的元素且顺序相同,则它们被视为相等)
一种选择是将您的方法更改getContactList(List<Integer> contactIdList)为getContact(Integer id)- 在这种情况下,可能需要一段时间来构建缓存,但是一旦给定 ID 的联系人位于缓存中,数据库将不会用于在将来的调用中重新获取它。
虽然不优雅,但另一种选择是在方法中手动进行缓存getContactList。
| 归档时间: |
|
| 查看次数: |
2547 次 |
| 最近记录: |