如何在 Spring Boot 应用程序中的同一方法上使用 @CachePut 和 @CacheEvict? [英] How to use @CachePut and @CacheEvict on the same method in a Spring Boot application?

查看:37
本文介绍了如何在 Spring Boot 应用程序中的同一方法上使用 @CachePut 和 @CacheEvict?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 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 注解可以实现在同一个方法上使用多个不同的缓存注解.

注意:这在使用不同缓存时有效.

根据Spring Cache 抽象文档:

<块引用>

当多个注解如 @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 to true)

  • I don't want my cache to be updated or add a new entry if isDeleted is set to true.

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:

When multiple annotations such as @CacheEvict or @CachePut is needed to be specified on the same method for different caches.

Then to achieve this, the workaround is to use @Caching. @Caching allows multiple nested @Cacheable, @CachePut and @CacheEvict to be used on the same method.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