* Returns {@link Cache} instance or create new one if not exists.
*
* @return {@link Cache}
*/
public Ehcache initializeCache() {
CacheManager cacheManager = getCacheManagerFactory().getInstance();
Ehcache cache;
if (cacheManager.cacheExists(config.getCacheName())) {
if (LOG.isTraceEnabled()) {
LOG.trace("Found an existing cache: {}", config.getCacheName());
LOG.trace("Cache {} currently contains {} elements",
config.getCacheName(),
cacheManager.getEhcache(config.getCacheName()).getSize());
}
cache = cacheManager.getEhcache(config.getCacheName());
} else {
cache = new Cache(config.getCacheName(),
config.getMaxElementsInMemory(),
config.getMemoryStoreEvictionPolicy(),
config.isOverflowToDisk(),
config.getDiskStorePath(),
config.isEternal(),
config.getTimeToLiveSeconds(),
config.getTimeToIdleSeconds(),
config.isDiskPersistent(),
config.getDiskExpiryThreadIntervalSeconds(),
null);
for (CacheEventListener listener : config.getEventListenerRegistry().getEventListeners()) {
cache.getCacheEventNotificationService().registerListener(listener);
}
for (CacheLoaderWrapper loader : config.getCacheLoaderRegistry().getCacheLoaders()) {
loader.init(cache);
cache.registerCacheLoader(loader);
}
cacheManager.addCache(cache);
if (LOG.isDebugEnabled()) {
LOG.debug("Added a new cache: " + cache.getName());
}
}