本文介绍了休眠第二级缓存 - 打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在应用程序中使用@Cache注释定义了二级缓存。我使用findById查询,如下所示:
$
$ b
long id = 4;
公司cmp = companyDAO.findById(id);
其中Company是我从DB获得的对象。
如何检查公司对象是来自数据库还是来自缓存?
解决方案
HitCount和/或MissCount API。href =http://docs.jboss.org/hibernate/stable/annotations/api/org/hibernate/stat/SecondLevelCacheStatistics.html =nofollow noreferrer> HitCount和/或MissCount API。
类似这样.....
int oldMissCount = 。sessionFactory.getStatistics()方法getSecondLevelCacheStatistics(RNAME).getMissCount();
int oldHitCount = sessionFactory.getStatistics()。getSecondLevelCacheStatistics(rName).getHitCount();
long id = 4;
公司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(from DB from);
} else if(oldHitCount + 1 == newHitCount&& oldMissCount == newMissCount){
logger.debug(came from cache);
}
I defined a second level cache in my application using @Cache annotation
I am using findById query, as the following:
long id = 4;
Company cmp = companyDAO.findById(id);
Where Company is the object that I get from the DB.
How can I check if the Company object came from the DB or from the cache?
解决方案
Try HitCount and/or MissCount API.
Something like this.....
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");
}
这篇关于休眠第二级缓存 - 打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!