How to cache results of a Spring Data JPA query method without using query cache?

后端 未结 3 1295
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 20:00

I have a Spring Boot app with Spring Data JPA (hibernate backend) repository classes. I\'ve added a couple custom finder methods, some with specific @Query anno

相关标签:
3条回答
  • 2020-12-12 20:03

    First I quote your question:

    What am I doing wrong?

    The way you are trying to name the cache is not appropriate to how hibernate will use it. Check org.hibernate.engine.spi.CacheInitiator which uses org.hibernate.internal.CacheImpl which is based on:

    if ( settings.isQueryCacheEnabled() ) {
        final TimestampsRegion timestampsRegion = regionFactory.buildTimestampsRegion(
                qualifyRegionName( UpdateTimestampsCache.REGION_NAME ),
                sessionFactory.getProperties()
        );
        updateTimestampsCache = new UpdateTimestampsCache( sessionFactory, timestampsRegion );
        ...
    }
    

    And UpdateTimestampsCache.REGION_NAME (equals to org.hibernate.cache.spi.UpdateTimestampsCache) is what you are missing as the cache name. For the query cache you'll have to use exactly that cache name and no other!

    Now few other thoughts related to your problem:

    • removing @Cache and setting cache name to org.hibernate.cache.spi.UpdateTimestampsCache will allow your query to be cached with ehcache by hibernate (spring cache abstraction is not involved here)
    • setting a hardcoded cache name won't make you happy I'm sure but at least you know why this happens
    • Balamaci Serban (the post just below) is painfully right

    Below is the configuration from one of my projects where ehcache + @Query + @QueryHints work as expected (ehcache/ehcache-in-memory.xml file):

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache name="in-memory" xmlns="http://ehcache.org/ehcache.xsd">
        <!--<diskStore path="java.io.tmpdir"/>-->
    
        <!--
            30d = 3600×24×30 = 2592000
        -->
    
        <cache name="org.hibernate.cache.internal.StandardQueryCache"
               maxElementsInMemory="9999" eternal="false"
               timeToIdleSeconds="2592000" timeToLiveSeconds="2592000"
               overflowToDisk="false" overflowToOffHeap="false"/>
    
        <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
               maxElementsInMemory="9999" eternal="true"
               overflowToDisk="false" overflowToOffHeap="false"/>
    
        <defaultCache maxElementsInMemory="9999" eternal="false"
                      timeToIdleSeconds="2592000" timeToLiveSeconds="2592000"
                      overflowToDisk="false" overflowToOffHeap="false"/>
    </ehcache>
    

    and hibernate.properties:

    hibernate.jdbc.batch_size=20
    hibernate.show_sql=true
    hibernate.format_sql=true
    hibernate.validator.autoregister_listeners=false
    hibernate.cache.use_second_level_cache=true
    hibernate.cache.use_query_cache=true
    hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
    hibernate.hbm2ddl.auto=update
    net.sf.ehcache.configurationResourceName=ehcache/ehcache-in-memory.xml
    hibernate.dialect=org.hibernate.dialect.H2Dialect
    

    and some versions from pom.xml for which my explanation applies:

    <springframework.version>5.0.6.RELEASE</springframework.version>
    <spring-security.version>5.0.5.RELEASE</spring-security.version>
    <spring-data-jpa.version>2.1.0.RELEASE</spring-data-jpa.version>
    <hibernate.version>5.2.13.Final</hibernate.version>
    <jackson-datatype-hibernate5.version>2.9.4</jackson-datatype-hibernate5.version>
    

    And the full working test is image.persistence.repositories.ImageRepositoryTest.java found here: https://github.com/adrhc/photos-server/tree/how-to-cache-results-of-a-spring-data-jpa-query-method-without-using-query-cache
    Yep, run mvn clean install or change my env.sh if you really want to use my shell scripts. Check then the number of sql queries on behalf of 3x imageRepository.count() call.

    0 讨论(0)
  • 2020-12-12 20:13

    You need to be aware that by giving up on the Hibernate QueryCache your are responsible for invalidating the queries that become stale when saving, updating, deleting entities that influenced the query result(what Oliver is doing by setting CacheEvict on save) - which I think can be a pain- or at least you need to take into account and ignore it if it's not really a problem for your scenario.

    0 讨论(0)
  • 2020-12-12 20:23

    The reason the code you have is not working is that @Cache is not intended to work that way. If you want to cache the results of a query method execution, the easiest way is to use Spring's caching abstraction.

    interface PromotionServiceXrefRepository extends PagingAndSortingRepository<PromotionServiceXref, Integer> {
    
      @Query("…")
      @Cacheable("servicesByCustomerId")
      Set<PromotionServiceXref> findByCustomerId(int customerId);
    
      @Override
      @CacheEvict(value = "servicesByCustomerId", key = "#p0.customer.id")
      <S extends PromotionServiceXref> S save(S service);
    }
    

    This setup will cause results of calls to findByCustomerId(…) be cached by the customer identifier. Note, that we added an @CacheEvict to the overridden save(…) method, so that the cache we populate with the query method is evicted, whenever an entity is saved. This probably has to be propagated to the delete(…) methods as well.

    Now you can go ahead an configure a dedicated CacheManager (see the reference documentation for details) to plug in whichever caching solution you prefer (using a plain ConcurrentHashMap here).

     @Configuration
     @EnableCaching
     class CachingConfig {
    
       @Bean
       CacheManager cacheManager() {
    
         SimpleCacheManager cacheManager = new SimpleCacheManager();
         cacheManager.addCaches(Arrays.asList(new ConcurrentMapCache("servicesByCustomerId)));
    
         return cacheManager;
       }
     }
    
    0 讨论(0)
提交回复
热议问题