we have a multi-threaded application that uses Oracle Coherence 3.5 L1/L2 caching heavily (1k requests/second) where performance is critical...
do I need to synchronize access to CacheFactory.getCache()?
should I reuse the NamedCache result for subsequent requests?
currently its doing the following to minimize calls to the CacheFactory and synchronize access to it...
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: after poking around a bit, this seems unnecessary since the Coherence APIs being supposed to be thread safe...seems like I could simplify to just this, correct?
protected static NamedCache getCache(String cacheName)
{
return CacheFactory.getCache(cacheName);
}
after some performance testing...it seemed that reusing the NamedCache did prove slightly faster, so here is where I ended up...removed synchronized, used putIfAbsent() instead
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With