Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Coherence CacheFactory.getCache() usage across threads

we have a multi-threaded application that uses Oracle Coherence 3.5 L1/L2 caching heavily (1k requests/second) where performance is critical...

  1. do I need to synchronize access to CacheFactory.getCache()?

  2. 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);
}
like image 345
Ben ODay Avatar asked Dec 09 '25 06:12

Ben ODay


1 Answers

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;
}
like image 143
Ben ODay Avatar answered Dec 12 '25 12:12

Ben ODay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!