我正在尝试在我的项目中使用ehcache ..我在hibernate配置文件中指定了以下属性 -
config.setProperty( “在hibernate.cache.provider_class”, “org.hibernate.cache.EhCacheProvider”); config.setProperty( “hibernate.cache.provider_configuration_file_resource_path”, “ehcache.xml中”); config.setProperty( “hibernate.cache.use_second_level_cache”, “真”); config.setProperty( “hibernate.cache.use_query_cache”, “真”);
现在我仍然不确定结果是来自数据库还是缓存..
我环顾四周,发现 - Hibernate second level cache - print result此人建议使用HitCount / Misscount API
然而,当我尝试使用它时,hitcount和miss计数总是返回0 ...这是我的代码
String rName =“org.hibernate.cache.UpdateTimestampsCache”; 统计数据= HibernateHelper.getInstance()getFactory()getStatistics()。; long oldMissCount = stat.getSecondLevelCacheStatistics(RNAME).getMissCount(); long oldHitCount = stat.getSecondLevelCacheStatistics(RNAME).getHitCount(); UserDAO user = new UserDAO(); user.read(new Long(1));长 newMissCount = stat.getSecondLevelCacheStatistics(RNAME).getMissCount(); long newHitCount = stat.getSecondLevelCacheStatistics(RNAME).getHitCount();
if(oldHitCount + 1 == newHitCount&& oldMissCount + 1 == newMissCount){ System.out.println(“来自DB”); } 否则if(oldHitCount + 1 == newHitCount &安培;&安培; oldMissCount == newMissCount){
System.out.println(“来自缓存”); }
如果我使用它错误请告诉我..在这种情况下应该是rName(区域名称)..
还有其他方法可以确定二级缓存是否正常工作吗?
谢谢
答案 0 :(得分:2)
您需要启用统计信息收集:
config.setProperty("hibernate.generate_statistics", "true");