Java Code Examples for net.sf.ehcache.config.CacheConfiguration#setTimeToIdleSeconds()

The following examples show how to use net.sf.ehcache.config.CacheConfiguration#setTimeToIdleSeconds() . 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: EnchachePooFactory.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CachePool createCachePool(String poolName, int cacheSize,
		int expiredSeconds) {
	CacheManager cacheManager = CacheManager.create();
	Cache enCache = cacheManager.getCache(poolName);
	if (enCache == null) {

		CacheConfiguration cacheConf = cacheManager.getConfiguration()
				.getDefaultCacheConfiguration().clone();
		cacheConf.setName(poolName);
		if (cacheConf.getMaxEntriesLocalHeap() != 0) {
			cacheConf.setMaxEntriesLocalHeap(cacheSize);
		} else {
			cacheConf.setMaxBytesLocalHeap(String.valueOf(cacheSize));
		}
		cacheConf.setTimeToIdleSeconds(expiredSeconds);
		Cache cache = new Cache(cacheConf);
		cacheManager.addCache(cache);
		return new EnchachePool(poolName,cache,cacheSize);
	} else {
		return new EnchachePool(poolName,enCache,cacheSize);
	}
}
 
Example 2
Source File: EnchachePooFactory.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public CachePool createCachePool(String poolName, int cacheSize,
                                 int expiredSeconds) {
    CacheManager cacheManager = CacheManager.create();
    Cache enCache = cacheManager.getCache(poolName);
    if (enCache == null) {

        CacheConfiguration cacheConf = cacheManager.getConfiguration().getDefaultCacheConfiguration().clone();
        cacheConf.setName(poolName);
        if (cacheConf.getMaxEntriesLocalHeap() != 0) {
            cacheConf.setMaxEntriesLocalHeap(cacheSize);
        } else {
            cacheConf.setMaxBytesLocalHeap(String.valueOf(cacheSize));
        }
        cacheConf.setTimeToIdleSeconds(expiredSeconds);
        Cache cache = new Cache(cacheConf);
        cacheManager.addCache(cache);
        return new EnchachePool(poolName, cache, cacheSize);
    } else {
        return new EnchachePool(poolName, enCache, cacheSize);
    }
}
 
Example 3
Source File: DaoSpringModuleConfig.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an EH Cache manager.
 *
 * @return the EH Cache manager.
 */
@Bean(destroyMethod = "shutdown")
public net.sf.ehcache.CacheManager ehCacheManager()
{
    CacheConfiguration cacheConfiguration = new CacheConfiguration();
    cacheConfiguration.setName(HERD_CACHE_NAME);
    cacheConfiguration.setTimeToLiveSeconds(configurationHelper.getProperty(ConfigurationValue.HERD_CACHE_TIME_TO_LIVE_SECONDS, Long.class));
    cacheConfiguration.setTimeToIdleSeconds(configurationHelper.getProperty(ConfigurationValue.HERD_CACHE_TIME_TO_IDLE_SECONDS, Long.class));
    cacheConfiguration.setMaxElementsInMemory(configurationHelper.getProperty(ConfigurationValue.HERD_CACHE_MAX_ELEMENTS_IN_MEMORY, Integer.class));
    cacheConfiguration.setMemoryStoreEvictionPolicy(configurationHelper.getProperty(ConfigurationValue.HERD_CACHE_MEMORY_STORE_EVICTION_POLICY));

    net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
    config.addCache(cacheConfiguration);

    return net.sf.ehcache.CacheManager.create(config);
}
 
Example 4
Source File: CacheService.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read cache config from the file caches.dat
 *
 * @param strCacheName
 *            The cache name
 * @return The config
 */
