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

use*_*048 2 java spring spring-cache

我正在使用Spring内置缓存使用@Cacheable@CachePut注释.

我的@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);
}
Run Code Online (Sandbox Code Playgroud)

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

@CachePut(key = "'TMP_'.concat(#code)")
Run Code Online (Sandbox Code Playgroud)

但我不能@CachePutsaveMyObject方法上使用两个注释.

我该怎么办?

Myk*_*iuk 8

您可以使用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);
}
Run Code Online (Sandbox Code Playgroud)