【问题标题】:How to get entries from the second level query cache?如何从二级查询缓存中获取条目?
【发布时间】:2011-02-19 23:27:01
【问题描述】:

在我的grails 应用程序中,我想显示所有区域的二级缓存的所有当前条目

我的代码如下:

def getCacheStats() {
  StatisticsImpl stats = sessionFactory.statistics
  for (regionName in stats.secondLevelCacheRegionNames) {
    log.debug stats.getSecondLevelCacheStatistics(regionName).entries
  }
}

但是,只要区域名称不是 org.hibernate.cache.StandardQueryCache(用于Query Cache 的区域),一切都可以正常工作。在这种情况下,会引发异常:

java.lang.ClassCastException: org.hibernate.cache.QueryKey cannot be cast to org.hibernate.cache.CacheKey

通过谷歌搜索,我没有找到任何关于如何显示与区域StandardQueryCacheUpdateTimestampsCache 相关联的缓存查询结果集的条目列表 的线索。

您能帮我找到解决办法吗?

【问题讨论】:

标签: java hibernate grails second-level-cache


【解决方案1】:

这相当复杂,但这应该会让你走得更远。您可以通过 SessionFactory 访问查询缓存,因此假设您可以访问它(例如通过“def sessionFactory”),那么您可以像这样访问底层缓存:

def cache = sessionFactory.queryCache
def realCache = cache.region.@underlyingCache.backingCache
def keys = realCache.keys
for (key in keys) {
    def value = realCache.get(key).value
    // do something with the value
}

请注意,这些值将是一个 Long 值列表。我不确定第一个表示什么(这是一个很大的值,例如 5219682970079232),但其余的是缓存域类实例的 ID。

【讨论】:

  • 再次感谢伯特!!你真是个天才。
  • @BurtBeckwith 在 java 中有这样的解决方法吗?我试过sessionFactory.queryCache,但没用。
  • @GurkiratSingh 你在 java 中找到解决方案了吗?
猜你喜欢
  • 1970-01-01
  • 2012-05-08
  • 2014-11-28
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2014-05-28
相关资源
最近更新 更多