Java Code Examples for net.sf.ehcache.config.Configuration#setName()

The following examples show how to use net.sf.ehcache.config.Configuration#setName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccessControlClientModule.java    From joynr with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
public CacheManager provideCacheManager() {
    Configuration configuration = new Configuration();
    configuration.setName("LDACEhCacheManager");
    configuration.setUpdateCheck(false);
    return CacheManager.create(configuration);
}
 
Example 2
Source File: GlobalDomainAccessControllerModule.java    From joynr with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
CacheManager provideCacheManager() {
    Configuration configuration = new Configuration();
    configuration.setName("GDACEhCacheManager");
    return CacheManager.create(configuration);
}
 
Example 3
Source File: EhCacheManagerFactoryBean.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws CacheException {
	if (logger.isInfoEnabled()) {
		logger.info("Initializing EhCache CacheManager" +
				(this.cacheManagerName != null ? " '" + this.cacheManagerName + "'" : ""));
	}

	Configuration configuration = (this.configLocation != null ?
			EhCacheManagerUtils.parseConfiguration(this.configLocation) : ConfigurationFactory.parseConfiguration());
	if (this.cacheManagerName != null) {
		configuration.setName(this.cacheManagerName);
	}

	if (this.shared) {
		// Old-school EhCache singleton sharing...
		// No way to find out whether we actually created a new CacheManager
		// or just received an existing singleton reference.
		this.cacheManager = CacheManager.create(configuration);
	}
	else if (this.acceptExisting) {
		// EhCache 2.5+: Reusing an existing CacheManager of the same name.
		// Basically the same code as in CacheManager.getInstance(String),
		// just storing whether we're dealing with an existing instance.
		synchronized (CacheManager.class) {
			this.cacheManager = CacheManager.getCacheManager(this.cacheManagerName);
			if (this.cacheManager == null) {
				this.cacheManager = new CacheManager(configuration);
			}
			else {
				this.locallyManaged = false;
			}
		}
	}
	else {
		// Throwing an exception if a CacheManager of the same name exists already...
		this.cacheManager = new CacheManager(configuration);
	}
}
 
Example 4
Source File: EhCacheManagerFactoryBean.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws IOException, CacheException {
	logger.info("Initializing EhCache CacheManager");
	InputStream is = this.extractEntandoConfig();
	try {
		// A bit convoluted for EhCache 1.x/2.0 compatibility.
		// To be much simpler once we require EhCache 2.1+
		if (this.cacheManagerName != null) {
			if (this.shared && createWithConfiguration == null) {
				// No CacheManager.create(Configuration) method available before EhCache 2.1;
				// can only set CacheManager name after creation.
				this.cacheManager = (is != null ? CacheManager.create(is) : CacheManager.create());
				this.cacheManager.setName(this.cacheManagerName);
			} else {
				Configuration configuration = (is != null ? ConfigurationFactory.parseConfiguration(is) :
						ConfigurationFactory.parseConfiguration());
				configuration.setName(this.cacheManagerName);
				if (this.shared) {
					this.cacheManager = (CacheManager) ReflectionUtils.invokeMethod(createWithConfiguration, null, configuration);
				} else {
					this.cacheManager = new CacheManager(configuration);
				}
			}
		} else if (this.shared) {
			// For strict backwards compatibility: use simplest possible constructors...
			this.cacheManager = (is != null ? CacheManager.create(is) : CacheManager.create());
		} else {
			this.cacheManager = (is != null ? new CacheManager(is) : new CacheManager());
		}
	} finally {
		if (is != null) {
			is.close();
		}
	}
}
 
Example 5
Source File: CacheTemplate.java    From smart-cache with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void afterPropertiesSet() throws Exception {
    Cache.ID = key + "." + Dates.newDateStringOfFormatDateTimeSSSNoneSpace();
    Cache.HOST = Utils.getLocalHostIP();
    Cache.CACHE_STORE = key + spliter + "cache" + spliter + "store";
    Cache.CACHE_STORE_SYNC = Cache.CACHE_STORE + spliter + "sync";
    if (this.localEnabled) {
        Configuration configuration = new Configuration();
        configuration.setName(Cache.ID);
        configuration.setMaxBytesLocalHeap(localMaxBytesLocalHeap);
        configuration.setMaxBytesLocalDisk(localMaxBytesLocalDisk);
        // DiskStore
        // 每次启动设置新的文件地址,以避免重启期间一级缓存未同步,以及单机多应用启动造成EhcacheManager重复的问题.
        DiskStoreConfiguration dsc = new DiskStoreConfiguration();
        dsc.setPath(localStoreLocation + Cache.ID);
        configuration.diskStore(dsc);
        // DefaultCache
        CacheConfiguration defaultCacheConfiguration = new CacheConfiguration();
        defaultCacheConfiguration.setEternal(false);
        defaultCacheConfiguration.setOverflowToDisk(true);
        defaultCacheConfiguration.setDiskPersistent(false);
        defaultCacheConfiguration.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU);
        defaultCacheConfiguration.setDiskExpiryThreadIntervalSeconds(localDiskExpiryThreadIntervalSeconds);
        // 默认false,使用引用.设置为true,避免外部代码修改了缓存对象.造成EhCache的缓存对象也随之改变
        // 但是设置为true后,将引起element的tti不自动刷新.如果直接新建element去覆盖原值.则本地ttl和远程ttl会产生一定的误差.
        // 因此,使用时放弃手动覆盖方式刷新本地tti,当本地tti过期后,自动从Redis中再获取即可.
        defaultCacheConfiguration.copyOnRead(true);
        defaultCacheConfiguration.copyOnWrite(true);
        defaultCacheConfiguration.setTimeToIdleSeconds(localTimeToIdleSeconds);
        defaultCacheConfiguration.setTimeToLiveSeconds(localTimeToLiveSeconds);
        configuration.setDefaultCacheConfiguration(defaultCacheConfiguration);
        configuration.setDynamicConfig(false);
        configuration.setUpdateCheck(false);
        this.cacheManager = new CacheManager(configuration);
        this.cacheSync = new RedisPubSubSync(this);// 使用Redis Topic发送订阅缓存变更消息
    }
}
 
