gpt4 book ai didi

oracle-coherence - Oracle Coherence CacheFactory.getCache() 跨线程使用

转载 作者:行者123 更新时间:2023-12-02 04:21:21 24 4
gpt4 key购买 nike

我们有一个多线程应用程序,该应用程序大量使用 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);
}

最佳答案

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

关于oracle-coherence - Oracle Coherence CacheFactory.getCache() 跨线程使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13693922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com