引用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 和作者进行缓存。
有人可以建议如何使用两个属性启用缓存吗?
谢谢
最佳答案
您可以轻松使用 key="#author.toString() + #isbn.toString()")
或实现您自己的 keyGenerator 并配置它,或者您只需省略 key 属性,以便所有考虑参数。
关于java - 如何使用两个属性作为key的spring缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38644514/