잡동사니

반응형

질문

안녕하세요 저는 EHCache를 사용하여 JPA 앱에서 일부 Hibernate Native Queries의 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 Repository class의 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;
}

답변1

답을 찾았습니다. 해결책은 "캐시 영역"을 사용하는 것입니다. 쿼리에 대한 고유 한 지역을 정의 할 수 있습니다.

예 : "분석"캐시 영역 설정

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


 

 

 

 

출처 : https://stackoverflow.com/questions/59127495/caching-hibernate-native-query-how-can-i-set-the-max-time-to-live-for-a-specif

반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band