【问题标题】:Cache default method of spring data repositoryspring数据仓库的缓存默认方法
【发布时间】:2015-11-26 03:10:30
【问题描述】:
我正在使用弹簧数据和缓存。
有什么方法可以将缓存放在存储库的默认方法(findOne...)上,而无需在我们创建的接口中重新声明这些方法?
public interface AccountOperationRepository extends JpaRepository<AccountOperation, Long>{
@Cacheable(value = "myCache")
AccountOperation findOne(Long id)
}
【问题讨论】:
标签:
spring
spring-data
spring-cache
【解决方案1】:
您可以实现自己的 dao,了解基本信息:
public interface CachedDAO<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
@Cacheable(value = "cacheValue1")
T findOne(ID id);
@Cacheable(value = "cacheValue2")
List<T> findAll();
@Cacheable(value = "cacheValue3")
Page<T> findAll(Pageable pageable);
//Evict because you want to modify value
@CacheEvict(value = "cacheValue4", allEntries = true)
<S extends T> S save(S entity);
//the same with delete
@CacheEvict(value = "cacheValue5", allEntries = true)
void delete(ID id);
}
您需要再搜索一下,希望对您有所帮助。
【解决方案2】:
我在spring数据存储库中使用过这样的
@Cacheable(value = "accountIds",sync = true)
default List<AccountDetailDTO> getAccountIds() {
findById();
}