All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tuwien.auto.calimero.buffer.cache.ExpiringCache Maven / Gradle / Ivy

The newest version!
/*
    Calimero 2 - A library for KNX network access
    Copyright (c) 2006, 2023 B. Malinowsky

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Linking this library statically or dynamically with other modules is
    making a combined work based on this library. Thus, the terms and
    conditions of the GNU General Public License cover the whole
    combination.

    As a special exception, the copyright holders of this library give you
    permission to link this library with independent modules to produce an
    executable, regardless of the license terms of these independent
    modules, and to copy and distribute the resulting executable under terms
    of your choice, provided that you also meet, for each linked independent
    module, the terms and conditions of the license of that module. An
    independent module is a module which is not derived from or based on
    this library. If you modify this library, you may extend this exception
    to your version of the library, but you are not obligated to do so. If
    you do not wish to do so, delete this exception statement from your
    version.
*/

package tuwien.auto.calimero.buffer.cache;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import tuwien.auto.calimero.internal.Executor;

/**
 * Implements a cache expiring mechanism for {@link CacheObject}s.
 * 

* A time span is specified for how long a value is considered valid. After that time for * expiring, the cache object is removed on the next call of {@link #removeExpired()} by * an internal cache sweeper. If a cache with expiring cache objects is not used * anymore, invoke {@link Cache#clear()} to quit the running cache sweeping mechanism.
* The timestamp of {@link CacheObject#getTimestamp()} is used to determine if a cache * object value has expired.
* Note that if the timestamp of a {@link CacheObject} changes after it was put into the * cache, the object has to be reinserted (see {@link #put(CacheObject)}) to keep the * cache in a consistent state. If no expiring is used, this might be omitted.
* * @author B. Malinowsky */ public abstract class ExpiringCache implements Cache { /** * Default sweep interval in seconds used for a cache sweeper * ({@value defaultSweepInterval} seconds). */ protected static final int defaultSweepInterval = 60; /** * Sweep interval in seconds used for the cache sweeper. *

* It defaults to {@value #defaultSweepInterval} seconds. For a new value to take * effect, it has to be assigned either before the first {@link #startSweeper()} call, * or the cache sweeper has to be restarted. */ protected int sweepInterval = defaultSweepInterval; /** * The map holding the {@link CacheObject}s. *

* The map instance itself is not synchronized, synchronization is done using the * cache object (this). */ protected final Map map; private ScheduledFuture sweeper; private final int timeToExpire; /** * Creates an {@link ExpiringCache}. *

* Note that for the actual begin of cache sweeping {@link #startSweeper()} has to be * invoked. * * @param timeToExpire time > 0 in seconds for cache entries to stay valid, on time = * 0 no expiring of cache entries will occur */ public ExpiringCache(final int timeToExpire) { if (timeToExpire > 0) { this.timeToExpire = timeToExpire; map = new LinkedHashMap<>(); } else { this.timeToExpire = 0; map = new HashMap<>(); } } /** * Removes all {@link CacheObject CacheObjects}, where {@code CacheObject::getTimestamp + timeToExpire ≤ now}, with * {@code timeToExpire > 0} and {@code now} is the point of time {@link #removeExpired()} is invoked. * If no expiring time was specified at creation of cache, no cache object will be expired. */ @Override public void removeExpired() { if (timeToExpire == 0 || !(map instanceof LinkedHashMap)) return; final long now = System.currentTimeMillis(); final long duration = timeToExpire * 1000L; CacheObject o = null; synchronized (this) { for (final Iterator i = map.values().iterator(); i.hasNext();) { o = i.next(); if (now >= o.getTimestamp() + duration) { i.remove(); notifyRemoved(o); } else break; } } } /** * Override this method to get notified when {@link #removeExpired()} removed a * {@link CacheObject} from the {@link #map}. * * @param obj removed {@link CacheObject} */ protected void notifyRemoved(final CacheObject obj) {} /** * Starts a new cache sweeper, if not already running, and if an expiring time * for {@link CacheObject} was specified.
* If the methods {@code startSweeper} and {@code stopSweeper} are invoked * by different threads, they need to be synchronized. */ protected final void startSweeper() { if (timeToExpire > 0 && sweeper == null) { final Runnable task = () -> { Thread.currentThread().setName("Calimero cache sweeper"); removeExpired(); }; final int delay = sweepInterval; sweeper = Executor.scheduledExecutor().scheduleWithFixedDelay(task, delay, delay, TimeUnit.SECONDS); } } /** * Stops the cache sweeper, if any. */ protected final void stopSweeper() { final var s = sweeper; if (s != null) { s.cancel(true); sweeper = null; } } static void updateAccess(final CacheObject obj) { obj.incCount(); obj.setUsage(obj.getCount()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy