下面列出了org.hibernate.stat.Statistics#getSecondLevelCacheRegionNames ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setMemoryInfo(Model model) {
//系统的
MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
long usedSystemMemory = heapMemoryUsage.getUsed();
long maxSystemMemory = heapMemoryUsage.getMax();
model.addAttribute("usedSystemMemory", usedSystemMemory);
model.addAttribute("maxSystemMemory", maxSystemMemory);
//二级缓存的
Statistics statistics = (Statistics) model.asMap().get("statistics");
String[] secondLevelCacheRegionNames = statistics.getSecondLevelCacheRegionNames();
int totalMemorySize = 0;
int totalMemoryCount = 0;
int totalDiskCount = 0;
for(String secondLevelCacheRegionName : secondLevelCacheRegionNames) {
SecondLevelCacheStatistics secondLevelCacheStatistics =
statistics.getSecondLevelCacheStatistics(secondLevelCacheRegionName);
totalMemorySize += secondLevelCacheStatistics.getSizeInMemory();
totalMemoryCount += secondLevelCacheStatistics.getElementCountInMemory();
totalDiskCount += secondLevelCacheStatistics.getElementCountOnDisk();
}
model.addAttribute("totalMemorySize", totalMemorySize);
model.addAttribute("totalMemoryCount", totalMemoryCount);
model.addAttribute("totalDiskCount", totalDiskCount);
}
/**
* Get all the Hibernate Entities second level cache statistics aggregated in a sorted Map
*
* @param statistics
* @return a sorted map containing all the Hibernate second level cache statistics
*/
public static SortedMap<String, Map<String, HibernateMetric>> getSecondLevelCacheRegionsStats(
Statistics statistics) {
SortedMap<String, Map<String, HibernateMetric>> secondLevelCachesStatMap = new TreeMap<String, Map<String, HibernateMetric>>();
if (statistics.isStatisticsEnabled()) {
String[] cacheRegionNames = statistics.getSecondLevelCacheRegionNames();
Stream.of(cacheRegionNames).forEach(crN -> {
try {
CacheRegionStatistics sLCStats = statistics.getDomainDataRegionStatistics(crN);
SortedMap<String, HibernateMetric> sLCStatMap = new TreeMap<String, HibernateMetric>();
sLCStatMap.put(
"second-level-cache.cache.region.name",
createHibernateMetricItem(
"regionName",
"The name of the region where this data is cached.",
sLCStats.getRegionName()));
sLCStatMap.put(
"second-level-cache.element.count.in.memory",
createHibernateMetricItem(
"elementCountInMemory",
"The number of elements currently in memory within the cache provider.",
sLCStats.getElementCountInMemory() != CacheRegionStatistics.NO_EXTENDED_STAT_SUPPORT_RETURN
? sLCStats.getElementCountInMemory()
: -1));
sLCStatMap.put(
"second-level-cache.element.count.on.disk",
createHibernateMetricItem(
"elementCountOnDisk",
"The number of elements currently stored to disk within the cache provider.",
sLCStats.getElementCountOnDisk() != CacheRegionStatistics.NO_EXTENDED_STAT_SUPPORT_RETURN
? sLCStats.getElementCountOnDisk()
: -1));
sLCStatMap.put(
"second-level-cache.size.in.memory",
createHibernateMetricItem(
"sizeInMemory",
"The size that the in-memory elements take up within the cache provider.",
sLCStats.getSizeInMemory() != CacheRegionStatistics.NO_EXTENDED_STAT_SUPPORT_RETURN
? sLCStats.getSizeInMemory()
: -1));
sLCStatMap.put(
"second-level-cache.hit.count",
createHibernateMetricItem(
"hitCount",
"The number of successful cache look-ups against the region since the last Statistics clearing.",
sLCStats.getHitCount()));
sLCStatMap.put(
"second-level-cache.miss.count",
createHibernateMetricItem(
"missCount",
"The number of unsuccessful cache look-ups against the region since the last Statistics clearing.",
sLCStats.getMissCount()));
double secondLvlCacheHitsRatio = (sLCStats.getHitCount() + sLCStats.getMissCount()) != 0
? ((double) sLCStats.getHitCount() / (sLCStats.getHitCount() + sLCStats.getMissCount())
* 100)
: -1;
sLCStatMap.put(
"second-level-cache.hit.ratio",
createHibernateMetricItem(
"hitRatio",
"The ratio of successful cache look-ups against the region since the last Statistics clearing.",
df2.format(secondLvlCacheHitsRatio)));
sLCStatMap.put(
"second-level-cache.put.count",
createHibernateMetricItem(
"putCount",
"The number of cache puts into the region since the last Statistics clearing.",
sLCStats.getPutCount()));
secondLevelCachesStatMap.put(REGION_STATS_PREFIX + crN, sLCStatMap);
} catch (IllegalArgumentException e) {
// logger.error("The region name could not be resolved: {}", e);
}
});
}
return secondLevelCachesStatMap;
}