繁体   English   中英

缓存 Hibernate Native Query - 如何设置特定查询的最大存活时间?

[英]Caching Hibernate Native Query - How can I set the max time to Live for a specifc query?

嗨,我正在使用 EHCache 来缓存来自 JPA 应用程序中一些 Hibernate Native 查询的 SQL 结果。 我想知道如何为特定查询设置结果应缓存多长时间? 即到 24 小时,但对于其他查询不是?

休眠属性

hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=none
hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

EHCache 配置 XML

<ehcache>
  <diskStore path="java.io.tmpdir"/>
  <defaultCache
     maxEntriesLocalHeap="10000"
     eternal="false"
     timeToIdleSeconds="120"
     timeToLiveSeconds="120"
     maxEntriesLocalDisk="10000000"
     diskExpiryThreadIntervalSeconds="120"
     memoryStoreEvictionPolicy="LRU"
     <persistence strategy="localTempSwap"/>
  />
</ehcache>

Spring 存储库类中的 Java 方法

@Repository
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AnalyticsRepositoryImpl implements AnalyticsRepository {

   public Map<Long, Long> getAgeStatistic(boolean onlyPaid) {

    StringBuilder sb = new StringBuilder();
    sb.append("SELECT (date_part('year',current_date) - date_part('year',a.date_value))  as age, COUNT(u.id) as count from answers a ");
    sb.append("JOIN ...");
    ...
    Query q = em.createNativeQuery(sb.toString());

    // enable the cache for the native query
    NativeQuery nativeQuery = q.unwrap(NativeQuery.class);
    nativeQuery.addScalar("age", IntegerType.INSTANCE);
    nativeQuery.addScalar("count", LongType.INSTANCE);
    nativeQuery.setCacheable(true);

    List<Object[]> result = q.getResultList();

    // Place results in map
    Map<Long, Long> map = convertResultSetToLongKeyMap(result);

    return map;
}

我找到了答案,解决方案是针对我的问题使用“缓存区域”。 所以你可以为你的查询定义一个自己的区域

例如设置“分析”缓存区域

@Repository
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AnalyticsRepositoryImpl implements AnalyticsRepository {

   public Map<Long, Long> getAgeStatistic(boolean onlyPaid) {

    StringBuilder sb = new StringBuilder();
    sb.append("SELECT (date_part('year',current_date) - date_part('year',a.date_value))  as age, COUNT(u.id) as count from answers a ");
    sb.append("JOIN ...");
    ...
    Query q = em.createNativeQuery(sb.toString());

    // enable the cache for the native query
    NativeQuery nativeQuery = q.unwrap(NativeQuery.class);
    nativeQuery.addScalar("age", IntegerType.INSTANCE);
    nativeQuery.addScalar("count", LongType.INSTANCE);
    nativeQuery.setCacheable(true);

    nativeQuery.setCacheRegion("analytics");

    List<Object[]> result = q.getResultList();

    // Place results in map
    Map<Long, Long> map = convertResultSetToLongKeyMap(result);

    return map;
}

并在 ehcache 配置中

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
  <diskStore path="java.io.tmpdir"/>

  <defaultCache
     maxEntriesLocalHeap="10000"
     eternal="false"
     timeToIdleSeconds="120"
     timeToLiveSeconds="120"
     maxEntriesLocalDisk="10000000"
     diskExpiryThreadIntervalSeconds="120"
     memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
     </defaultCache>

   <cache name="analytics"
    maxEntriesLocalHeap="10000"
    eternal="false"
    timeToIdleSeconds="6000"
    timeToLiveSeconds="6000">
        <persistence strategy="localTempSwap"/>
    </cache>

</ehcache>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM