Do I need to block my read-only cache before updating it with Ehcache?

I am doing an Ehcache implementation in a multi-threaded environment. I have 1 thread designed to manage and update the cache, and other threads can refer to it at any time.

The cache is read-only, but is updated every 30 minutes. Does ehchache automatically manage and block read access to the cache when it is updated? Or do I need to wrap using purchaseReadLockOnKey () instructions?

Ehcache cache = getCache(); cache.acquireReadLockOnKey(element); try { cache.put(element); } finally { cache.releaseReadLockOnKey(element); } 

UDPATE: Here is more detailed implementation information. The above code is for the "updateCache ()" method, but the following shows how it was originally created.

 import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Ehcache; public class TTLCacheProviderHelper { private CacheManager cacheManager = null; Ehcache myTtlCache = null; private static final String CACHE_NAME = "myTTLCache"; private static int TTL_VALUE = 1800; private static EvictTimeClock evictTimeClock = null; /** * Constructor */ public TTLCacheProviderHelper() { cacheManager = CacheManager.create(); Cache testCache = new Cache(CACHE_NAME, 100, false, false, 10, 10); Ehcache originalCache = testCache; cacheManager.addCache(originalCache); myTtlCache = cacheManager.getCache(CACHE_NAME); String ttlValue = AppConfigService.getInstance().getTTL(); if(ttlValue!= null) TTL_VALUE = Integer.parseInt(ttlValue); evictTimeClock = new EvictTimeClock(); setEvictTimeClock(TTL_VALUE); } Ehcache getCache(){ return myTtlCache; }` 

And its config: `

  <cache name="configCache" maxElementsInMemory="100000" eternal="true" overflowToDisk="false" /> <cache name="validationTemporaryCache" maxElementsInMemory="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="500" timeToLiveSeconds="500" /> </ehcache>` 
+4
source share
1 answer

If your script read -> flush -> recalculate -> put -> read , then there will be no answer, Ehcache will not automatically block all readers until a new value is calculated. What you need to do is use the read-through mechanism or something similar.

But if your script read -> recalculate -> replace -> read , then I do not see the need for an additional lock.

+3
source

All Articles