@Override public CacheEventListener createCacheEventListener(Properties props) { try { if (props == null || props.isEmpty()) { manager = EhcacheHydratedCacheManagerImpl.getInstance(); } else { String managerClass = props.getProperty("managerClass"); Class<?> clazz = Class.forName(managerClass); Method method = clazz.getDeclaredMethod("getInstance"); manager = (HydratedCacheManager) method.invoke(null); } } catch (Exception e) { throw new RuntimeException("Unable to create a CacheEventListener instance", e); } return (CacheEventListener) manager; }
/** * Create a raw Cache object based on the configuration of this FactoryBean. */ protected Cache createCache() { // Only call EHCache 1.6 constructor if actually necessary (for compatibility with EHCache 1.3+) Cache cache = (!this.clearOnFlush) ? new Cache(this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy, this.overflowToDisk, null, this.eternal, this.timeToLive, this.timeToIdle, this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null, this.bootstrapCacheLoader, this.maxElementsOnDisk, this.diskSpoolBufferSize, this.clearOnFlush) : new Cache(this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy, this.overflowToDisk, null, this.eternal, this.timeToLive, this.timeToIdle, this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null, this.bootstrapCacheLoader, this.maxElementsOnDisk, this.diskSpoolBufferSize); if (this.cacheEventListeners != null) { for (CacheEventListener listener : this.cacheEventListeners) { cache.getCacheEventNotificationService().registerListener(listener); } } if (this.disabled) { cache.setDisabled(true); } net.sf.ehcache.config.CacheConfiguration config = cache.getCacheConfiguration(); config.setMaxEntriesLocalHeap(maxElementsInMemory); return cache; }
public BigMemoryGoStore() { Configuration managerConfiguration = new Configuration() .name("benchmark") .cache(new CacheConfiguration() .name("store") .maxBytesLocalHeap(50, MemoryUnit.MEGABYTES) .maxBytesLocalOffHeap(500, MemoryUnit.MEGABYTES) .eternal(true) ); cacheManager = CacheManager.create(managerConfiguration); cache = cacheManager.getCache("store"); // get notified when cache is not big enough CacheEventListener evictionListener = new CacheEventListenerAdapter() { @Override public void notifyElementEvicted(Ehcache ehcache, Element element) { cacheFull = true; } }; cache.getCacheEventNotificationService().registerListener(evictionListener); }
/** * Extend the already initialized cache manager with additional configuration. * * @param cacheManager * the cache manager to modify * @param config * the configuration to parse for settings that can be added at runtime */ @SuppressWarnings("unchecked") private static void extendEhCacheWithCustomConfig(CacheManager cacheManager, Configuration config) { ConfigurationHelper helper = new ConfigurationHelper(cacheManager, config); Set<net.sf.ehcache.Cache> caches = helper.createCaches(); for (net.sf.ehcache.Cache cache : caches) { if (cacheManager.cacheExists(cache.getName())) { if (LOG.isDebugEnabled()) { LOG.debug("Skipping cache with name " + cache.getName() + " because it already exists"); } Set<CacheEventListener> listeners = cache.getCacheEventNotificationService() .getCacheEventListeners(); net.sf.ehcache.Cache existCache = cacheManager.getCache(cache.getName()); for (CacheEventListener listener : listeners) { LOG.debug("copy cache event listener for cache " + cache.getName()); existCache.getCacheEventNotificationService().registerListener(listener); } } else { if (LOG.isDebugEnabled()) { LOG.debug("Adding cache with name " + cache.getName()); } cacheManager.addCache(cache); } } attachPeerProviderToPlaceHolder(cacheManager, helper); attachPeerListenerToPlaceHolder(cacheManager, helper); }
@Override public void registerCacheEventListener(org.sakaiproject.memory.api.CacheEventListener cacheEventListener) { super.registerCacheEventListener(cacheEventListener); if (cacheEventListener == null) { cache.getCacheEventNotificationService().unregisterListener(this); } else { cache.getCacheEventNotificationService().registerListener(this); } }
public static Ehcache createInMemoryCache(String cacheName, CacheEntryFactory entryFactory, int maxElements) { CacheManager manager = CacheManager.getInstance(); Ehcache cache = getCache(cacheName); if (cache == null) { // Create the cache cache = new Cache(cacheName, maxElements, memoryStoreEvictionPolicy, overflowToDisk, diskStorePath, eternal, timeToLive, timeToIdle, diskPersistent, diskExpiryThreadIntervalSeconds, null); // Associate the cacheEntryFactory with the cache SelfPopulatingCache selfPopulatingCache = new SelfPopulatingCache(cache, entryFactory); // Add any additional listener properties if (manager.getCachePeerListener("RMI") != null) { LOG.info("Setting RMI properties"); Properties properties = new Properties(); properties.put("replicateAsynchronously", "true"); properties.put("replicatePuts", "false"); properties.put("replicateUpdates", "true"); properties.put("replicateRemovals", "true"); properties.put("replicateUpdatesViaCopy", "false"); properties.put("asynchronousReplicationIntervalMillis", "1000"); RMICacheReplicatorFactory factory = new RMICacheReplicatorFactory(); CacheEventListener listener = factory.createCacheEventListener(properties); selfPopulatingCache.getCacheEventNotificationService().registerListener(listener); RMIBootstrapCacheLoaderFactory bootstrapFactory = new RMIBootstrapCacheLoaderFactory(); BootstrapCacheLoader bootstrapCacheLoader = bootstrapFactory.createBootstrapCacheLoader(new Properties()); selfPopulatingCache.setBootstrapCacheLoader(bootstrapCacheLoader); LOG.debug("RMI enabled"); } // Make the cache available manager.addCache(selfPopulatingCache); LOG.info("cache created: " + cache.getName()); } return cache; }
public CacheEventListener getCacheEventListener() { return cacheEventListener; }
public void setCacheEventListener(CacheEventListener cacheEventListener) { this.cacheEventListener = cacheEventListener; }
@Override public CacheEventListener createCacheEventListener(Properties properties) { return new CustomerCacheEventListener(); }
@Override public void afterPropertiesSet() throws CacheException, IOException { // If no cache name given, use bean name as cache name. String cacheName = getName(); if (cacheName == null) { cacheName = this.beanName; setName(cacheName); } // If no CacheManager given, fetch the default. if (this.cacheManager == null) { if (logger.isDebugEnabled()) { logger.debug("Using default EhCache CacheManager for cache region '" + cacheName + "'"); } this.cacheManager = CacheManager.getInstance(); } synchronized (this.cacheManager) { // Fetch cache region: If none with the given name exists, create one on the fly. Ehcache rawCache; boolean cacheExists = this.cacheManager.cacheExists(cacheName); if (cacheExists) { if (logger.isDebugEnabled()) { logger.debug("Using existing EhCache cache region '" + cacheName + "'"); } rawCache = this.cacheManager.getEhcache(cacheName); } else { if (logger.isDebugEnabled()) { logger.debug("Creating new EhCache cache region '" + cacheName + "'"); } rawCache = createCache(); rawCache.setBootstrapCacheLoader(this.bootstrapCacheLoader); } if (this.cacheEventListeners != null) { for (CacheEventListener listener : this.cacheEventListeners) { rawCache.getCacheEventNotificationService().registerListener(listener); } } // Needs to happen after listener registration but before setStatisticsEnabled if (!cacheExists) { this.cacheManager.addCache(rawCache); } // Only necessary on EhCache <2.7: As of 2.7, statistics are on by default. if (setStatisticsAvailable) { if (this.statisticsEnabled) { rawCache.setStatisticsEnabled(true); } if (this.sampledStatisticsEnabled) { rawCache.setSampledStatisticsEnabled(true); } } if (this.disabled) { rawCache.setDisabled(true); } Ehcache decoratedCache = decorateCache(rawCache); if (decoratedCache != rawCache) { this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache); } this.cache = decoratedCache; } }
@Override public void afterPropertiesSet() throws CacheException { // If no cache name given, use bean name as cache name. String cacheName = getName(); if (cacheName == null) { cacheName = this.beanName; setName(cacheName); } // If no CacheManager given, fetch the default. if (this.cacheManager == null) { if (logger.isDebugEnabled()) { logger.debug("Using default EhCache CacheManager for cache region '" + cacheName + "'"); } this.cacheManager = CacheManager.getInstance(); } synchronized (this.cacheManager) { // Fetch cache region: If none with the given name exists, create one on the fly. Ehcache rawCache; boolean cacheExists = this.cacheManager.cacheExists(cacheName); if (cacheExists) { if (logger.isDebugEnabled()) { logger.debug("Using existing EhCache cache region '" + cacheName + "'"); } rawCache = this.cacheManager.getEhcache(cacheName); } else { if (logger.isDebugEnabled()) { logger.debug("Creating new EhCache cache region '" + cacheName + "'"); } rawCache = createCache(); rawCache.setBootstrapCacheLoader(this.bootstrapCacheLoader); } if (this.cacheEventListeners != null) { for (CacheEventListener listener : this.cacheEventListeners) { rawCache.getCacheEventNotificationService().registerListener(listener); } } // Needs to happen after listener registration but before setStatisticsEnabled if (!cacheExists) { this.cacheManager.addCache(rawCache); } // Only necessary on EhCache <2.7: As of 2.7, statistics are on by default. if (this.statisticsEnabled && setStatisticsEnabledMethod != null) { ReflectionUtils.invokeMethod(setStatisticsEnabledMethod, rawCache, true); } if (this.sampledStatisticsEnabled && setSampledStatisticsEnabledMethod != null) { ReflectionUtils.invokeMethod(setSampledStatisticsEnabledMethod, rawCache, true); } if (this.disabled) { rawCache.setDisabled(true); } Ehcache decoratedCache = decorateCache(rawCache); if (decoratedCache != rawCache) { this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache); } this.cache = decoratedCache; } }
public CacheEventListenerRegistry(List<CacheEventListener> eventListeners) { this.eventListeners = eventListeners; }
public void addCacheEventListener(CacheEventListener listener) { getEventListeners().add(listener); }
public synchronized List<CacheEventListener> getEventListeners() { if (eventListeners == null) { eventListeners = new ArrayList<CacheEventListener>(); } return eventListeners; }
/** * Returns {@link Cache} instance or create new one if not exists. * * @return {@link Cache} */ public Ehcache initializeCache() { CacheManager cacheManager = getCacheManagerFactory().getInstance(); Ehcache cache; if (cacheManager.cacheExists(config.getCacheName())) { if (LOG.isTraceEnabled()) { LOG.trace("Found an existing cache: {}", config.getCacheName()); LOG.trace("Cache {} currently contains {} elements", config.getCacheName(), cacheManager.getEhcache(config.getCacheName()).getSize()); } cache = cacheManager.getEhcache(config.getCacheName()); } else { cache = new Cache(config.getCacheName(), config.getMaxElementsInMemory(), config.getMemoryStoreEvictionPolicy(), config.isOverflowToDisk(), config.getDiskStorePath(), config.isEternal(), config.getTimeToLiveSeconds(), config.getTimeToIdleSeconds(), config.isDiskPersistent(), config.getDiskExpiryThreadIntervalSeconds(), null); for (CacheEventListener listener : config.getEventListenerRegistry().getEventListeners()) { cache.getCacheEventNotificationService().registerListener(listener); } for (CacheLoaderWrapper loader : config.getCacheLoaderRegistry().getCacheLoaders()) { loader.init(cache); cache.registerCacheLoader(loader); } cacheManager.addCache(cache); if (LOG.isDebugEnabled()) { LOG.debug("Added a new cache: " + cache.getName()); } } return cache; }
@Test public void testConfig() throws Exception { producerTemplate.send(new Processor() { public void process(Exchange exchange) throws Exception { exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8"); Message in = exchange.getIn(); in.setHeader(CacheConstants.CACHE_OPERATION, CacheConstants.CACHE_OPERATION_ADD); in.setHeader(CacheConstants.CACHE_KEY, "greeting"); in.setBody("Hello World"); } }); CacheManager cm = cacheEndpoint.getCacheManagerFactory().getInstance(); Cache cache = cm.getCache(cacheEndpoint.getConfig().getCacheName()); Set<CacheEventListener> ehcacheEventListners = cache.getCacheEventNotificationService().getCacheEventListeners(); List<CacheLoader> cacheLoaders = cache.getRegisteredCacheLoaders(); CacheEventListenerRegistry configuredEventRegistry = cacheEndpoint.getConfig().getEventListenerRegistry(); CacheLoaderRegistry configuredLoaderRegistry = cacheEndpoint.getConfig().getCacheLoaderRegistry(); //Test if CacheEventListenerRegistry was referenced correctly assertEquals("CacheEventListenerRegistry size", 1, configuredEventRegistry.size()); //Expecting 1 loader to be configured via spring assertEquals("configuredLoaderRegistry size", 1, configuredLoaderRegistry.size()); //Expecting 1 listener added by us: TestCacheEventListener assertEquals("Number of registered listeners", 1, ehcacheEventListners.size()); assertEquals("Number of registered loaders", 1, cacheLoaders.size()); //Is our TestCacheEventListener really invoked? int puts = 0; for (Object listener : ehcacheEventListners) { if (listener instanceof TestCacheEventListener) { puts = ((TestCacheEventListener)listener).getNumberOfPuts(); break; } } assertEquals("TestCacheEventListener put invocations", 1, puts); //Is cache loader initialized by ehcache assertEquals("loader initialized", cacheLoaders.get(0).getStatus(), Status.STATUS_ALIVE); }
@Override public CacheEventListener createCacheEventListener(final Properties properties) { return new HazelcastCacheReplicator(); }
@Nullable public CacheEventListener[] getListeners() { return this.listeners; }
public void setListeners(@Nullable CacheEventListener ... listeners) { this.listeners = listeners; }
public CacheEventListener[] getListeners() { return this.listeners; }
public void setListeners(CacheEventListener ... listeners) { this.listeners = listeners; }
public void afterPropertiesSet() throws CacheException, IOException { // If no CacheManager given, fetch the default. if (this.cacheManager == null) { if (logger.isDebugEnabled()) { logger.debug("Using default EhCache CacheManager for cache region '" + this.cacheName + "'"); } this.cacheManager = CacheManager.getInstance(); } // If no cache name given, use bean name as cache name. if (this.cacheName == null) { this.cacheName = this.beanName; } synchronized (this.cacheManager) { // Fetch cache region: If none with the given name exists, // create one on the fly. Ehcache rawCache; boolean cacheExists = this.cacheManager.cacheExists(this.cacheName); if (cacheExists) { if (logger.isDebugEnabled()) { logger.debug("Using existing EhCache cache region '" + this.cacheName + "'"); } rawCache = this.cacheManager.getEhcache(this.cacheName); } else { if (logger.isDebugEnabled()) { logger.debug("Creating new EhCache cache region '" + this.cacheName + "'"); } rawCache = createCache(); } if (this.cacheEventListeners != null) { for (CacheEventListener listener : this.cacheEventListeners) { rawCache.getCacheEventNotificationService().registerListener(listener); } } // Needs to happen after listener registration but before setStatisticsEnabled if (!cacheExists) { this.cacheManager.addCache(rawCache); } // Only necessary on EhCache <2.7: As of 2.7, statistics are on by default. if (setStatisticsAvailable) { if (this.statisticsEnabled) { rawCache.setStatisticsEnabled(true); } if (this.sampledStatisticsEnabled) { rawCache.setSampledStatisticsEnabled(true); } } if (this.disabled) { rawCache.setDisabled(true); } Ehcache decoratedCache = decorateCache(rawCache); if (decoratedCache != rawCache) { this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache); } this.cache = decoratedCache; } }
@Override public CacheEventListener createCacheEventListener(Properties properties) { return new EhCacheEventListener(); }
@Override public CacheEventListener createCacheEventListener(Properties properties) { return new SessionCacheListener(); }
/** * Specify EhCache {@link net.sf.ehcache.event.CacheEventListener cache event listeners} * to registered with this cache. */ public void setCacheEventListeners(Set<CacheEventListener> cacheEventListeners) { this.cacheEventListeners = cacheEventListeners; }
/** * Create the cache event listener that does the replication. * * @return the event listener */ private CacheEventListener createEventListener() { return new RMICacheReplicatorFactory().createCacheEventListener(settings); }
/** * Specify EHCache {@link net.sf.ehcache.event.CacheEventListener cache event listeners} * to registered with this cache. */ public void setCacheEventListeners(Set<CacheEventListener> cacheEventListeners) { this.cacheEventListeners = cacheEventListeners; }