我正在使用Hibernate 3.3.4.GA.在我們的hibernate.cfg.xml文件,我們指定...休眠:麻煩啓用二級緩存
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_configuration">classpath:ehcache.xml</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
但在零命中計數和零命中次數,這完全是混淆執行測試對DB(從JUnit的)結果...
@Test
public void testCache() {
final String cacheRegion = WebLead.class.getCanonicalName();
final SecondLevelCacheStatistics settingsStatistics = sessionFactory.getStatistics().getSecondLevelCacheStatistics(cacheRegion);
// Make first query.
webLeadsDAO.getLeads(lead);
System.out.println("miss count:" + settingsStatistics.getMissCount());
System.out.println("hit count:" + settingsStatistics.getHitCount());
// Make second query, expect this to hit cache.
webLeadsDAO.getLeads(lead);
System.out.println("after second query, hit count: " + settingsStatistics.getHitCount());
}
下面是我們用於檢索結果的方法。任何錯過計數和命中數的想法都是零?
@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
@Table(name="LEAD")
public WebLeads getLeads(WebLead webLead) {
final Session session = sessionFactory.openSession();
final Criteria crit = session.createCriteria(WebLead.class);
crit.setCacheable(Boolean.TRUE);
// Find webLeads matching input
crit.add(Example.create(webLead));
// Make special exception for primary key since that isn't covered by Example.create
if (webLead.getLEAD_ID() != null) {
crit.add(Restrictions.eq("LEAD_ID", webLead.getLEAD_ID()));
} // if
@SuppressWarnings("unchecked")
final List<WebLead> results = crit.list();
final WebLeads webLeads = new WebLeads();
webLeads.addAll(results);
session.close();
return webLeads;
}
顯然緩存未啓用,但我不明白爲什麼。 - 戴夫
我的配置是一樣的(除非您禁用統計數據),它仍然是行不通的。爲什麼錯過和命中總是零?如果我們沒有擊中緩存,不應該錯過一些數字嗎? – Dave
不要錯過ehcache.xml中緩存實體或xml片段的註釋嗎?這個例子很適合我。我不知道可能是什麼問題。 – lepike
我編輯了我的resopnse - 我有註釋。我認爲緩存工作正常,但統計軟件包不是 - 爲什麼總是返回零點擊和失誤?你如何啓用SecondLevelCacheStatistics? – Dave