Example 6
Source File: EhCache2MetricsCompatibilityTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
EhCache2MetricsCompatibilityTest() {
    Configuration config = ConfigurationFactory.parseConfiguration();
    config.setName(UUID.randomUUID().toString());

    this.cacheManager = CacheManager.newInstance(config);
    this.cacheManager.addCache("mycache");
    this.cache = cacheManager.getCache("mycache");
}
 
Example 7
Source File: EhCacheManagerFactoryBean.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws CacheException {
	if (logger.isInfoEnabled()) {
		logger.info("Initializing EhCache CacheManager" +
				(this.cacheManagerName != null ? " '" + this.cacheManagerName + "'" : ""));
	}

	Configuration configuration = (this.configLocation != null ?
			EhCacheManagerUtils.parseConfiguration(this.configLocation) : ConfigurationFactory.parseConfiguration());
	if (this.cacheManagerName != null) {
		configuration.setName(this.cacheManagerName);
	}

	if (this.shared) {
		// Old-school EhCache singleton sharing...
		// No way to find out whether we actually created a new CacheManager
		// or just received an existing singleton reference.
		this.cacheManager = CacheManager.create(configuration);
	}
	else if (this.acceptExisting) {
		// EhCache 2.5+: Reusing an existing CacheManager of the same name.
		// Basically the same code as in CacheManager.getInstance(String),
		// just storing whether we're dealing with an existing instance.
		synchronized (CacheManager.class) {
			this.cacheManager = CacheManager.getCacheManager(this.cacheManagerName);
			if (this.cacheManager == null) {
				this.cacheManager = new CacheManager(configuration);
			}
			else {
				this.locallyManaged = false;
			}
		}
	}
	else {
		// Throwing an exception if a CacheManager of the same name exists already...
		this.cacheManager = new CacheManager(configuration);
	}
}
 
Example 8
Source File: EhCacheManagerFactoryBean.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws CacheException {
	if (logger.isInfoEnabled()) {
		logger.info("Initializing EhCache CacheManager" +
				(this.cacheManagerName != null ? " '" + this.cacheManagerName + "'" : ""));
	}

	Configuration configuration = (this.configLocation != null ?
			EhCacheManagerUtils.parseConfiguration(this.configLocation) : ConfigurationFactory.parseConfiguration());
	if (this.cacheManagerName != null) {
		configuration.setName(this.cacheManagerName);
	}

	if (this.shared) {
		// Old-school EhCache singleton sharing...
		// No way to find out whether we actually created a new CacheManager
		// or just received an existing singleton reference.
		this.cacheManager = CacheManager.create(configuration);
	}
	else if (this.acceptExisting) {
		// EhCache 2.5+: Reusing an existing CacheManager of the same name.
		// Basically the same code as in CacheManager.getInstance(String),
		// just storing whether we're dealing with an existing instance.
		synchronized (CacheManager.class) {
			this.cacheManager = CacheManager.getCacheManager(this.cacheManagerName);
			if (this.cacheManager == null) {
				this.cacheManager = new CacheManager(configuration);
			}
			else {
				this.locallyManaged = false;
			}
		}
	}
	else {
		// Throwing an exception if a CacheManager of the same name exists already...
		this.cacheManager = new CacheManager(configuration);
	}
}
 
Example 9
Source File: CacheService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Gets the unique instance of the CacheService
 *
 * @return The unique instance of the CacheService
 */
public static synchronized CacheService getInstance( )
{
    if ( _singleton == null )
    {
        _singleton = new CacheService( );
        _singleton.init( );
        Configuration configuration = ConfigurationFactory.parseConfiguration( );
        configuration.setName( LUTECE_CACHEMANAGER_NAME );
        _manager = CacheManager.create( configuration );
    }

    return _singleton;
}
 
