因此,我目前尝试在 Spring Boot 应用程序中安全尽可能多地缓存。
老兄,我遇到了令人抓狂的事情。 如何处理 saveAll(实体列表) 方法?
public List<WorkaroundEntity> saveAll(List<WorkaroundEntity> workaroundEntities) {
此方法保存所有实体,我当前的缓存如下所示:
/**
* K=WorkaroundEntity.id -- Value=WorkaroundEntity
*/
private static final String CACHE_NAME_WORKAROUNDS = "workarounds";
所以基本上我的 map 有 ID 和实体。 当用户保存所有实体的列表时。从我的角度来看,我只有这个选择:
@CacheEvict(value = CACHE_NAME_WORKAROUNDS, allEntries = true, condition = "#workaroundEntities.size()>=1")
public List<WorkaroundEntity> saveAll(List<WorkaroundEntity> workaroundEntities) {
return workaroundEntityRepository.saveAll(workaroundEntities);
}
我错了吗?针对此类问题有更好的解决方案吗?
到目前为止,我在方法中使用了这个缓存:
public WorkaroundEntity save(WorkaroundEntity workaroundEntity)
public WorkaroundEntity findById(long id)
public void delete(WorkaroundEntity workaroundEntity)
public WorkaroundEntity getOne(long id)
如何处理适用于集合而不是单个实体的方法?
最佳答案
最后,我的答案是同时使用 hibernate 二级缓存和 spring 缓存。我充分利用了这两个词。除此之外,我还创建了额外的内部缓存以减少对数据库的影响。
关于Java spring缓存、saveAll更新的正确处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56139460/