【问题标题】:Oracle Coherence CacheFactory.getCache() usage across threads跨线程使用 Oracle Coherence CacheFactory.getCache()
【发布时间】:2012-12-03 23:55:16
【问题描述】:

我们有一个多线程应用程序,它大量使用 Oracle Coherence 3.5 L1/L2 缓存(1k 请求/秒),其中性能至关重要...

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

更新:经过一番探索,这似乎是不必要的,因为 Coherence API 应该是线程安全的......似乎我可以简化为这个,对吗?

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

【问题讨论】:

    标签: oracle-coherence


    【解决方案1】:

    经过一些性能测试...似乎重用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 2013-06-19
      • 2018-06-29
      • 1970-01-01
      • 2023-03-14
      • 2010-09-12
      相关资源
      最近更新 更多