【发布时间】:2023-04-09 11:48:01
【问题描述】:
我是 EhCache 的新手,所以也许我的问题很愚蠢,但无论如何: 我将 EhCache 与 Spring 一起使用。在我的 DAO 中有这个:
@Cacheable(value="product", key="#id")
public Product getProductById(Integer id) {//some code;}
@Cacheable(value="productList")
public List<Product> getAllProducts() {//some code;}
我想在删除或更新某些产品时更新我的 productList 缓存。我可以在我的代码中做这样的事情来解决这个问题吗(在这个例子中产品列表和产品缓存都被删除了):
@Caching(evict={@CacheEvict(value="productList", allEntries=true), @CacheEvict(value="product", key="#id")})
public boolean deleteProductById(Integer id) { //some code;}
也许,有一些方法可以在没有注释的情况下做到这一点。很高兴有任何建议。谢谢。
【问题讨论】:
-
看起来正确。你试过了吗?
-
是的,我试过了。此代码只是删除我的产品和产品列表缓存。但我不想删除 productList 缓存。我想通过 productList 缓存中的某个键找到对象并删除它,而不删除 productList 缓存。我可以这样做吗?
-
你签出
@CachePut了吗? -
是的,但我无法理解如何使用它。我应该这样写:@Caching(evict={ @CacheEvict(value = "product", key = "#id")}, put= { @CachePut(value = "productList", key="#id") }) 上面我的删除方法?也许你有一些例子?谢谢你的回答。
-
尝试使用
@Caching{put={@CachePut(value="productList", key="#id"), @CachePut(value="product", key="#id")}并告诉我
标签: java spring caching annotations ehcache