如何在Spring Data JPA CRUDRepository中添加缓存功能 [英] How to add cache feature in Spring Data JPA CRUDRepository
本文介绍了如何在Spring Data JPA CRUDRepository中添加缓存功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在findOne方法中添加Cacheable注释,并在删除或发生方法发生时驱逐缓存。
I want to add "Cacheable" annotation in findOne method, and evict the cache when delete or happen methods happened.
我该怎么做?
推荐答案
virsir,如果您使用Spring Data JPA(仅使用接口),还有一种方法。这里我做了什么,为类似的结构化实体创建了dao:
virsir, there is one more way if you use Spring Data JPA (using just interfaces). Here what I have done, genereic dao for similar structured entities:
public interface CachingDao<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
@Cacheable(value = "myCache")
T findOne(ID id);
@Cacheable(value = "myCache")
List<T> findAll();
@Cacheable(value = "myCache")
Page<T> findAll(Pageable pageable);
....
@CacheEvict(value = "myCache", allEntries = true)
<S extends T> S save(S entity);
....
@CacheEvict(value = "myCache", allEntries = true)
void delete(ID id);
}
这篇关于如何在Spring Data JPA CRUDRepository中添加缓存功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文