【问题标题】: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();
        }
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 2021-11-08
      • 2021-05-16
      • 2020-08-19
      • 2013-01-17
      • 2015-03-13
      • 2017-05-02
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多