How to get entries from the second level query cache?

人盡茶涼 提交于 2020-01-02 05:17:20

问题


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 please help me find a solution for this?


回答1:


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.



来源:https://stackoverflow.com/questions/2841885/how-to-get-entries-from-the-second-level-query-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!