Is it possible to define a common interface and use Spring @CacheConfig?

Inspired by this answer I tried to do the following

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

}

Then use this interface to determine which repository to use.

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

 }

Working:

Cache for the above works findByType, findByCategory,findByCategoryAndWizardType_id

Does not work:

For all methods cacheabledefined in CacheableRepository. It seems that the abstract CacheConfigto SomeObjectRepono effect on CacheableRepository.

My question is:

Why doesn't annotation work? Is there a way around this structure?

Thanks Oak

+4
source share
1 answer

, , , .

spring, .

+2

Source: https://habr.com/ru/post/1610544/


All Articles