2017-01-20 64 views
0

我正在使用Spring以及使用@Cacheable@CachePut註釋的內置高速緩存。Spring @CachePut將兩個鍵的值相同

我在我的@Service中有2個方法,一個用於保存數據庫中的值,第二個用於從數據庫中獲取值。他們兩人都使用緩存。

@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註解。

我該怎麼辦?

回答

2

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

真棒,不知道緩存註解! – user3626048

相關問題