我正在使用Spring的缓存工具。当我调用以下方法时,我想逐出数组receiptObject.resolverIds
中所有值的缓存:
@Override
@Caching(evict = {
@CacheEvict(value = "assignedFeedbacks", key = "#receiptObject.resolverIds[0]"),
@CacheEvict(value = "newFeedbacks", key = "#receiptObject.feedbackObject.serviceId") })
public void addReceipt(ReceiptObject receiptObject) throws Exception {
feedbackDao.insertReceipt(receiptObject);
}
在这里,我只使用了第一个元素,但是我想对数组中的所有元素进行概括。我该怎么做?
最佳答案
我猜你在某个地方有这样的@Cacheable
方法:
@Cacheable(value = "assignedFeedbacks")
public void getReceipt(Object receiptId) throws Exception {
由于
@Caching
批注只能一次处理单个缓存项,因此没有理由看看SpEL如何处理数组,因为key
应该是单个对象。我看到的唯一方法是基于直接的缓存抽象用法:
@Autowired
private CacheManager cacheManager;
public void addReceipt(ReceiptObject receiptObject) throws Exception {
feedbackDao.insertReceipt(receiptObject);
Cache cache = cacheManager.getCache("assignedFeedbacks");
for (Object receiptId: receiptObject.getResolverIds()) {
cache.evict(receiptId);
}
}