繁体   English   中英

Spring @CachePut用两个键放置相同的值

[英]Spring @CachePut to put the same value with two keys

我正在使用带有@Cacheable@CachePut注释的@Cacheable缓存的Spring

我的@Service中有两个方法,一个用于保存数据库中的值,另一个用于从数据库中获取值。 他们都使用缓存。

@CachePut(key = "#code")
MyObject saveMyObject(MyObject o, String code) {
    return dao.save(o);
}

@Cacheable(key = "#code")
MyObject getMyObject(String code) {
    return dao.getMyObject(code);
}

在保存对象的同时,我想把它放在另一个缓存中,例如

@CachePut(key = "'TMP_'.concat(#code)")

但我不能在saveMyObject方法上使用两个@CachePut注释。

我该怎么办?

您可以使用org.springframework.cache.annotation.Caching注释对CachePut进行分组:

@Caching( put = {
        @CachePut(key = "#code"),
        @CachePut(key = "'TMP_'.concat(#code)")
})
MyObject saveMyObject(MyObject o, String code) {
    return dao.save(o);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM