How to get entries from the cache of requests of the second level?

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

My code is as follows:

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

However, everything works fine until the domain name is org.hibernate.cache.StandardQueryCache (the region is used for Query Cache ). In this case, an exception is thrown:

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

After a google search, I did not find any tips on how to display a list of cached query result set entries related to the StandardQueryCache and UpdateTimestampsCache .

Could you help me find a solution to this?

+4
source share
1 answer

It is rather complicated, but it should help you. You can access the query cache through SessionFactory, so assuming you have access to this (for example, through "def sessionFactory"), you can go to the following caches as follows:

 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 means (this is a big value, for example 5219682970079232), but the rest are identifiers of instances of the cached domain class.

+3
source

Source: https://habr.com/ru/post/1309880/


All Articles