mysql jpa缓存,如何在Spring Data JPA CRUDRepository中添加缓存功能

  • Post author:
  • Post category:mysql


I want to add “Cacheable” annotation in findOne method, and evict the cache when delete or happen methods happened.

How can I do that ?

解决方案

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 extends JpaRepository, JpaSpecificationExecutor {

@Cacheable(value = “myCache”)

T findOne(ID id);

@Cacheable(value = “myCache”)

List findAll();

@Cacheable(value = “myCache”)

Page findAll(Pageable pageable);

….

@CacheEvict(value = “myCache”, allEntries = true)


S save(S entity);

….

@CacheEvict(value = “myCache”, allEntries = true)

void delete(ID id);

}