Example 10
Source File: EhCacheManagerFactoryBean.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws CacheException {
	logger.info("Initializing EhCache CacheManager");
	Configuration configuration = (this.configLocation != null ?
			EhCacheManagerUtils.parseConfiguration(this.configLocation) : ConfigurationFactory.parseConfiguration());
	if (this.cacheManagerName != null) {
		configuration.setName(this.cacheManagerName);
	}
	if (this.shared) {
		// Old-school EhCache singleton sharing...
		// No way to find out whether we actually created a new CacheManager
		// or just received an existing singleton reference.
		this.cacheManager = CacheManager.create(configuration);
	}
	else if (this.acceptExisting) {
		// EhCache 2.5+: Reusing an existing CacheManager of the same name.
		// Basically the same code as in CacheManager.getInstance(String),
		// just storing whether we're dealing with an existing instance.
		synchronized (CacheManager.class) {
			this.cacheManager = CacheManager.getCacheManager(this.cacheManagerName);
			if (this.cacheManager == null) {
				this.cacheManager = new CacheManager(configuration);
			}
			else {
				this.locallyManaged = false;
			}
		}
	}
	else {
		// Throwing an exception if a CacheManager of the same name exists already...
		this.cacheManager = new CacheManager(configuration);
	}
}
 
Example 11
Source File: SessionCache.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
@PostConstruct
@SuppressWarnings("unused")
private void init() throws Exception {

    // Init Cache
    Configuration c = new Configuration();
    c.setName("sessionManager");
    manager = CacheManager.create(c);
    CacheConfiguration config = new CacheConfiguration();
    config.eternal(false).name(CACHE_NAME).maxEntriesLocalHeap(maxEntries).memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).timeToIdleSeconds(timeToIdle).timeToLiveSeconds(timeToLive);
    if (!manager.cacheExists(CACHE_NAME)) {
        manager.addCache(new Cache(config));
    }
    sessions = manager.getCache(CACHE_NAME);

    // Init JMS replication
    ConnectionFactory factory = new ActiveMQConnectionFactory(this.url);
    Connection conn = factory.createConnection();
    conn.start();
    jmsSession = (TopicSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final Topic topic = jmsSession.createTopic(TOPIC_NAME);
    tp = jmsSession.createPublisher(topic);

    listener = new Thread() { // Thread created once upon container startup
        @Override
        public void run() {
            try {
                MessageConsumer consumer = jmsSession.createConsumer(topic);
                while (live) {

                    ObjectMessage msg = (ObjectMessage) consumer.receive();

                    LOG.debug("Received replication message: {}", msg);

                    if (PUT.equals(msg.getStringProperty(ACTION_KEY))) {
                        sessions.put(new Element(msg.getStringProperty(TOKEN_KEY), msg.getObject()));
                    } else if (REMOVE.equals(msg.getStringProperty(ACTION_KEY))) {
                        sessions.remove(msg.getStringProperty(TOKEN_KEY));
                    }

                }
            } catch (JMSException e) {
                LOG.error("Error reading replication message", e);
            }
        }
    };

    listener.start();
}
 
Example 12
Source File: EhCacheManagerUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Build an EhCache {@link CacheManager} from the default configuration.
 * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
 * (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
 * If no configuration file can be found, a fail-safe fallback configuration will be used.
 * @param name the desired name of the cache manager
 * @return the new EhCache CacheManager
 * @throws CacheException in case of configuration parsing failure
 */
public static CacheManager buildCacheManager(String name) throws CacheException {
	Configuration configuration = ConfigurationFactory.parseConfiguration();
	configuration.setName(name);
	return new CacheManager(configuration);
}
 
Example 13
Source File: EhCacheManagerUtils.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: EhCacheManagerUtils.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Build an EhCache {@link CacheManager} from the default configuration.
 * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
 * (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
 * If no configuration file can be found, a fail-safe fallback configuration will be used.
 * @param name the desired name of the cache manager
 * @return the new EhCache CacheManager
 * @throws CacheException in case of configuration parsing failure
 */
public static CacheManager buildCacheManager(String name) throws CacheException {
	Configuration configuration = ConfigurationFactory.parseConfiguration();
	configuration.setName(name);
	return new CacheManager(configuration);
}
 
Example 15
Source File: EhCacheManagerUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 16
Source File: EhCacheManagerUtils.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: EhCacheManagerUtils.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Build an EhCache {@link CacheManager} from the default configuration.
 * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
 * (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
 * If no configuration file can be found, a fail-safe fallback configuration will be used.
 * @param name the desired name of the cache manager
 * @return the new EhCache CacheManager
 * @throws CacheException in case of configuration parsing failure
 */
public static CacheManager buildCacheManager(String name) throws CacheException {
	Configuration configuration = ConfigurationFactory.parseConfiguration();
	configuration.setName(name);
	return new CacheManager(configuration);
}
 
Example 18
Source File: EhCacheManagerUtils.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: EhCacheManagerUtils.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Build an EhCache {@link CacheManager} from the default configuration.
 * <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
 * (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
 * If no configuration file can be found, a fail-safe fallback configuration will be used.
 * @param name the desired name of the cache manager
 * @return the new EhCache CacheManager
 * @throws CacheException in case of configuration parsing failure
 */
public static CacheManager buildCacheManager(String name) throws CacheException {
	Configuration configuration = ConfigurationFactory.parseConfiguration();
	configuration.setName(name);
	return new CacheManager(configuration);
}