2015-11-26 67 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

不,你應該有獨特的 –