Java缓存系统 - 查找地图大小

时间:2015-09-29 23:53:06

标签: java caching

我正在使用Java缓存系统(JCS - https://commons.apache.org/proper/commons-jcs/)我需要找到缓存的大小(来自org.apache.commons.jcs.access.CacheAccess类)

使用CacheAccess.getStats()我可以获得一个String,它可以为我提供缓存的统计信息

e.g。

String stats = ((ICacheAccess<String, Book>) cache).getStats();

并会给我很多信息

Region Name = bookCache
HitCountRam = 0
HitCountAux = 0
---------------------------Memory Cache
List Size = 5015
Map Size = 5015
Put Count = 5015
Hit Count = 0
Miss Count = 0
---------------------------Indexed Disk Cache
Is Alive = true
Key Map Size = 0
Data File Length = 0
Hit Count = 0
Bytes Free = 0
Optimize Operation Count = 0
Times Optimized = 0
Recycle Count = 0
Recycle Bin Size = 0
Startup Size = 0
Purgatory Hits = 0
Purgatory Size = 0
Working = true
Alive = false
Empty = true
Size = 0

但我需要的只是地图或列表大小。

任何想法 - 除了正则表达式: - )

2 个答案:

答案 0 :(得分:1)

你可以尝试

List<IStatElement> stats = ((ICacheAccess<String, Book>) cache).getStatistics().getStatsElements();
// using CollectionsUtil from commons-collection
CollectionsUtil.select(stats, new Predicate<IStatElement>() {
    public boolean evaluate(IStatElement elem) {
        String name = elem.getName();
        return "List Size".equals(name) || "Map Size".equals(name);
    }
}); 

答案 1 :(得分:0)

我的解决方案并不像@DJ那样优雅。但是这里有:

    /**
     * Get the AuxiliaryCacheStats from the Statistics for the CacheAccess
     * and from get make a List of the first element
     */
    IStats list = ((ICacheAccess<String, Book>) cache).getStatistics().getAuxiliaryCacheStats().get(0);


    /**
     * Here are the List Size and Map Size
     */
    System.out.println("List Cache : "  + list.getStatElements().get(0));
    System.out.println("List Cache : "  + list.getStatElements().get(1));

    /**
     * Do some substrings
     */
    int listSize = Integer.parseInt(list.getStatElements().get(0).toString().substring(12));
    int mapSize = Integer.parseInt(list.getStatElements().get(1).toString().substring(11));

    System.out.println("From cache list size : " + listSize);
    System.out.println("From cache map size : " + mapSize);