1

我正在使用带有 Spring 框架的 Hibernate,并尝试缓存通过 CriteriaAPI 编写的查询。我启用了查询缓存和二级缓存。我还在该特定查询中将 setCacheable 设置为 true。问题是休眠会话统计显示它会针对我多次运行的同一查询命中数据库所有内容。

这是带有条件查询的函数

@Override
public Collection<Article> findLatestArticle(int count) {
    // TODO Auto-generated method stub
    Criteria criteria = this.getSession()
    .createCriteria(Article.class)
    .setCacheable(true)
    .setCacheRegion(CACHE_REGION)
    .add(Restrictions.eq("isVisible", 1))
    //.createCriteria("Count")
    //.add(Restrictions.eq("Count", count))
    .setFirstResult(0)
    .setMaxResults(count);

    return criteria.list();
}

这是调用者函数

@Test
public void t1(){
    List<Article> articleList = (List<Article>) articleDao.findLatestArticle(1);

    //Article a = articleDao.findById(articlePk);

    for(int i=0; i<19; i++)
        articleDao.findLatestArticle(1);

    Statistics stats = sessionFactory.getCurrentSession().getSessionFactory().getStatistics();

    //System.out.println("2nd Lvl Stats: " + stats.getSecondLevelCacheStatistics(CACHE_REGION));
    stats.logSummary();

    System.out.println("Empty: " + articleList.isEmpty());

    assertTrue(!articleList.isEmpty());
    //assertNotNull(a);
}

spring-hibernate-context 文件

<property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.cache.use_structured_entries">true</prop>
            <prop key="net.sf.Ehcache.configurationResourceName">ehcache.xml</prop>
        </props>
    </property>

这个类的ehcache部分

<cache name="com.contento3.cms.article.model.Article"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="60"
overflowToDisk="true"/>

日志摘录:

    2012-11-14 21:05:03,020 [main] INFO  org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
2012-11-14 21:05:03,020 [main] INFO  org.hibernate.cfg.SettingsFactory - Query cache: enabled
    2012-11-14 21:05:03,969 [main] INFO  org.hibernate.cache.StandardQueryCache - starting query cache at region: com.contento3.cms.article.model.Article
2012-11-14 21:05:03,998 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 10ms, rows: 1
2012-11-14 21:05:04,010 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 1ms, rows: 1
2012-11-14 21:05:04,019 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 8ms, rows: 1
2012-11-14 21:05:04,022 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,024 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,026 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,033 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 7ms, rows: 1
2012-11-14 21:05:04,035 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,038 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,041 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,050 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 3ms, rows: 1
2012-11-14 21:05:04,059 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 9ms, rows: 1
2012-11-14 21:05:04,062 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 3ms, rows: 1
2012-11-14 21:05:04,067 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 4ms, rows: 1
2012-11-14 21:05:04,074 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 5ms, rows: 1
2012-11-14 21:05:04,077 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,079 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 1ms, rows: 1
2012-11-14 21:05:04,081 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 1ms, rows: 1
2012-11-14 21:05:04,083 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,085 [main] INFO  org.hibernate.stat.Statistics - HQL: from Article as a, time: 2ms, rows: 1
2012-11-14 21:05:04,085 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - Logging statistics....
2012-11-14 21:05:04,085 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - start time: 1352909103053
2012-11-14 21:05:04,085 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - sessions opened: 1
2012-11-14 21:05:04,085 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - sessions closed: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - transactions: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - successful transactions: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - optimistic lock failures: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - flushes: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - connections obtained: 1
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - statements prepared: 21
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - statements closed: 21
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - second level cache puts: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - second level cache hits: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - second level cache misses: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - entities loaded: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - entities updated: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - entities inserted: 1
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - entities deleted: 0
2012-11-14 21:05:04,086 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - entities fetched (minimize this): 0
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - collections loaded: 0
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - collections updated: 0
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - collections removed: 0
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - collections recreated: 0
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - collections fetched (minimize this): 0
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - queries executed to database: 20
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - query cache puts: 20
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - query cache hits: 0
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - query cache misses: 20
2012-11-14 21:05:04,087 [main] INFO  org.hibernate.stat.ConcurrentStatisticsImpl - max query time: 10ms

我对 Spring 和 Hibernate 还很陌生。我已经阅读了至少十几篇关于这个问题的文章,并尽我所能。另外,我使用 createQuery 方法写了一个类似的查询,结果是一样的。任何帮助,将不胜感激。

4

1 回答 1

0

尝试使用缓存策略为此 @Cacheable @cache 启用注释

于 2013-11-22T06:56:57.180 回答