java - 是否可以定义通用接口(interface)并使用 Spring @CacheConfig?

标签 java spring spring-data-jpa spring-cache spring-data-redis

灵感来自 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);

}

然后使用这个接口(interface)来定义Used repository

 @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);

 }

作品:

上面的缓存适用于 findByTypefindByCategoryfindByCategoryAndWizardType_id

不起作用:

对于在 CacheableRepository 中定义的所有 cacheable 方法。似乎 SomeObjectRepo 上的 CacheConfig 注释不会影响 CacheableRepository

我的问题:

为什么注释不起作用?是否有解决方法可以使此结构正常工作?

谢谢, 橡木

最佳答案

是的,这几乎就是 Ollie 和我在尝试支持这种情况时发现的结果。

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

关于java - 是否可以定义通用接口(interface)并使用 Spring @CacheConfig?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986807/

相关文章:

java - Spring 数据 JPA : Sorting and paging with joined tables

java - java移动开发工具

java - 如何在 netbeans 中制作属性文件

java - 字符串在 for 循环中被覆盖

spring - 如何停止 org.springframework.web.client.RestTemplate 缓存响应?

java - 我的 application.properties 文件中的属性显示为灰色,我无法配置数据源 : 'url' attribute is not specified

java - 上传的文本文件的编码不正确

spring - 创建自定义 Zuul 过滤器

java - id 具有相同列名的对象的 ManyToMany 关系

java - Spring中如何在一个事务方法中管理多个事务?