Java Code Examples for org.hibernate.stat.Statistics#getSecondLevelCacheRegionNames()

The following examples show how to use org.hibernate.stat.Statistics#getSecondLevelCacheRegionNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateCacheMonitorController.java    From es with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 2
Source File: HibernateStatsUtils.java    From pnc with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}