Class LRUCache<K,​V>

  • Type Parameters:
    K - The key
    V - The value
    K - The key
    V - The value
    All Implemented Interfaces:
    Cache<K,​V>

    public class LRUCache<K,​V>
    extends Object
    implements Cache<K,​V>
    Implementation of a Least Recently Used cache policy.
    Author:
    Simone Bordet
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  LRUCache.LRUCacheEntry<K,​V>
      Double linked cell used as entry in the cache list.
      class  LRUCache.LRUList
      Double queued list used to store cache entries.
    • Constructor Summary

      Constructors 
      Constructor Description
      LRUCache​(int max)
      Creates a LRU cache
    • Constructor Detail

      • LRUCache

        public LRUCache​(int max)
        Creates a LRU cache
        Parameters:
        max - The maximum number of entries
    • Method Detail

      • get

        public V get​(K key)
        Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
        Implementations of this method must have complexity of order O(1). Differently from Cache.peek(K) this method not only return whether the object is present in the cache or not, but also applies the implemented policy that will "refresh" the cached object in the cache, because this cached object was really requested.
        Specified by:
        get in interface Cache<K,​V>
        Parameters:
        key - the key paired with the object
        Returns:
        the object
        See Also:
        Cache.peek(K)
      • peek

        public V peek​(K key)
        Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
        Implementations of this method must have complexity of order O(1). This method should not apply the implemented caching policy to the object paired with the given key, so that a client can query if an object is cached without "refresh" its cache status. Real requests for the object must be done using Cache.get(K).
        Specified by:
        peek in interface Cache<K,​V>
        Parameters:
        key - the key paired with the object
        Returns:
        the object
        See Also:
        Cache.get(K)
      • insert

        public void insert​(K key,
                           V o)
        Inserts the specified object into the cache following the implemented policy.
        Implementations of this method must have complexity of order O(1).
        Specified by:
        insert in interface Cache<K,​V>
        Parameters:
        key - the key paired with the object
        o - the object to cache
        See Also:
        Cache.remove(K)
      • remove

        public void remove​(K key)
        Remove the cached object paired with the specified key.
        Implementations of this method must have complexity of order O(1).
        Specified by:
        remove in interface Cache<K,​V>
        Parameters:
        key - the key paired with the object
        See Also:
        Cache.insert(K, V)
      • flush

        public void flush()
        Flushes the cached objects from the cache.
        Specified by:
        flush in interface Cache<K,​V>
      • size

        public int size()
        The cache size
        Specified by:
        size in interface Cache<K,​V>
        Returns:
        the size of the cache
      • setListener

        public void setListener​(CacheListener listener)
        Set the cache listener
        Specified by:
        setListener in interface Cache<K,​V>
        Parameters:
        listener - The listener