Hibernate二级缓存-打印结果

发布于 2024-09-08 10:39:27 字数 229 浏览 1 评论 0原文

我使用 @Cache 注释在我的应用程序中定义了二级缓存,

我正在使用 findById 查询,如下所示:

  long id = 4;    
        Company cmp = companyDAO.findById(id);

其中 Company 是我从数据库获取的对象。

如何检查 Company 对象是来自数据库还是来自缓存?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小情绪 2024-09-15 10:39:27

如何检查 Company 对象是来自数据库还是来自缓存?

Hibernate 使用特定类别来记录所有二级缓存活动。相关类别是org.hibernate.cache,只需在日志框架的配置中为其启用debug即可。

请参阅第 3.5 章日志记录

How can I check if the Company object came from the DB or from the cache?

Hibernate uses a specific category to Log all second-level cache activity. The relevant category is org.hibernate.cache, just enable debug for it in the configuration of your logging framework.

See Chapter 3.5 Logging.

夏日浅笑〃 2024-09-15 10:39:27

尝试 HitCount 和/或 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");
}

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");
}
夜灵血窟げ 2024-09-15 10:39:27

打开缓存日志记录。

Turn on cache logging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文