Search in sources :

Example 1 with CacheManager

use of net.sf.ehcache.CacheManager in project hibernate-orm by hibernate.

the class EhCacheRegionFactory method start.

@Override
public void start(SessionFactoryOptions settings, Properties properties) throws CacheException {
    this.settings = settings;
    if (manager != null) {
        LOG.attemptToRestartAlreadyStartedEhCacheProvider();
        return;
    }
    try {
        String configurationResourceName = null;
        if (properties != null) {
            configurationResourceName = (String) properties.get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
        }
        if (configurationResourceName == null || configurationResourceName.length() == 0) {
            final Configuration configuration = ConfigurationFactory.parseConfiguration();
            manager = new CacheManager(configuration);
        } else {
            final URL url = loadResource(configurationResourceName);
            final Configuration configuration = HibernateEhcacheUtils.loadAndCorrectConfiguration(url);
            manager = new CacheManager(configuration);
        }
        mbeanRegistrationHelper.registerMBean(manager, properties);
    } catch (net.sf.ehcache.CacheException e) {
        if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " + "CacheManager using the diskStorePath")) {
            throw new CacheException("Attempt to restart an already started EhCacheRegionFactory. " + "Use sessionFactory.close() between repeated calls to buildSessionFactory. " + "Consider using SingletonEhCacheRegionFactory. Error from ehcache was: " + e.getMessage());
        } else {
            throw new CacheException(e);
        }
    }
}
Also used : Configuration(net.sf.ehcache.config.Configuration) CacheException(org.hibernate.cache.CacheException) CacheManager(net.sf.ehcache.CacheManager) URL(java.net.URL)

Example 2 with CacheManager

use of net.sf.ehcache.CacheManager in project CloudStack-archive by CloudStack-extras.

the class GenericDaoBase method createCache.

@DB(txn = false)
protected void createCache(final Map<String, ? extends Object> params) {
    final String value = (String) params.get("cache.size");
    if (value != null) {
        final CacheManager cm = CacheManager.create();
        final int maxElements = NumbersUtil.parseInt(value, 0);
        final int live = NumbersUtil.parseInt((String) params.get("cache.time.to.live"), 300);
        final int idle = NumbersUtil.parseInt((String) params.get("cache.time.to.idle"), 300);
        _cache = new Cache(getName(), maxElements, false, live == -1, live == -1 ? Integer.MAX_VALUE : live, idle);
        cm.addCache(_cache);
        s_logger.info("Cache created: " + _cache.toString());
    } else {
        _cache = null;
    }
}
Also used : CacheManager(net.sf.ehcache.CacheManager) Cache(net.sf.ehcache.Cache)

Example 3 with CacheManager

use of net.sf.ehcache.CacheManager in project cloudstack by apache.

the class IAMServiceImpl method createIAMCache.

private void createIAMCache(final Map<String, ? extends Object> params) {
    final String value = (String) params.get("cache.size");
    if (value != null) {
        final CacheManager cm = CacheManager.create();
        final int maxElements = NumbersUtil.parseInt(value, 0);
        final int live = NumbersUtil.parseInt((String) params.get("cache.time.to.live"), 300);
        final int idle = NumbersUtil.parseInt((String) params.get("cache.time.to.idle"), 300);
        _iamCache = new Cache(getName(), maxElements, false, live == -1, live == -1 ? Integer.MAX_VALUE : live, idle);
        cm.addCache(_iamCache);
        s_logger.info("IAM Cache created: " + _iamCache.toString());
    } else {
        _iamCache = null;
    }
}
Also used : CacheManager(net.sf.ehcache.CacheManager) Cache(net.sf.ehcache.Cache)

Example 4 with CacheManager

use of net.sf.ehcache.CacheManager in project spring-framework by spring-projects.

the class EhCacheManagerUtils method buildCacheManager.

/**
	 * Build an EhCache {@link CacheManager} from the given configuration resource.
	 * @param name the desired name of the cache manager
	 * @param configLocation the location of the configuration file (as a Spring resource)
	 * @return the new EhCache CacheManager
	 * @throws CacheException in case of configuration parsing failure
	 */
public static CacheManager buildCacheManager(String name, Resource configLocation) throws CacheException {
    Configuration configuration = parseConfiguration(configLocation);
    configuration.setName(name);
    return new CacheManager(configuration);
}
Also used : Configuration(net.sf.ehcache.config.Configuration) CacheManager(net.sf.ehcache.CacheManager)

Example 5 with CacheManager

use of net.sf.ehcache.CacheManager in project killbill by killbill.

the class EhCacheCacheManagerProvider method get.

@Override
public CacheManager get() {
    final CacheManager cacheManager;
    try {
        final InputStream inputStream = UriAccessor.accessUri(cacheConfig.getCacheConfigLocation());
        cacheManager = CacheManager.create(inputStream);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    } catch (final URISyntaxException e) {
        throw new RuntimeException(e);
    }
    for (final BaseCacheLoader cacheLoader : cacheLoaders) {
        cacheLoader.init();
        final Ehcache cache = cacheManager.getEhcache(cacheLoader.getCacheType().getCacheName());
        if (cache == null) {
            logger.warn("Cache for cacheName='{}' not configured - check your ehcache.xml", cacheLoader.getCacheType().getCacheName());
            continue;
        }
        // Make sure we start from a clean state - this is mainly useful for tests
        for (final CacheLoader existingCacheLoader : cache.getRegisteredCacheLoaders()) {
            cache.unregisterCacheLoader(existingCacheLoader);
        }
        cache.registerCacheLoader(cacheLoader);
        // Instrument the cache
        final Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, cache);
        try {
            cacheManager.replaceCacheWithDecoratedCache(cache, decoratedCache);
        } catch (final CacheException e) {
            logger.warn("Unable to instrument cache {}: {}", cache.getName(), e.getMessage());
        }
    }
    return cacheManager;
}
Also used : CacheException(net.sf.ehcache.CacheException) InputStream(java.io.InputStream) CacheManager(net.sf.ehcache.CacheManager) InstrumentedEhcache(com.codahale.metrics.ehcache.InstrumentedEhcache) Ehcache(net.sf.ehcache.Ehcache) CacheLoader(net.sf.ehcache.loader.CacheLoader) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Aggregations

CacheManager (net.sf.ehcache.CacheManager)102 Cache (net.sf.ehcache.Cache)55 ClassPathResource (org.springframework.core.io.ClassPathResource)21 Element (net.sf.ehcache.Element)20 Configuration (net.sf.ehcache.config.Configuration)18 Test (org.junit.Test)18 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)17 MarkupCache (org.apache.wicket.markup.MarkupCache)10 CacheException (net.sf.ehcache.CacheException)9 IOException (java.io.IOException)7 Ehcache (net.sf.ehcache.Ehcache)7 UpdatingSelfPopulatingCache (net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache)6 URL (java.net.URL)5 BlockingCache (net.sf.ehcache.constructs.blocking.BlockingCache)5 SelfPopulatingCache (net.sf.ehcache.constructs.blocking.SelfPopulatingCache)5 DiskStoreConfiguration (net.sf.ehcache.config.DiskStoreConfiguration)4 PersistenceConfiguration (net.sf.ehcache.config.PersistenceConfiguration)4 Around (org.aspectj.lang.annotation.Around)4 Before (org.junit.Before)4 File (java.io.File)3