2012-12-03 33 views
3

我們有一個使用的Oracle Coherence 3.5 L1/L2高速緩存重(1K請求/秒),其中性能是至關重要的一個多線程應用程序...Oracle Coherence的CacheFactory.getCache()的用法

  1. 我是否需要同步訪問CacheFactory.getCache()

  2. 我應該重複使用NamedCache後續請求的結果嗎?

目前其執行以下操作,以儘量減少對CacheFactory通話和同步訪問它...

static ConcurrentHashMap<String, NamedCache> cacheMap = new ConcurrentHashMap<String, NamedCache>(); 
protected static NamedCache getCache(String cacheName) 
{ 
    NamedCache cache = cacheMap.get(cacheName); 
    if (cache == null) 
    { 
     cache = CacheFactory.getCache(cacheName); 
     cacheMap.put(cacheName, cache); 
    } 

    return cache; 
} 

UPDATE:張望了一下後戳,這似乎是不必要的,因爲被認爲連貫性的API要線程安全...似乎我可以簡化到這一點,對嗎?

protected static NamedCache getCache(String cacheName) 
{ 
    return CacheFactory.getCache(cacheName); 
} 

回答

2

後一些性能測試......它似乎是重用NamedCache也稍快證明,所以這裏是我結束了......除去同步,使用的putIfAbsent()代替

protected static NamedCache getCache(String cacheName) 
{ 
    NamedCache cache = cacheMap.get(cacheName); 
    if (cache == null) 
    { 
     cache = CacheFactory.getCache(cacheName); 
     NamedCache existing = cacheMap.putIfAbsent(cacheName, cache); 
     if (existing != null) 
      return existing; 
    } 

    return cache; 
} 
相關問題