java – 是否可以定义通用接口并使用Spring @CacheConfig?

受到
this answer的启发,我试图做到以下几点

@NoRepositoryBean
public interface CacheableRepository<T, ID extends Serializable>
    extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>,       PagingAndSortingRepository<T, ID> {

    @Cacheable()
    T findOne(ID id);

    @Cacheable()
    List<T> findAll();

   @Cacheable()
   Page<T> findAll(Pageable pageable);

   @CacheEvict(allEntries = true)
   <S extends T> S save(S entity);

   @CacheEvict(allEntries = true)
   void delete(ID id);

}

然后使用此接口定义Used存储库

 @CacheConfig(cacheNames={"uniqueCache"})
 public interface SomeObjectRepo extends    CacheableRepository<SomeObject, Long>  {

     @Cacheable(key = "someobject+#p0")
     List<SomeObject> findByType(Integer type);

    @Cacheable(key = "someobject+#p0")
    List<SomeObject> findByCategory(String field);

    @Cacheable(key="someobject+#p0.concat(#p1)")
    List<SomeObject> findByCategoryAndWizardType_id(String field,Integer id);

 }

作品:

上面的缓存适用于findByType,findByCategory,findByCategoryAndWizardType_id

不起作用:

对于CacheableRepository中定义的所有可缓存方法.似乎SomeObjectRepo上的CacheConfig注释不会影响CacheableRepository.

我的问题:

为什么注释不起作用?是否有解决方法使这个结构工作?

谢谢,
橡木

最佳答案 是的,当我们试图支持那种情况时,这就是奥利和我发现的东西.

我建议你在spring数据项目中创建一个问题来请求该功能.

点赞