Refine search
System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M"); Configuration conf = new Configuration(); conf.setMaxBytesLocalHeap("100M"); CacheManager cacheManager = CacheManager.create(conf); new Cache(new CacheConfiguration("test", 0).// memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).// eternal(false).// timeToIdleSeconds(86400).// diskExpiryThreadIntervalSeconds(0).// persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE))); cacheManager.addCache(testCache); testCache.put(new Element("1", blob)); System.out.println(testCache.get("1") == null); System.out.println(testCache.getSize()); System.out.println(testCache.getStatistics().getLocalHeapSizeInBytes()); System.out.println("runtime used memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024 + "M"); cacheManager.shutdown();
public Map<String, Object> getCacheConfigurationInformationAsJson(Cache cache) { CacheConfiguration config = cache.getCacheConfiguration(); LinkedHashMap<String, Object> json = new LinkedHashMap<>(); json.put("Name", config.getName()); json.put("Maximum Elements in Memory", config.getMaxEntriesLocalHeap()); json.put("Maximum Elements on Disk", config.getMaxBytesLocalDisk()); json.put("Memory Store Eviction Policy", config.getMemoryStoreEvictionPolicy().toString()); json.put("Clean or Flush", config.isClearOnFlush()); json.put("Eternal", config.isEternal()); json.put("Time To Idle Seconds", config.getTimeToIdleSeconds()); json.put("time To Live Seconds", config.getTimeToLiveSeconds()); if (config.getPersistenceConfiguration() != null) { json.put("Persistence Configuration Strategy", config.getPersistenceConfiguration().getStrategy()); json.put("Persistence Configuration Synchronous writes", config.getPersistenceConfiguration().getSynchronousWrites()); } else { json.put("Persistence Configuration Strategy", "NONE"); json.put("Persistence Configuration Synchronous writes", false); json.put("Disk Spool Buffer Size in MB", config.getDiskSpoolBufferSizeMB()); json.put("Disk Access Stripes", config.getDiskAccessStripes()); json.put("Disk Expiry Thread Interval Seconds", config.getDiskExpiryThreadIntervalSeconds()); json.put("Logging Enabled", config.getLogging()); json.put("Terracotta Configuration", config.getTerracottaConfiguration()); json.put("Cache Writer Configuration", config.getCacheWriterConfiguration()); json.put("Cache Loader Configurations", config.getCacheLoaderConfigurations()); json.put("Frozen", config.isFrozen()); json.put("Transactional Mode", config.getTransactionalMode()); json.put("Statistics Enabled", config.getStatistics());
@Configuration @EnableCaching public class CachingConfig implements CachingConfigurer { @Bean(destroyMethod="shutdown") public net.sf.ehcache.CacheManager ehCacheManager() { CacheConfiguration cacheConfiguration = new CacheConfiguration(); cacheConfiguration.setName("myCacheName"); cacheConfiguration.setMemoryStoreEvictionPolicy("LRU"); cacheConfiguration.setMaxEntriesLocalHeap(1000); net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration(); config.addCache(cacheConfiguration); return net.sf.ehcache.CacheManager.newInstance(config); } @Bean @Override public CacheManager cacheManager() { return new EhCacheCacheManager(ehCacheManager()); } @Bean @Override public KeyGenerator keyGenerator() { return new SimpleKeyGenerator(); } }
private static CacheConfiguration cacheConfiguration(String cacheName) { return new CacheConfiguration(cacheName, 1024) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) .overflowToDisk(false) .diskPersistent(false); }
public CacheConfigurationBuilder(final String name) { this.path = "ehcache.cache." + name; this.cache = new CacheConfiguration(); cache.setName(name); }
private static CacheConfiguration cacheConfiguration() { return new CacheConfiguration(CACHE_NAME, 2048) .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE)) .timeToIdleSeconds(120) .timeToLiveSeconds(0) .eternal(false) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU); }
private synchronized Cache getHeap() { if (heap == null) { if (CacheManager.getInstance().cacheExists("hydrated-cache")) { heap = CacheManager.getInstance().getCache("hydrated-cache"); } else { CacheConfiguration config = new CacheConfiguration("hydrated-cache", 0).eternal(true).overflowToDisk(false).maxElementsInMemory(100000); Cache cache = new Cache(config); CacheManager.create().addCache(cache); heap = cache; } } return heap; }
private synchronized Cache getHeap() { if (offHeap == null) { if (CacheManager.getInstance().cacheExists("hydrated-offheap-cache")) { offHeap = CacheManager.getInstance().getCache("hydrated-offheap-cache"); } else { CacheConfiguration config = new CacheConfiguration("hydrated-offheap-cache", 500).eternal(true).overflowToOffHeap(true).maxMemoryOffHeap("1400M"); Cache cache = new Cache(config); CacheManager.create().addCache(cache); offHeap = cache; } } return offHeap; }
/** * Returns the active configuration text for the input cacheName * * @param cacheName * @return Returns the active configuration text for the input cacheName * @throws CacheException if the cache with <code>cacheName</code> does not exist */ public String getActiveConfigurationText(String cacheName) throws CacheException { boolean decoratedCache = false; Ehcache cache = getCache(cacheName); if (cache == null) { cache = getEhcache(cacheName); decoratedCache = true; } CacheConfiguration actualConfig = cache != null ? cache.getCacheConfiguration() : null; if (actualConfig == null) { throw new CacheException("Cache with name '" + cacheName + "' does not exist"); } CacheConfiguration config = decoratedCache ? actualConfig.clone().name(cacheName) : actualConfig; return ConfigurationUtil.generateCacheConfigurationText(runtimeCfg.getConfiguration(), config); }
/** * {@inheritDoc} */ public void setRegionCacheMaxTTLSeconds(String region, int maxTTLSeconds) { Cache cache = this.cacheManager.getCache(region); if (cache != null) { cache.getCacheConfiguration().setTimeToLiveSeconds(maxTTLSeconds); sendNotification(CACHE_REGION_CHANGED, getRegionCacheAttributes(region), region); } }
/** * Gets the cache with the given name from Ehache. * <p> * If no cache with that name exists, one will be created and registered, using the configuration from the cache * with the given template name. */ private static net.sf.ehcache.Cache getCache(CacheManager embeddedCacheManager, String cacheName, String templateName) { net.sf.ehcache.Cache cache = embeddedCacheManager.getCache( cacheName ); if ( cache == null ) { CacheConfiguration configuration = embeddedCacheManager.getConfiguration().getCacheConfigurations().get( templateName ).clone(); configuration.setName( cacheName ); cache = new net.sf.ehcache.Cache( configuration ); embeddedCacheManager.addCache( cache ); } return cache; }
/** * {@inheritDoc} */ public void setRegionCacheMaxTTISeconds(String region, int maxTTISeconds) { Cache cache = this.cacheManager.getCache(region); if (cache != null) { cache.getCacheConfiguration().setTimeToIdleSeconds(maxTTISeconds); sendNotification(CACHE_REGION_CHANGED, getRegionCacheAttributes(region), region); } }
/** * {@inheritDoc} */ public int getRegionCacheTargetMaxInMemoryCount(String region) { Cache cache = cacheManager.getCache(region); if (cache != null) { return cache.getCacheConfiguration().getMaxElementsInMemory(); } else { return -1; } }
@Before public void setup() { cacheManager = new CacheManager(new Configuration().name("EhCacheCacheTests") .defaultCache(new CacheConfiguration("default", 100))); nativeCache = new net.sf.ehcache.Cache(new CacheConfiguration(CACHE_NAME, 100)); cacheManager.addCache(nativeCache); cache = new EhCacheCache(nativeCache); }
private void initSupportCache() { this.supportConfig = new CacheConfiguration(); supportConfig.name(underlyingCache.getName() + "_" + getClass().getName() + "_refreshAheadSupport"); supportConfig = supportConfig.persistence(new PersistenceConfiguration().strategy(Strategy.NONE)); int activeSize = 2 * refreshAheadConfig.getBatchSize() * refreshAheadConfig.getNumberOfThreads(); supportConfig = supportConfig.maxEntriesLocalHeap(activeSize); supportConfig = supportConfig.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU); supportConfig = supportConfig.timeToLiveSeconds(DEFAULT_SUPPORT_TTL_SECONDS); if (underlyingCache.getCacheConfiguration().isTerracottaClustered()) { supportConfig = supportConfig.persistence(new PersistenceConfiguration().strategy(Strategy.DISTRIBUTED)); supportConfig.addTerracotta(newTerracottaConfig); } else { supportConfig.setMaxElementsOnDisk(activeSize); } this.supportCache = new Cache(supportConfig); Ehcache prior = underlyingCache.getCacheManager().addCacheIfAbsent(supportCache); if (prior != supportCache) { throw new IllegalStateException("Unable to add refresh ahead support cache due to name collision: "
addAttribute(new SimpleNodeAttribute("name", configuration.getName()).optional(true)); addAttribute(new SimpleNodeAttribute("monitoring", configuration.getMonitoring()).optional(true).defaultValue( Configuration.DEFAULT_MONITORING.name().toLowerCase())); addAttribute(new SimpleNodeAttribute("dynamicConfig", configuration.getDynamicConfig()).optional(true).defaultValue( String.valueOf(Configuration.DEFAULT_DYNAMIC_CONFIG))); addAttribute(new SimpleNodeAttribute("defaultTransactionTimeoutInSeconds", configuration.getDefaultTransactionTimeoutInSeconds()) for (String cacheName : cacheManager.getCacheNames()) { boolean decoratedCache = false; Ehcache cache = cacheManager.getCache(cacheName); if (cache == null) { cache = cacheManager.getEhcache(cacheName); decoratedCache = true; CacheConfiguration config = decoratedCache ? cache.getCacheConfiguration().clone().name(cacheName) : cache.getCacheConfiguration(); addChildElement(new CacheConfigurationElement(this, configuration, config));
public static CachePool createEnCachePool() { CacheConfiguration cacheConf = new CacheConfiguration(); cacheConf.setName("testcache"); cacheConf.maxBytesLocalHeap(400, MemoryUnit.MEGABYTES) .timeToIdleSeconds(3600); Cache cache = new Cache(cacheConf); CacheManager.create().addCache(cache); EnchachePool enCachePool = new EnchachePool(cacheConf.getName(), cache, 400 * 10000); return enCachePool; }
EhCacheService(String name) { if (S.notBlank(name)) { cacheName = name; } Configuration configuration = ConfigurationFactory.parseConfiguration(); configuration.setClassLoader(CacheServiceProvider.Impl.classLoader()); cacheManager = CacheManager.create(configuration); Cache cache = cacheManager.getCache(cacheName); if (null == cache) { cache = (Cache)cacheManager.addCacheIfAbsent(cacheName); } this.cache = cache; long l = this.cache.getCacheConfiguration().getTimeToLiveSeconds(); if (0 != l) { defaultTTL = (int)l; } }
Class<? extends Ehcache> objectType2 = cacheFb.getObjectType(); assertSame(objectType, objectType2); CacheConfiguration config = cache.getCacheConfiguration(); assertEquals("myCache1", cache.getName()); if (useCacheManagerFb){ assertEquals("myCache1.maxElements", 300, config.getMaxEntriesLocalHeap()); assertEquals("myCache1.maxElements", 10000, config.getMaxEntriesLocalHeap()); cacheFb.afterPropertiesSet(); cache = (Cache) cacheFb.getObject(); config = cache.getCacheConfiguration(); assertEquals("undefinedCache", cache.getName()); assertTrue("default maxElements is correct", config.getMaxEntriesLocalHeap() == 10000); assertFalse("default eternal is correct", config.isEternal()); assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 120); assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 120); assertTrue("default diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 120); assertEquals("undefinedCache2", cache.getName()); assertTrue("overridden maxElements is correct", config.getMaxEntriesLocalHeap() == 5); assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 8); assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 7); assertTrue("overridden diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 10); CacheManager.getInstance().shutdown();
public void testCacheManagerFromConfigFile() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass())); cacheManagerFb.setCacheManagerName("myCacheManager"); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); assertTrue("Correct number of caches loaded", cm.getCacheNames().length == 1); Cache myCache1 = cm.getCache("myCache1"); assertFalse("myCache1 is not eternal", myCache1.getCacheConfiguration().isEternal()); assertTrue("myCache1.maxElements == 300", myCache1.getCacheConfiguration().getMaxEntriesLocalHeap() == 300); } finally { cacheManagerFb.destroy(); } }