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; 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>`
source share