本文介绍了Java Spring缓存,save所有对更新的正确处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我目前尝试在我的Spring Boot应用程序中尽可能多地安全进行缓存.

So I currently try to cache as much as possible SAFELY in my spring boot application.

伙计,我碰到了脑部刮擦.我该如何处理saveAll(实体列表)方法?

Bud i run into brain scratcher.How do i handle saveAll(list of entities) method ?

 public List<WorkaroundEntity> saveAll(List<WorkaroundEntity> workaroundEntities) {

此方法保存所有实体,我当前的缓存如下:

This method saves all entities, My current cache looks like this :

  /**
    * K=WorkaroundEntity.id   --   Value=WorkaroundEntity
   */
   private static final String CACHE_NAME_WORKAROUNDS = "workarounds";

因此,基本上我的地图具有ID和实体.用户保存所有实体的列表时.从我的角度来看,我只有这个选项:

So basically my map has IDs AND entities.When user saves LIST OF ALL ENTITIES. From my point of view I only have this option :

@CacheEvict(value = CACHE_NAME_WORKAROUNDS, allEntries = true, condition = "#workaroundEntities.size()>=1")
public List<WorkaroundEntity> saveAll(List<WorkaroundEntity> workaroundEntities) {
    return workaroundEntityRepository.saveAll(workaroundEntities);
}

我错了吗,对于这种问题有更好的解决方案吗?

到目前为止,我在方法中使用此缓存:

So far i use this cache in methods :

public WorkaroundEntity save(WorkaroundEntity workaroundEntity)
public WorkaroundEntity findById(long id)
public void delete(WorkaroundEntity workaroundEntity) 
public WorkaroundEntity getOne(long id) 

您如何处理适用于集合而不是单个实体的方法?

推荐答案

最后,对我来说,答案是一起使用hibernate 2级缓存和spring缓存.我两个字都很好.除此之外,我还创建了一个附加的内部缓存,以减少对数据库的访问.

In the end , answer for me was to use hibernate 2nd level cache and spring cache together. I got best of both words. In addition to this i have created additiona linternal caches to hit database even less.

这篇关于Java Spring缓存,save所有对更新的正确处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:55