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

uk.num.numlib.internal.util.SimpleCache Maven / Gradle / Ivy

package uk.num.numlib.internal.util;

import java.util.HashMap;
import java.util.Map;

/**
 * A very basic cache with a timeout mechanism.
 *
 * @param  The class that the keys will be instances of.
 * @param  The class that the values will be instances of.
 * @author tonywalmsley
 */
public class SimpleCache {

    /**
     * The default cache timeout.
     */
    private static final int TEN_MINUTES = 1000 * 60 * 10;

    /**
     * The actual cache is a HashMap of K,CacheEntry pairs.
     */
    private Map> cache = new HashMap<>();

    /**
     * The overridable cache timeout - defaults to 10 minutes.
     */
    private int millis = TEN_MINUTES;

    /**
     * Default Constructor
     */
    public SimpleCache() {
    }

    /**
     * Override the default cache timeour. Can be set at any time but only affects items cached after the value is set.
     *
     * @param millis The number of milliseconds until the cached item should be considered stale.
     */
    public void setTimeToLive(int millis) {
        this.millis = millis;
    }

    /**
     * Add an item to the cache.
     *
     * @param key   The key of class K
     * @param value The value of class V
     */
    public void put(K key, V value) {
        final CacheEntry entry = new CacheEntry<>(value, millis + System.currentTimeMillis());
        cache.put(key, entry);
    }

    /**
     * Get an item from the cache.
     *
     * @param key The key of class K
     * @return null if the item is not present or has expired, otherwise the value of class V
     */
    public V get(K key) {
        final CacheEntry entry = cache.get(key);
        if (entry == null || entry.expiry < System.currentTimeMillis()) {

            // Remove the expired item
            if (entry != null) {
                cache.remove(key);
            }
            return null;
        }
        return entry.value;
    }

    /**
     * The CacheEntry holds the cached value and the expiry time of the item.
     *
     * @param  The class of the values to be cached.
     */
    private static class CacheEntry {
        private V value;
        private long expiry;

        /**
         * Constructor.
         *
         * @param value  The value to be cached.
         * @param expiry The expiry time of the cached item.
         */
        CacheEntry(V value, long expiry) {
            this.value = value;
            this.expiry = expiry;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy