public void addListener(CacheEventListener listener) { ehCache.getCacheEventNotificationService().registerListener(listener); }
public void removeListener(CacheEventListener cacheEventListener) { ehCache.getCacheEventNotificationService().unregisterListener(cacheEventListener); }
@Override protected Registration doRegisterListener(CacheEventListener listenerAdapter) { ehCache.getCacheEventNotificationService().registerListener(listenerAdapter); return () -> ehCache.getCacheEventNotificationService().unregisterListener(listenerAdapter); }
protected void registerAsCacheEvictionListener() { ehCache.getCacheEventNotificationService().registerListener(new CacheEvictionListener(this)); }
rawCache.getCacheEventNotificationService().registerListener(listener);
rawCache.getCacheEventNotificationService().registerListener(listener);
/** * {@inheritDoc} */ public RegisteredEventListeners getCacheEventNotificationService() { return underlyingCache.getCacheEventNotificationService(); }
/** * {@inheritDoc} */ public RegisteredEventListeners getCacheEventNotificationService() { // THIS IS GENERATED CODE -- DO NOT HAND MODIFY! Thread t = Thread.currentThread(); ClassLoader prev = t.getContextClassLoader(); t.setContextClassLoader(this.classLoader); try { return this.cache.getCacheEventNotificationService(); } finally { t.setContextClassLoader(prev); } }
/** * Determine if the given cache is distributed. * * @param cache the cache to check * @return true if a <code>CacheReplicator</code> is found in the listeners */ protected boolean isDistributed(Ehcache cache) { Set listeners = cache.getCacheEventNotificationService().getCacheEventListeners(); for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { CacheEventListener cacheEventListener = (CacheEventListener) iterator.next(); if (cacheEventListener instanceof CacheReplicator) { return true; } } return false; }
/** * Before eviction elements are checked. * * @param element the element to notify about its expiry */ private void notifyExpiry(final Element element) { cache.getCacheEventNotificationService().notifyElementExpiry(copyStrategyHandler.copyElementForReadIfNeeded(element), false); }
/** * Before eviction elements are checked. * * @param element */ protected final void notifyExpiry(Element element) { cache.getCacheEventNotificationService().notifyElementExpiry(copyStrategyHandler.copyElementForReadIfNeeded(element), false); }
@Override public synchronized void notifyCacheEventListenersChanged() { if (cache.getCacheEventNotificationService().hasCacheEventListeners() && !cacheEventListenerRegistered) { backend.addListener(evictionListener); cacheEventListenerRegistered = true; } else if (!cache.getCacheEventNotificationService().hasCacheEventListeners() && cacheEventListenerRegistered) { dropLeaderStatus(); backend.removeListener(evictionListener); cacheEventListenerRegistered = false; } }
/** * Called when an element is evicted even before it could be installed inside the store * * @param element the evicted element */ protected void notifyDirectEviction(final Element element) { evictionObserver.begin(); evictionObserver.end(EvictionOutcome.SUCCESS); cache.getCacheEventNotificationService().notifyElementEvicted(copyStrategyHandler.copyElementForReadIfNeeded(element), false); }
/** * 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); } }
/** * Expire all elements. * <p> * This is a default implementation which does nothing. Expiration on demand is only implemented for disk stores. */ public void expireElements() { for (Object key : keySet()) { final Element element = expireElement(key); if (element != null) { cache.getCacheEventNotificationService() .notifyElementExpiry(copyStrategyHandler.copyElementForReadIfNeeded(element), false); } } }
public SoftLockManager getOrCreateClusteredSoftLockFactory(Ehcache cache) { String name = toolkitInstanceFactory.getFullyQualifiedCacheName(cache); SoftLockManager softLockFactory = softLockFactories.get(name); if (softLockFactory == null) { softLockFactory = new ReadCommittedClusteredSoftLockFactory(toolkitInstanceFactory, cache.getCacheManager() .getName(), cache.getName()); SoftLockManager old = softLockFactories.putIfAbsent(name, softLockFactory); if (old == null) { // Put successful add a Cache Event Listener. cache.getCacheEventNotificationService().registerListener(new EventListener(name)); } else { softLockFactory = old; } } return softLockFactory; }
/** * Evicts the element from the store * @param element the element to be evicted * @return true if succeeded, false otherwise */ protected boolean evict(final Element element) { final ReentrantReadWriteLock.WriteLock lock = map.lockFor(element.getObjectKey()).writeLock(); if (lock.tryLock()) { evictionObserver.begin(); Element remove; try { remove = remove(element.getObjectKey()); } finally { lock.unlock(); } if (remove != null) { evictionObserver.end(EvictionOutcome.SUCCESS); cache.getCacheEventNotificationService().notifyElementEvicted(copyStrategyHandler.copyElementForReadIfNeeded(remove), false); } return remove != null; } return false; }
/** * Creates a persitent-to-disk store for the given cache, using the given disk path. * * @param cache cache that fronts this store * @param onHeapPool pool to track heap usage * @param onDiskPool pool to track disk usage * @return a fully initialized store */ public static DiskStore create(Ehcache cache, Pool onHeapPool, Pool onDiskPool) { if (cache.getCacheManager() == null) { throw new CacheException("Can't create diskstore without a cache manager"); } DiskStorageFactory disk = new DiskStorageFactory(cache, cache.getCacheEventNotificationService()); DiskStore store = new DiskStore(disk, cache, onHeapPool, onDiskPool); cache.getCacheConfiguration().addConfigurationListener(new CacheConfigurationListenerAdapter(disk, onDiskPool)); return store; }
private DiskStore(DiskStorageFactory disk, Ehcache cache, Pool onHeapPool, Pool onDiskPool) { this.segments = new Segment[DEFAULT_SEGMENT_COUNT]; this.segmentShift = Integer.numberOfLeadingZeros(segments.length - 1); EventRateSimpleMovingAverage hitRate = new EventRateSimpleMovingAverage(1, TimeUnit.SECONDS); EventRateSimpleMovingAverage missRate = new EventRateSimpleMovingAverage(1, TimeUnit.SECONDS); OperationStatistic<GetOutcome> getStatistic = StatisticsManager.getOperationStatisticFor(getObserver); getStatistic.addDerivedStatistic(new OperationResultFilter<GetOutcome>(EnumSet.of(GetOutcome.HIT), hitRate)); getStatistic.addDerivedStatistic(new OperationResultFilter<GetOutcome>(EnumSet.of(GetOutcome.MISS), missRate)); this.onHeapPoolAccessor = onHeapPool.createPoolAccessor(new DiskStoreHeapPoolParticipant(hitRate, missRate), SizeOfPolicyConfiguration.resolveMaxDepth(cache), SizeOfPolicyConfiguration.resolveBehavior(cache).equals(SizeOfPolicyConfiguration.MaxDepthExceededBehavior.ABORT)); this.onDiskPoolAccessor = onDiskPool.createPoolAccessor(new DiskStoreDiskPoolParticipant(hitRate, missRate), new DiskSizeOfEngine()); for (int i = 0; i < this.segments.length; ++i) { this.segments[i] = new Segment(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, disk, cache.getCacheConfiguration(), onHeapPoolAccessor, onDiskPoolAccessor, cache.getCacheEventNotificationService(), evictionObserver); } this.disk = disk; this.disk.bind(this); this.status.set(Status.STATUS_ALIVE); }
private void processEventNotification(CacheEventNotificationMsg msg) { RegisteredEventListeners notificationService = ecache.getCacheEventNotificationService(); switch (msg.getToolkitEventType()) { case ELEMENT_REMOVED: notificationService.notifyElementRemoved(msg.getElement(), true); break; case ELEMENT_PUT: notificationService.notifyElementPut(msg.getElement(), true); break; case ELEMENT_UPDATED: notificationService.notifyElementUpdated(msg.getElement(), true); break; case ELEMENT_EXPIRED: notificationService.notifyElementExpiry(msg.getElement(), true); break; case ELEMENT_EVICTED: notificationService.notifyElementEvicted(msg.getElement(), true); break; case REMOVEALL: notificationService.notifyRemoveAll(true); break; } }