我在应用程序定义的二级缓存使用@Cache注释Hibernate的二级缓存 - 打印结果
我使用findById查询,如下:
long id = 4;
Company cmp = companyDAO.findById(id);
凡公司为对象,我从数据库中获得。
如何检查公司对象是来自数据库还是来自缓存?
我在应用程序定义的二级缓存使用@Cache注释Hibernate的二级缓存 - 打印结果
我使用findById查询,如下:
long id = 4;
Company cmp = companyDAO.findById(id);
凡公司为对象,我从数据库中获得。
如何检查公司对象是来自数据库还是来自缓存?
尝试HitCount and/or MissCount API。
像这样的东西.....
int oldMissCount = sessionFactory.getStatistics().getSecondLevelCacheStatistics(rName).getMissCount();
int oldHitCount = sessionFactory.getStatistics().getSecondLevelCacheStatistics(rName).getHitCount();
long id = 4;
Company cmp = companyDAO.findById(id);
int newMissCount = sessionFactory.getStatistics().getSecondLevelCacheStatistics(rName).getMissCount();
int newHitCount = sessionFactory.getStatistics().getSecondLevelCacheStatistics(rName).getHitCount();
if(oldHitCount+1 == newHitCount && oldMissCount+1 == newMissCount) {
logger.debug("came from DB");
} else if(oldHitCount+1 == newHitCount && oldMissCount == newMissCount) {
logger.debug("came from cache");
}
打开缓存记录。
如何检查公司对象是来自数据库还是来自缓存?
Hibernate使用特定类别记录所有二级缓存活动。相关类别为org.hibernate.cache
,只需在配置日志框架时启用调试即可。
我试过这段代码,但是misscount和hit count总是返回0 ...我可以做出的任何错误?? – Anand 2011-01-02 03:12:28
@Anand你确定你已经启用了hibernate.cache.use_second_level_cache,hibernate.cache.use_query_cache,hibernate.generate_statistics? http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional – dira 2011-01-04 05:51:36
如何找出实体的区域名称(rName)? – Sriram 2015-06-26 11:26:23