2015-09-29 49 views
0

我使用Java緩存系統(JCS - https://commons.apache.org/proper/commons-jcs/)我需要找到一個緩存的大小(從類org.apache.commons.jcs.access.CacheAccess)的Java緩存系統 - 尋找地圖的大小

使用CacheAccess.getStats()我可以得到一個字符串,它給我的緩存統計信息

eg

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 

,但我需要的是地圖或列表的大小。

任何想法 - 比正則表達式:-)其他

回答

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); 
    } 
}); 
+0

感謝您的回答。我可以問一下哪些公共收藏?我有commons-collections4-4.0.jar,但CollectionUtil沒有顯示出來。 – mycowan

+0

這是Collection(收藏)Util,而不是CollectionUtil。請參閱https://commons.apache.org/proper/commons-collections/javadocs/api-release/index.html?org/apache/commons/collections4/CollectionUtils.html –

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);