如何使用弹簧缓存基于其主键缓存整体列表 [英] How to cache the list of entires based on its primary key using spring caching

查看:76
本文介绍了如何使用弹簧缓存基于其主键缓存整体列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缓存具有以下方法的存储库类的响应:

@Cacheable(cacheNames = "books", key="#id")
Book findById(Long Id);

@CacheEvict(cacheNames = "books", key = "#id")
void deleteById(Long id);

@Cacheable(cacheNames = "books", key="#book.id")
Book save(Book book);

@Cacheable("books")
List<Book> findAll();

除了 findAll()方法外,其他方法都按预期工作.

如何使用 book.id 作为键,使 findAll()填充 books 缓存?

解决方案

您需要提供自定义的几乎相同的问题的最后答复. /p>

I want to cache the response of the repository class which has the following methods:

@Cacheable(cacheNames = "books", key="#id")
Book findById(Long Id);

@CacheEvict(cacheNames = "books", key = "#id")
void deleteById(Long id);

@Cacheable(cacheNames = "books", key="#book.id")
Book save(Book book);

@Cacheable("books")
List<Book> findAll();

Except the findAll() method, others are working as expected.

How to make findAll() to populate the books cache with book.id as key?

解决方案

You need to provide a customized CacheManager and Cache implementation for your caching provider (e.g. Ehcache, Redis or Hazelcast).

By default, OOTB, Spring's Cache Abstraction does not split up cached method array/collection type return values into separate entries in the targeted cache. You must handle this yourself.

See my last response for this nearly identical question.

这篇关于如何使用弹簧缓存基于其主键缓存整体列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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