【问题标题】:Spring @CachePut to put the same value with two keysSpring @CachePut 用两个键放置相同的值
【发布时间】:2017-01-20 15:38:59
【问题描述】:

我正在使用带有内置缓存的 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);
}

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

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

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

我该怎么办?

【问题讨论】:

    标签: java spring spring-cache


    【解决方案1】:

    您可以使用 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);
    }
    

    【讨论】:

    • 太棒了,不知道缓存注解!
    • @CachePut(key = "'TMP_' + #code") 是有效的较短语法。不知道哪个更好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 2018-09-02
    • 2016-06-20
    • 2012-06-06
    • 1970-01-01
    相关资源
    最近更新 更多