我在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());
}
[我想要实现的是,将联系人存储在缓存中,并且当我再次调用getContactList
方法时,将从缓存中检索所有已缓存id
的联系人,而其他联系人应该正常查询然后缓存。然后,此缓存应在更新时更新缓存的联系人实体。
我使用的是普通的Spring JDBC和Ehcache,没有JPA也没有Hibernate。
认为不可能。 List<Integer>
将是键,而getContactList
的返回值将保存在缓存中。
因此,除非正在输入到getContactList
的ID列表包含与先前调用之一完全相同的ID,否则它将是缓存未命中,并且将从DB中获取数据。 (注意:如果两个列表完全包含相同的元素且顺序相同,则认为它们相等)
[一种选择是将您的方法getContactList(List<Integer> contactIdList)
更改为getContact(Integer id)
-在这种情况下,可能需要一段时间才能构建缓存,但是一旦有给定ID的联系人进入缓存,DB将不会被用于重新获取在以后的电话中。
虽然不是很好,但是另一个选择是用getContactList
方法手动进行缓存。
为我工作。这是我的答案的链接。https://stackoverflow.com/a/60992530/2891027
TL:DR
@Cacheable(cacheNames = "test", key = "#p0")
public List<String> getTestFunction(List<String> someIds) {
答案中有关我的环境的更多信息。