如何使用以两个属性为键的 spring 缓存

how to use spring cache with two properties as key

参考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 属性以便考虑所有参数。