Class LRUCache


  • public class LRUCache
    extends Object
    An LRU cache, based on LinkedHashMap.

    This cache has a fixed maximum number of elements (cacheSize). If the cache is full and another entry is added, the LRU (least recently used) entry is dropped.

    This class is thread-safe. All methods of this class are synchronized.

    Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
    Multi-licensed: EPL / LGPL / GPL / AL / BSD.

    Modified slightly to fit the use of a wayback file cache.

    • Constructor Detail

      • LRUCache

        public LRUCache​(File dir,
                        int cacheSize)
        Creates a new LRU cache. Using filename as the key, and the cached file as the value.
        Parameters:
        dir - The directory where the file is stored.
        cacheSize - the maximum number of entries that will be kept in this cache.
      • LRUCache

        public LRUCache()
        Constructor, where the arguments for the primary constructor is read from settings.
    • Method Detail

      • getInstance

        public static LRUCache getInstance()
        Returns:
        instance of our Cache
      • get

        public File get​(String key)
        Retrieves an entry from the cache.
        The retrieved entry becomes the MRU (most recently used) entry.
        Parameters:
        key - the key whose associated value is to be returned.
        Returns:
        the value associated to this key, or null if no value with this key exists in the cache.
      • put

        public void put​(String key,
                        File value)
        Adds an entry to this cache. The new entry becomes the MRU (most recently used) entry. If an entry with the specified key already exists in the cache, it is replaced by the new entry. If the cache is full, the LRU (least recently used) entry is removed from the cache.
        Parameters:
        key - the key with which the specified value is to be associated.
        value - a value to be associated with the specified key.
      • clear

        public void clear()
        Clears the cache.
      • usedEntries

        public int usedEntries()
        Returns the number of used entries in the cache.
        Returns:
        the number of entries currently in the cache.
      • getCacheDir

        public File getCacheDir()
        Returns:
        the cacheDir