带有列表的Spring @Cacheable方法 [英] Spring @Cacheable methods with lists

查看:740
本文介绍了带有列表的Spring @Cacheable方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring 4.1.4应用程序中使用最新的Ehcache.我所拥有的是:

I'm using latest Ehcache in my Spring 4.1.4 application. What I have is:

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的联系人,而其他联系人应该正常查询然后缓存.然后,该缓存应在更新后更新缓存的联系人实体.

What I want to achieve is, that contacts are stored in the cache, and when I'm calling the getContactList method again, that all contacts whose id is already cached are retrieved from the cache and the other ones should be queried normally and then cached. This cache should then update the cached contact entity when it is updated.

我使用的是普通的Spring JDBC和Ehcache,没有JPA,也没有Hibernate.

I'm using plain Spring JDBC and Ehcache, no JPA and no Hibernate.

推荐答案

为我工作.这是我的答案的链接. https://stackoverflow.com/a/60992530/2891027

Worked for me. Here's a link to my answer. https://stackoverflow.com/a/60992530/2891027

TL:DR

@Cacheable(cacheNames = "test", key = "#p0")
public List<String> getTestFunction(List<String> someIds) {

答案中有关我的环境的更多信息.

more info about my enviroment in the answer.

这篇关于带有列表的Spring @Cacheable方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