问题描述
我正在开发一个 Spring Boot 应用程序,我有一个场景将 @CachePut
和 @CacheEvict
放在同一个方法上.
我用下面给出的代码尝试了这个场景:
@CachePut(value=myValue",key=#entity.getId(),#entity.getOrgId()",cacheManager="no-expire-caching")@CacheEvict(value=myValue", key=#entity.getId(),#entity.getOrgId()",cacheManager="no-expire-caching", condition="#entity.isDeleted() == true")公共 MyEntity 保存(布尔标志,MyEntity 实体){如果(标志==真){entity.setIsDeleted(true);返回 myRepo.save(entity);}返回 myRepo.save(entity);}
但这似乎没有按预期工作.
目标:
我希望
@CachePut
注释总是首先执行,因为这将是更新缓存的第一个验证.@CacheEvict
每当满足条件时执行(即isDeleted
字段设置为true
)如果
isDeleted
设置为true
,我不希望更新我的缓存或添加新条目.
有可能在 Spring 中实现吗?我应该如何修改我的代码?
在 Spring 中使用 @Caching
注解可以实现在同一个方法上使用多个不同的缓存注解.
注意:这在使用不同缓存时有效.
当多个注解如 @CacheEvict
或@CachePut
需要在同一个方法上为不同的缓存指定.
然后要实现这一点,解决方法是使用@Caching
.@Caching
允许多个嵌套的 @Cacheable
、@CachePut
和 @CacheEvict
用于相同的方法.
试试这个(如果有效):
@Caching(put={@CachePut(value=myValue",key=#entity.getId(),#entity.getOrgId()",cacheManager="no-expire-caching")}, evict = {@CacheEvict(value="myValue", key="#entity.getId(),#entity.getOrgId()",cacheManager="no-expire-caching", condition="#entity.isDeleted() == true")})公共 MyEntity 保存(布尔标志,MyEntity 实体){如果(标志==真){entity.setIsDeleted(true);返回 myRepo.save(entity);}返回 myRepo.save(entity);}
@Caching Java 文档 供参考.
I'm working on a Spring Boot application where I have a scenario to put @CachePut
and @CacheEvict
on the same method.
I tried the scenario with this code given below :
@CachePut(value="myValue",key="#entity.getId(),#entity.getOrgId()",
cacheManager="no-expire-caching")
@CacheEvict(value="myValue", key="#entity.getId(),#entity.getOrgId()",
cacheManager="no-expire-caching", condition="#entity.isDeleted() == true")
public MyEntity save(boolean flag, MyEntity entity){
if (flag==true){
entity.setIsDeleted(true);
return myRepo.save(entity);
}
return myRepo.save(entity);
}
But this doesn't seem to work as expected.
Objectives :
I want
@CachePut
annotation to always execute first since that will be the first validation to update the cache.@CacheEvict
is executed whenever the condition is satisfied (i.e.isDeleted
field is set totrue
)I don't want my cache to be updated or add a new entry if
isDeleted
is set totrue
.
Is it possible to achieve this in Spring ? How should I modify my code ?
You can achieve the use of multiple different cache annotations on the same method in Spring using @Caching
annotation.
Note: This works when different caches are used.
According to Spring Cache Abstraction Docs:
Try this (if it works) :
@Caching(put={@CachePut(value="myValue",key="#entity.getId(),#entity.getOrgId()",
cacheManager="no-expire-caching")}, evict = {@CacheEvict(value="myValue", key="#entity.getId(),#entity.getOrgId()",
cacheManager="no-expire-caching", condition="#entity.isDeleted() == true")})
public MyEntity save(boolean flag, MyEntity entity){
if (flag==true){
entity.setIsDeleted(true);
return myRepo.save(entity);
}
return myRepo.save(entity);
}
@Caching Java Docs for reference.
这篇关于如何在 Spring Boot 应用程序中的同一方法上使用 @CachePut 和 @CacheEvict?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!