private String buildConfiguration(Object cache) { final StringBuilder sb = new StringBuilder(); // getCacheConfiguration() et getMaxElementsOnDisk() n'existent pas en ehcache 1.2 final CacheConfiguration config = ((Ehcache) cache).getCacheConfiguration(); sb.append("ehcache [maxElementsInMemory = ").append(config.getMaxElementsInMemory()); final boolean overflowToDisk = config.isOverflowToDisk(); sb.append(", overflowToDisk = ").append(overflowToDisk); if (overflowToDisk) { sb.append(", maxElementsOnDisk = ").append(config.getMaxElementsOnDisk()); } final boolean eternal = config.isEternal(); sb.append(", eternal = ").append(eternal); if (!eternal) { sb.append(", timeToLiveSeconds = ").append(config.getTimeToLiveSeconds()); sb.append(", timeToIdleSeconds = ").append(config.getTimeToIdleSeconds()); sb.append(", memoryStoreEvictionPolicy = ") .append(config.getMemoryStoreEvictionPolicy()); } sb.append(", diskPersistent = ").append(config.isDiskPersistent()); sb.append(']'); return sb.toString(); }
/** * Whether this cache uses a disk store * * @return true if the cache either overflows to disk or uses a local-classic persistence strategy. */ protected boolean isDiskStore() { return configuration.isOverflowToDisk(); }
/** * Accessor */ public boolean isOverflowToDisk() { return cacheConfiguration.isOverflowToDisk(); }
/** * {@inheritDoc} */ @Override public boolean isOverflowToDisk() { return cache.getCacheConfiguration().isOverflowToDisk(); }
private static boolean determineCachePinned(CacheConfiguration cacheConfiguration) { PinningConfiguration pinningConfiguration = cacheConfiguration.getPinningConfiguration(); if (pinningConfiguration == null) { return false; } switch (pinningConfiguration.getStore()) { case LOCALMEMORY: return false; case INCACHE: return cacheConfiguration.isOverflowToDisk(); default: throw new IllegalArgumentException(); } }
private boolean determineStorePinned(CacheConfiguration cacheConfiguration) { PinningConfiguration pinningConfiguration = cacheConfiguration.getPinningConfiguration(); if (pinningConfiguration == null) { return false; } switch (pinningConfiguration.getStore()) { case LOCALMEMORY: return false; case INCACHE: return !cacheConfiguration.isOverflowToOffHeap() && !cacheConfiguration.isOverflowToDisk(); default: throw new IllegalArgumentException(); } }
private boolean determineCachePinned(CacheConfiguration cacheConfiguration) { PinningConfiguration pinningConfiguration = cacheConfiguration.getPinningConfiguration(); if (pinningConfiguration == null) { return false; } switch (pinningConfiguration.getStore()) { case LOCALMEMORY: return false; case INCACHE: return !cacheConfiguration.isOverflowToOffHeap() && !cacheConfiguration.isOverflowToDisk(); default: throw new IllegalArgumentException(); } }
/** * 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 void validateConfig(Ehcache ehcache) { CacheConfiguration cacheConfiguration = ehcache.getCacheConfiguration(); final TerracottaConfiguration terracottaConfiguration = cacheConfiguration.getTerracottaConfiguration(); List<ConfigError> errors = new ArrayList<ConfigError>(); if (terracottaConfiguration == null || !terracottaConfiguration.isClustered()) { throw new InvalidConfigurationException( "Cannot create clustered store for non-terracotta clustered caches"); } MemoryStoreEvictionPolicy policy = cacheConfiguration.getMemoryStoreEvictionPolicy(); if (policy == MemoryStoreEvictionPolicy.FIFO || policy == MemoryStoreEvictionPolicy.LFU) { errors.add(new ConfigError("Policy '" + policy + "' is not a supported memory store eviction policy.")); } if (cacheConfiguration.isOverflowToDisk()) { if (LOG.isWarnEnabled()) { LOG.warn("Persistence on disk on the local node is not supported with a Terracotta clustered ehcache store. Configure the Terracotta server array to be persistent instead."); } } boolean cachePinned = cacheConfiguration.getPinningConfiguration() != null && cacheConfiguration.getPinningConfiguration().getStore() == PinningConfiguration.Store.INCACHE; if (cachePinned && cacheConfiguration.getMaxEntriesInCache() != CacheConfiguration.DEFAULT_MAX_ENTRIES_IN_CACHE) { errors.add(new ConfigError("Cache pinning is not supported with maxEntriesInCache")); } if (errors.size() > 0) { throw new InvalidConfigurationException(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."); } } }
/** * Evict the <code>Element</code>. * <p> * Evict means that the <code>Element</code> is: * <ul> * <li>if, the store is diskPersistent, the <code>Element</code> is spooled to the DiskStore * <li>if not, the <code>Element</code> is removed. * </ul> * * @param element the <code>Element</code> to be evicted. */ protected final void evict(Element element) throws CacheException { if (cache.getCacheConfiguration().isOverflowToDisk()) { if (!element.isSerializable()) { if (LOG.isWarnEnabled()) { LOG.warn(new StringBuilder("Object with key ").append(element.getObjectKey()) .append(" is not Serializable and cannot be overflowed to disk").toString()); } cache.getCacheEventNotificationService().notifyElementEvicted(copyStrategyHandler.copyElementForReadIfNeeded(element), false); } else { spoolToDisk(element); } } else { evictionObserver.begin(); evictionObserver.end(EvictionOutcome.SUCCESS); cache.getCacheEventNotificationService().notifyElementEvicted(copyStrategyHandler.copyElementForReadIfNeeded(element), false); } }
/** * Calculates the number of caches in the configuration that are diskPersistent */ public final int numberOfCachesThatUseDiskStorage() { int count = 0; Set cacheConfigurations = configuration.getCacheConfigurations().entrySet(); for (CacheConfiguration cacheConfig : configuration.getCacheConfigurations().values()) { if (cacheConfig.isOverflowToDisk() || cacheConfig.isDiskPersistent() || (cacheConfig.isOverflowToOffHeap() && cacheConfig.isSearchable())) { count++; } else { PersistenceConfiguration persistence = cacheConfig.getPersistenceConfiguration(); if (persistence != null) { switch (persistence.getStrategy()) { case LOCALTEMPSWAP: case LOCALRESTARTABLE: count++; break; default: break; } } } } return count; }
/** * Create a DiskBackedMemoryStore instance * @param cache the cache * @param onHeapPool the pool tracking on-heap usage * @param onDiskPool the pool tracking on-disk usage * @return a DiskBackedMemoryStore instance */ public static Store createCacheStore(Ehcache cache, Pool onHeapPool, Pool onDiskPool) { final DiskStore result; CacheConfiguration config = cache.getCacheConfiguration(); if (config.isOverflowToDisk()) { result = create(cache, onHeapPool, onDiskPool); } else { throw new CacheException("DiskBackedMemoryStore can only be used for cache overflowing to disk"); } DiskStore diskStore = result; final OnHeapCachingTier<Object, Element> onHeapCache = OnHeapCachingTier.createOnHeapCache(cache, onHeapPool); return new CacheStore( onHeapCache, diskStore, cache.getCacheConfiguration() ); }
/** * Accessor */ public boolean isOverflowToDisk() { return cacheConfiguration.isOverflowToDisk(); }
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; } }
/** * Adds an {@link Ehcache} to the CacheManager. * <p> * Memory and Disk stores will be configured for it and it will be added to the map of caches. Also notifies the * CacheManagerEventListener after the cache was initialised and added. * * @param cache * @throws IllegalStateException * if the cache is not {@link Status#STATUS_UNINITIALISED} before this method is called. * @throws ObjectExistsException * if the cache already exists in the CacheManager * @throws CacheException * if there was an error adding the cache to the CacheManager */ public synchronized void addCache(Ehcache cache) throws IllegalStateException, ObjectExistsException, CacheException { checkStatus(); if (cache == null) { return; } final CacheConfiguration cacheConfiguration = cache.getCacheConfiguration(); final boolean verifyOffHeapUsage = runtimeCfg.hasOffHeapPool() && ((!cacheConfiguration.isOverflowToDisk() && !cacheConfiguration.isOverflowToOffHeapSet()) || cacheConfiguration .isOverflowToOffHeap()); if (verifyOffHeapUsage && (cacheConfiguration.isMaxBytesLocalOffHeapPercentageSet() || cacheConfiguration.getMaxBytesLocalOffHeap() > 0)) { throw new CacheException("CacheManager uses OffHeap settings, you can't add cache using offHeap dynamically!"); } addCacheNoCheck(cache, true); }
/** * 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); }
/** * Adds all common child elements with the "defaultCache" element in ehcache.xsd * * @param element * @param cacheConfiguration */ public static void addCommonChildElementsWithDefaultCache(NodeElement element, CacheConfiguration cacheConfiguration) { for (FactoryConfigurationElement child : getAllFactoryElements(element, "cacheEventListenerFactory", cacheConfiguration .getCacheEventListenerConfigurations())) { CacheEventListenerFactoryConfiguration factoryConfiguration = (CacheEventListenerFactoryConfiguration) child .getFactoryConfiguration(); child.addAttribute(new SimpleNodeAttribute("listenFor", factoryConfiguration.getListenFor())); element.addChildElement(child); } addAllFactoryConfigsAsChildElements(element, "cacheExtensionFactory", cacheConfiguration.getCacheExtensionConfigurations()); addAllFactoryConfigsAsChildElements(element, "cacheLoaderFactory", cacheConfiguration.getCacheLoaderConfigurations()); addBootstrapCacheLoaderFactoryConfigurationElement(element, cacheConfiguration); addCacheExceptionHandlerFactoryConfigurationElement(element, cacheConfiguration); addSizeOfPolicyConfigurationElement(element, cacheConfiguration); if (!cacheConfiguration.isOverflowToDisk() || !cacheConfiguration.isDiskPersistent()) { addPersistenceConfigurationElement(element, cacheConfiguration); } addCopyStrategyConfigurationElement(element, cacheConfiguration); addElementValueComparatorConfigurationElement(element, cacheConfiguration); addCacheWriterConfigurationElement(element, cacheConfiguration); addAllFactoryConfigsAsChildElements(element, "cacheDecoratorFactory", cacheConfiguration.getCacheDecoratorConfigurations()); addTerracottaConfigurationElement(element, cacheConfiguration); addPinningElement(element, cacheConfiguration); addSearchElement(element, cacheConfiguration); }
store = new LegacyStoreWrapper(new LruMemoryStore(this, disk), disk, registeredEventListeners, configuration); } else { if (configuration.isOverflowToDisk()) { store = DiskStore.createCacheStore(this, onHeapPool, onDiskPool); } else {
.toString().toUpperCase()).optional(true).defaultValue( CacheConfiguration.DEFAULT_MEMORY_STORE_EVICTION_POLICY.toString().toUpperCase())); if (cacheConfiguration.isOverflowToDisk() && cacheConfiguration.isDiskPersistent()) { element.addAttribute(new SimpleNodeAttribute("diskPersistent", "true")); element.addAttribute(new SimpleNodeAttribute("overflowToDisk", "true"));