안녕하세요 저는 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;
}