2015-11-26 65 views
2

我正在使用弹簧数据和缓存。弹簧数据存储库的缓存默认方法

有没有什么办法可以将缓存放在存储库(findOne ...)的默认方法中,而无需在界面中重新声明这些方法,我们创建的方法?

public interface AccountOperationRepository extends JpaRepository<AccountOperation, Long>{ 
    @Cacheable(value = "myCache") 
    AccountOperation findOne(Long id) 
} 

回答

-2

你可以实现你自己的道,基本信息:

public interface CachedDAO<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> { 

@Cacheable(value = "cacheValue") 
T findOne(ID id); 

@Cacheable(value = "cacheValue") 
List<T> findAll(); 

@Cacheable(value = "cacheValue") 
Page<T> findAll(Pageable pageable); 
//Evict because you want to modify value 
@CacheEvict(value = "cacheValue", allEntries = true) 
<S extends T> S save(S entity); 
//the same with delete 
@CacheEvict(value = "cacheValue", allEntries = true) 
void delete(ID id); 
} 

你需要寻找多一点,希望它帮助。

+0

每个对象都将使用相同的名称作为缓存? –

+0

不,你应该有独特的 –