【问题标题】:How to remove specific object from cache on findAll method?如何从 findAll 方法的缓存中删除特定对象?
【发布时间】:2018-03-31 16:11:43
【问题描述】:

我有 findAll 方法,它返回数据库中的所有对象并被缓存。 但是当我更新某个对象时,我想更新缓存中的那个元素。 但是在 findAll 上我不知道如何用键映射:

 @Cacheable("findAllStudens")
 public Collection<StudensImpl> findAll()

当我更新学生时,我想更新缓存,但只针对那个元素。 有人有同样的问题吗?

【问题讨论】:

    标签: spring caching spring-data ehcache


    【解决方案1】:

    你必须在你的更新方法上使用注解@CachePut,像这样:

    @CachePut(cacheNames = "findAllStudens")
    public void update(StudensImpl studensImpl) {
    ...
    }
    

    【讨论】:

    • 这是只更新缓存中的那个对象的方法吗?
    【解决方案2】:

    你需要在你的更新方法之前添加@CacheEvict(“findAllStudens”)注解

    CacheEvict 注释将从现有缓存中删除数据。下次调用 findAll() 时,方法内的代码将被执行,返回的列表将存储在缓存中

    【讨论】:

    • 我从未使用过它,但似乎@CachePut 正在完成这项工作(Felipe 回答)websystique.com/spring/…
    • @cachePut 将用新值重写缓存,因为键是方法名,值是新对象。
    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    相关资源
    最近更新 更多