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());
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(); } }
/** * Maximum number of entries that may be stored in local heap memory store. */ public long getMaxEntriesLocalHeap() { return cacheConfiguration.getMaxEntriesLocalHeap(); }
/** * Accessor * * @deprecated use {@link #getMaxEntriesLocalHeap()} */ @Deprecated public int getMaxElementsInMemory() { return (int)getMaxEntriesLocalHeap(); }
private void warnMaxEntriesForOverflowToOffHeap(final boolean register) { if (overflowToOffHeap != null && overflowToOffHeap && register) { if (getMaxEntriesLocalHeap() > 0 && getMaxEntriesLocalHeap() < MINIMUM_RECOMMENDED_IN_MEMORY) { LOG.warn("The " + getName() + " cache is configured for off-heap and has a maxEntriesLocalHeap/maxElementsInMemory of " + getMaxEntriesLocalHeap() + ". It is recommended to set maxEntriesLocalHeap/maxElementsInMemory to at least " + MINIMUM_RECOMMENDED_IN_MEMORY + " elements when using an off-heap store, otherwise performance " + "will be seriously degraded."); } } }
/** * {@inheritDoc} */ @Override public long getMaxEntriesLocalHeap() { return cache.getCacheConfiguration().getMaxEntriesLocalHeap(); }
assertEquals("myCache1", cache.getName()); if (useCacheManagerFb){ assertEquals("myCache1.maxElements", 300, config.getMaxEntriesLocalHeap()); assertEquals("myCache1.maxElements", 10000, config.getMaxEntriesLocalHeap()); 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("overridden maxElements is correct", config.getMaxEntriesLocalHeap() == 5); assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 8); assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 7);
private void warnMaxEntriesLocalHeap(final boolean register, CacheManager cacheManager) { if (getMaxEntriesLocalHeap() == 0 && register) { if (getMaxBytesLocalHeap() == 0 && (!cacheManager.getConfiguration().isMaxBytesLocalHeapSet())) { LOG.warn("Cache: " + getName() + " has a maxElementsInMemory of 0. This might lead to performance degradation or OutOfMemoryError at Terracotta client." + "From Ehcache 2.0 onwards this has been changed to mean a store" + " with no capacity limit. Set it to 1 if you want" + " no elements cached in memory"); } } }
/** * Returns a {@link String} representation of {@link Cache}. */ @Override public String toString() { StringBuilder dump = new StringBuilder(); dump.append("[") .append(" name = ").append(configuration.getName()) .append(" status = ").append(cacheStatus.getStatus()) .append(" eternal = ").append(configuration.isEternal()) .append(" overflowToDisk = ").append(configuration.isOverflowToDisk()) .append(" maxEntriesLocalHeap = ").append(configuration.getMaxEntriesLocalHeap()) .append(" maxEntriesLocalDisk = ").append(configuration.getMaxEntriesLocalDisk()) .append(" memoryStoreEvictionPolicy = ").append(configuration.getMemoryStoreEvictionPolicy()) .append(" timeToLiveSeconds = ").append(configuration.getTimeToLiveSeconds()) .append(" timeToIdleSeconds = ").append(configuration.getTimeToIdleSeconds()) .append(" persistence = ").append(configuration.getPersistenceConfiguration() == null ? "none" : configuration.getPersistenceConfiguration().getStrategy()) .append(" diskExpiryThreadIntervalSeconds = ").append(configuration.getDiskExpiryThreadIntervalSeconds()) .append(registeredEventListeners) .append(" maxBytesLocalHeap = ").append(configuration.getMaxBytesLocalHeap()) .append(" overflowToOffHeap = ").append(configuration.isOverflowToOffHeap()) .append(" maxBytesLocalOffHeap = ").append(configuration.getMaxBytesLocalOffHeap()) .append(" maxBytesLocalDisk = ").append(configuration.getMaxBytesLocalDisk()) .append(" pinned = ") .append(configuration.getPinningConfiguration() != null ? configuration.getPinningConfiguration().getStore().name() : "false") .append(" ]"); return dump.toString(); }
private static long getCachingTierMaxEntryCount(final Ehcache cache) { final PinningConfiguration pinningConfiguration = cache.getCacheConfiguration().getPinningConfiguration(); if (pinningConfiguration != null && pinningConfiguration.getStore() != PinningConfiguration.Store.INCACHE) { return 0; } return cache.getCacheConfiguration().getMaxEntriesLocalHeap(); }
/** * Setter for maxBytesLocalHeap in bytes * <p> * This property can be modified dynamically while the cache is operating. * * @param maxBytesHeap max bytes in heap in bytes */ public void setMaxBytesLocalHeap(final Long maxBytesHeap) { if (onHeapPoolUsage != null && getMaxEntriesLocalHeap() > 0) { throw new InvalidConfigurationException("MaxEntriesLocalHeap is not compatible with " + "MaxBytesLocalHeap set on cache"); } if (onHeapPoolUsage != null && onHeapPoolUsage != PoolUsage.Cache) { throw new IllegalStateException("A Cache can't switch memory pool!"); } checkDynamicChange(); verifyGreaterThanZero(maxBytesHeap, "maxBytesLocalHeap"); Long oldValue = this.maxBytesLocalHeap; this.maxBytesLocalHeap = maxBytesHeap; fireMaxBytesOnLocalHeapChanged(oldValue, maxBytesHeap); }
/** * Validates the CacheConfiguration against the CacheManager's Configuration * @param configuration The CacheManager Configuration * @return the list of errors encountered, or an empty list */ List<CacheConfigError> validateCachePools(final Configuration configuration) { List<CacheConfigError> errors = new ArrayList<CacheConfigError>(); if (configuration.isMaxBytesLocalHeapSet() && getMaxEntriesLocalHeap() > 0) { errors.add(new CacheConfigError("maxEntriesLocalHeap is not compatible with " + "maxBytesLocalHeap set on cache manager", getName())); } if (getMaxBytesLocalHeap() > 0 && getMaxEntriesLocalHeap() > 0) { errors.add(new CacheConfigError("maxEntriesLocalHeap is not compatible with " + "maxBytesLocalHeap set on cache", getName())); } if (isMaxBytesLocalHeapPercentageSet() && !configuration.isMaxBytesLocalHeapSet()) { errors.add(new CacheConfigError("Defines a percentage maxBytesOnHeap value but no CacheManager " + "wide value was configured", getName())); } if (isMaxBytesLocalOffHeapPercentageSet() && !configuration.isMaxBytesLocalOffHeapSet()) { errors.add(new CacheConfigError("Defines a percentage maxBytesOffHeap value but no CacheManager " + "wide value was configured", getName())); } if (isMaxBytesLocalDiskPercentageSet() && !configuration.isMaxBytesLocalDiskSet()) { errors.add(new CacheConfigError("Defines a percentage maxBytesOnDisk value but no CacheManager " + "wide value was configured", getName())); } return errors; }
private void warnTieredSizing() { if (isOverflowToOffHeap()) { if (getMaxBytesLocalHeap() >= getMaxBytesLocalOffHeap() && getMaxBytesLocalOffHeap() != 0) { LOG.warn("Configuration problem for cache " + getName() + ": MaxBytesLocalHeap equal or greater than MaxBytesLocalOffHeap. " + "This will result in useless off heap storage."); } if (isOverflowToDisk()) { if (getMaxBytesLocalOffHeap() >= getMaxBytesLocalDisk() && getMaxBytesLocalDisk() != 0) { LOG.warn("Configuration problem for cache " + getName() + ": MaxBytesLocalOffHeap equal or greater than MaxBytesLocalDisk. " + "This will result in useless disk storage."); } } } if (isOverflowToDisk()) { if (getMaxEntriesLocalHeap() >= getMaxEntriesLocalDisk() && getMaxEntriesLocalDisk() != 0) { LOG.warn("Configuration problem for cache " + getName() + ": MaxEntriesLocalHeap equal or greater than MaxEntriesLocalDisk. " + "This will result in useless disk storage."); } } }
status = Status.STATUS_UNINITIALISED; this.cache = cache; this.maximumSize = (int) cache.getCacheConfiguration().getMaxEntriesLocalHeap(); this.policy = determineEvictionPolicy(cache); if (pool instanceof UnboundedPool) {
/** * Maximum number of entries that may be stored in local heap memory store. */ public long getMaxEntriesLocalHeap() { return cacheConfiguration.getMaxEntriesLocalHeap(); }
private boolean skipUpdateAccessStatistics(Element element) { if (configuration.isFrozen()) { boolean forLifetime = element.isEternal(); boolean forHeap = configuration.getMaxEntriesLocalHeap() > 0 || configuration.getMaxBytesLocalHeap() > 0 || getCacheManager().getConfiguration().isMaxBytesLocalHeapSet(); boolean forDisk = configuration.isOverflowToDisk() && (configuration.getMaxEntriesLocalDisk() > 0 || configuration.getMaxBytesLocalDisk() > 0 || getCacheManager().getConfiguration().isMaxBytesLocalDiskSet()); return !(forLifetime || forHeap || forDisk); } else { return false; } }
long maxElementsInMem; final boolean overflowToOffHeap = cache.getCacheConfiguration().isOverflowToOffHeap(); maxElementsInMem = cache.getCacheConfiguration().getMaxEntriesLocalHeap() == 0 ? Integer.MAX_VALUE : cache.getCacheConfiguration().getMaxEntriesLocalHeap(); if (overflowToOffHeap) { maxBytesInMem = cache.getCacheConfiguration().getMaxBytesLocalOffHeap();
/** * Constructor for the LruMemoryStore object * The backing {@link java.util.LinkedHashMap} is created with LRU by access order. */ public LruMemoryStore(Ehcache cache, Store diskStore) { status = Status.STATUS_UNINITIALISED; this.maximumSize = cache.getCacheConfiguration().getMaxEntriesLocalHeap(); this.cachePinned = determineCachePinned(cache.getCacheConfiguration()); this.elementPinningEnabled = !cache.getCacheConfiguration().isOverflowToOffHeap(); this.cache = cache; this.diskStore = diskStore; if (cache.getCacheConfiguration().isOverflowToDisk()) { evictionObserver = null; } else { evictionObserver = StatisticBuilder.operation(EvictionOutcome.class).named("eviction").of(this).build(); } map = new SpoolingLinkedHashMap(); status = Status.STATUS_ALIVE; copyStrategyHandler = MemoryStore.getCopyStrategyHandler(cache); }
builder.pinnedInLocalMemory(isPinnedInLocalMemory(ehcacheConfig)); builder.evictionEnabled(!isPinnedInCache(ehcacheConfig)); builder.maxCountLocalHeap((int) ehcacheConfig.getMaxEntriesLocalHeap()); builder.maxBytesLocalHeap(ehcacheConfig.getMaxBytesLocalHeap()); builder.maxBytesLocalOffheap(ehcacheConfig.getMaxBytesLocalOffHeap());
CacheConfiguration.DEFAULT_ETERNAL_VALUE)); if (!(cacheConfiguration.getMaxBytesLocalHeap() > 0 || configuration.getMaxBytesLocalHeap() > 0)) { element.addAttribute(new SimpleNodeAttribute("maxEntriesLocalHeap", cacheConfiguration.getMaxEntriesLocalHeap()).optional(false));