如何使用弹簧缓存基于其主键缓存整体列表 [英] How to cache the list of entires based on its primary key using spring caching
问题描述
我想缓存具有以下方法的存储库类的响应:
@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 缓存?
您需要提供自定义的 Cache
实现.
默认情况下,OOTB, Spring的缓存抽象不会将缓存的方法数组/集合类型的返回值拆分为目标缓存中的单独条目.您必须自己处理.
请参阅我对这个几乎相同的问题的最后答复. /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屋!