private CacheConfiguration getCacheConfiguration( String strCacheName )
{
    CacheConfiguration config = new CacheConfiguration( );
    config.setName( strCacheName );
    config.setMaxElementsInMemory( getIntProperty( strCacheName, PROPERTY_MAX_ELEMENTS, _nDefaultMaxElementsInMemory ) );
    config.setEternal( getBooleanProperty( strCacheName, PROPERTY_ETERNAL, _bDefaultEternal ) );
    config.setTimeToIdleSeconds( getLongProperty( strCacheName, PROPERTY_TIME_TO_IDLE, _lDefaultTimeToIdle ) );
    config.setTimeToLiveSeconds( getLongProperty( strCacheName, PROPERTY_TIME_TO_LIVE, _lDefaultTimeToLive ) );
    config.setOverflowToDisk( getBooleanProperty( strCacheName, PROPERTY_OVERFLOW_TO_DISK, _bDefaultOverflowToDisk ) );
    config.setDiskPersistent( getBooleanProperty( strCacheName, PROPERTY_DISK_PERSISTENT, _bDefaultDiskPersistent ) );
    config.setDiskExpiryThreadIntervalSeconds( getLongProperty( strCacheName, PROPERTY_DISK_EXPIRY, _lDefaultDiskExpiry ) );
    config.setMaxElementsOnDisk( getIntProperty( strCacheName, PROPERTY_MAX_ELEMENTS_DISK, _nDefaultMaxElementsOnDisk ) );
    config.setStatistics( getBooleanProperty( strCacheName, PROPERTY_STATISTICS, _bDefaultStatistics ) );

    return config;
}
 
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: EhCacheRegionFactory.java    From webdsl with Apache License 2.0 4 votes vote down vote up
protected void overrideCacheConfiguration(Properties properties, CacheConfiguration cacheConfig, String cacheName) {
	String cachePrefix = NET_SF_EHCACHE_REGION_PREFIX + cacheName + ".";

	if(properties.get(cachePrefix + "maxElementsInMemory") != null) {
		int maxElementsInMemory = Integer.parseInt((String)properties.get(cachePrefix + "maxElementsInMemory"));
		cacheConfig.setMaxElementsInMemory(maxElementsInMemory);
	}

	if(properties.get(cachePrefix + "eternal") != null) {
		boolean eternal = Boolean.parseBoolean((String)properties.get(cachePrefix + "eternal"));
		cacheConfig.setEternal(eternal);
	}

	if(properties.get(cachePrefix + "timeToIdleSeconds") != null) {
		long timeToIdleSeconds = Long.parseLong((String)properties.get(cachePrefix + "timeToIdleSeconds"));
		cacheConfig.setTimeToIdleSeconds(timeToIdleSeconds);
	}

	if(properties.get(cachePrefix + "timeToLiveSeconds") != null) {
		long timeToLiveSeconds = Long.parseLong((String)properties.get(cachePrefix + "timeToLiveSeconds"));
		cacheConfig.setTimeToLiveSeconds(timeToLiveSeconds);
	}

	if(properties.get(cachePrefix + "overflowToDisk") != null) {
		boolean overflowToDisk = Boolean.parseBoolean((String)properties.get(cachePrefix + "overflowToDisk"));
		cacheConfig.setOverflowToDisk(overflowToDisk);
	}

	if(properties.get(cachePrefix + "maxElementsOnDisk") != null){
		int maxElementsOnDisk = Integer.parseInt((String)properties.get(cachePrefix + "maxElementsOnDisk"));
		cacheConfig.setMaxElementsOnDisk(maxElementsOnDisk);
	}

	if(properties.get(cachePrefix + "diskPersistent") != null){
		boolean diskPersistent = Boolean.parseBoolean((String)properties.get(cachePrefix + "diskPersistent"));
		cacheConfig.setDiskPersistent(diskPersistent);
	}

	if(properties.get(cachePrefix + "diskExpiryThreadIntervalSeconds") != null) {
		long diskExpiryThreadIntervalSeconds = Long.parseLong((String)properties.get(cachePrefix + "diskExpiryThreadIntervalSeconds"));
		cacheConfig.setDiskExpiryThreadIntervalSeconds(diskExpiryThreadIntervalSeconds);
	}

	if(properties.get(cachePrefix + "diskExpiryThreadIntervalSeconds") != null) {
		String memoryStoreEvictionPolicy = (String) properties.get(cachePrefix + "memoryStoreEvictionPolicy");
		cacheConfig.setMemoryStoreEvictionPolicy(memoryStoreEvictionPolicy);
	}
}