2016-07-28 29 views
0

請參閱Spring Cache可用於緩存服務調用。如何使用具有兩個屬性的彈簧緩存作爲密鑰

特別在下面的XML片段:

<!-- the service we want to make cacheable --> 
<bean id="bookService" class="x.y.service.DefaultBookService"/> 

<!-- cache definitions --> 
<cache:advice id="cacheAdvice" cache-manager="cacheManager"> 
    <cache:caching cache="books"> 
     <cache:cacheable method="findBook" key="#isbn"/> 
     <cache:cache-evict method="loadBooks" all-entries="true"/> 
    </cache:caching> 
</cache:advice> 

<!-- apply the cacheable behavior to all BookService interfaces --> 
<aop:config> 
    <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* x.y.BookService.*(..))"/> 
</aop:config> 

上述機制工作,如果關鍵是一個單一的財產。我需要基於isbn和作者進行緩存。

有人可以建議如何使用兩個屬性啓用緩存嗎?

感謝

回答

1

,你可以很容易地使用key="#author.toString() + #isbn.toString()")或實現自己的KeyGenerator並配置它,或者你只是忽略的關鍵屬性,以便所有參數都考慮在內。

+0

謝謝。那很完美。 – kevin