/** * A map with expiration. This class contains a worker thread that will * periodically check this class in order to determine if any objects * should be removed based on the provided time-to-live value. * 带过期时间的Map。这个类包含一个工作线程,它将定期检查这个类,以便根据提供的生存时间值确定是否应该删除任何对象。 * * @param <K> The key type * @param <V> The value type * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ publicclassExpiringMap<K, V> implementsMap<K, V> { /** * The default value, 60 seconds */ publicstaticfinalintDEFAULT_TIME_TO_LIVE=60;
/** * The default value, 1 second */ publicstaticfinalintDEFAULT_EXPIRATION_INTERVAL=1;
/** * Creates a new instance of ExpiringMap using the default values * DEFAULT_TIME_TO_LIVE and DEFAULT_EXPIRATION_INTERVAL */ publicExpiringMap() { this(DEFAULT_TIME_TO_LIVE, DEFAULT_EXPIRATION_INTERVAL); }
/** * Creates a new instance of ExpiringMap using the supplied * time-to-live value and the default value for DEFAULT_EXPIRATION_INTERVAL * * @param timeToLive The time-to-live value (seconds) */ publicExpiringMap(int timeToLive) { this(timeToLive, DEFAULT_EXPIRATION_INTERVAL); }
/** * Creates a new instance of ExpiringMap using the supplied values and * a {@link ConcurrentHashMap} for the internal data structure. * * @param timeToLive The time-to-live value (seconds) * @param expirationInterval The time between checks to see if a value should be removed (seconds) */ publicExpiringMap(int timeToLive, int expirationInterval) { this(newConcurrentHashMap<>(), newCopyOnWriteArrayList<>(), timeToLive, expirationInterval); }
privateExpiringMap(ConcurrentHashMap<K, ExpiringObject> delegate, CopyOnWriteArrayList<ExpirationListener<V>> expirationListeners, int timeToLive, int expirationInterval) { this.delegate = delegate; this.expirationListeners = expirationListeners;
/** * Adds a listener in the expiration listeners * * @param listener The listener to add */ publicvoidaddExpirationListener(ExpirationListener<V> listener) { expirationListeners.add(listener); }
/** * Removes a listener from the expiration listeners * * @param listener The listener to remove */ publicvoidremoveExpirationListener(ExpirationListener<V> listener) { expirationListeners.remove(listener); }
/** * @return The Expirer instance */ public Expirer getExpirer() { return expirer; }
/** * Get the interval in which an object will live in the map before it is removed. * * @return The expiration time in second */ publicintgetExpirationInterval() { return expirer.getExpirationInterval(); }
/** * @return the Time-to-live value in seconds. */ publicintgetTimeToLive() { return expirer.getTimeToLive(); }
/** * Set the interval in which an object will live in the map before it is removed. * * @param expirationInterval The expiration time in seconds */ publicvoidsetExpirationInterval(int expirationInterval) { expirer.setExpirationInterval(expirationInterval); }
/** * Update the value for the time-to-live * * @param timeToLive The time-to-live (seconds) */ publicvoidsetTimeToLive(int timeToLive) { expirer.setTimeToLive(timeToLive); }
/** * A Thread that monitors an {@link ExpiringMap} and will remove * elements that have passed the threshold. * 一个线程,它监视{@link ExpiringMap},并将删除超过阈值的元素。 */ publicclassExpirerimplementsRunnable { privatefinalReadWriteLockstateLock=newReentrantReadWriteLock();
privatelong timeToLiveMillis;
privatelong expirationIntervalMillis;
privatebooleanrunning=false;
privatefinal Thread expirerThread;
/** * Creates a new instance of Expirer. */ publicExpirer() { expirerThread = newThread(this, "ExpiringMapExpirer-" + expirerCount++); //守护线程 expirerThread.setDaemon(true); }
/** * Kick off this thread which will look for old objects and remove them. * 启动这个线程,它将查找旧对象并删除它们。 */ publicvoidstartExpiring() { stateLock.writeLock().lock();
/** * If this thread has not started, then start it. * Otherwise just return; * 如果这个线程还没有启动,那么启动它。否则就返回; */ publicvoidstartExpiringIfNotStarted() { stateLock.readLock().lock();
/** * Checks to see if the thread is running * * @return If the thread is running, true. Otherwise false. */ publicbooleanisRunning() { stateLock.readLock().lock();
/** * Update the value for the time-to-live * * @param timeToLive The time-to-live (seconds) */ publicvoidsetTimeToLive(long timeToLive) { stateLock.writeLock().lock();
/** * Get the interval in which an object will live in the map before * it is removed. * * @return The time in seconds. */ publicintgetExpirationInterval() { stateLock.readLock().lock();
/** * Set the interval in which an object will live in the map before * it is removed. * * @param expirationInterval The time in seconds */ publicvoidsetExpirationInterval(long expirationInterval) { stateLock.writeLock().lock();
/** * A listener for expired object events. * 过期对象事件的侦听器。 * * @param <E> The event type * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ publicinterfaceExpirationListener<E> { /** * Adds a given event to the listener * 向侦听器添加给定的事件 * * @param expiredObject The expired event */ voidexpired(E expiredObject); }