开发者

How to get entries from the second level query cache?

开发者 https://www.devze.com 2022-12-31 10:41 出处:网络
In my grails application, I want to display all the current entries of the second-level cache from all regions.

In my grails application, I want to display all the current entries of the second-level cache from all regions.

My code is as following :

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

However everything works fine as long as the region name is not org.hibernate.cache.StandardQueryCache (region used for Query Cache). In that case, an exception is thrown :

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

Having googling around, I didn't find any clues about how to display the list of entries of the cached query result sets associated with regions StandardQueryCache and UpdateTimestampsCache.

Could you plea开发者_如何学JAVAse help me find a solution for this?


It's fairly complicated but this should get you further. You can access the query cache via the SessionFactory, so assuming you have access to that (e.g. via 'def sessionFactory') then you can get to the underlying caches like this:

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
}

Note that the values will be a List of Long values. I'm not sure what the first one signifies (it's a large value, e.g. 5219682970079232), but the remaining are the IDs of the cached domain class instances.

0

精彩评论

暂无评论...
验证码 换一张
取 消