Class CacheLRU

  • All Implemented Interfaces:
    java.io.Serializable, Cache

    public final class CacheLRU
    extends GenericCache
    This class is a GenericCache subclass implementing an LRU (Least Recently Used) cache replacement policy. In other words, values are added to the cache until it becomes full. Once the cache is full, when a new value is added to the cache, it replaces the least recently used value currently in the cache. This is probably the best general purpose cache replacement policy.
    Since:
    1.0
    See Also:
    GenericCache, Serialized Form
    • Field Detail

      • __head

        private int __head
      • __tail

        private int __tail
      • __next

        private final int[] __next
      • __prev

        private final int[] __prev
    • Constructor Detail

      • CacheLRU

        public CacheLRU​(int capacity)
        Creates a CacheLRU instance with a given cache capacity.

        Parameters:
        capacity - The capacity of the cache.
      • CacheLRU

        public CacheLRU()
        Same as:
         CacheLRU(GenericCache.DEFAULT_CAPACITY);
         
    • Method Detail

      • __moveToFront

        private void __moveToFront​(int index)
      • getElement

        public java.lang.Object getElement​(java.lang.Object key)
        Description copied from interface: Cache
        Gets an element from the cache.
        Specified by:
        getElement in interface Cache
        Overrides:
        getElement in class GenericCache
        Parameters:
        key - Key
        Returns:
        Element value
      • addElement

        public final void addElement​(java.lang.Object key,
                                     java.lang.Object value)
        Adds a value to the cache. If the cache is full, when a new value is added to the cache, it replaces the least recently used value in the cache (i.e., LRU).

        Specified by:
        addElement in interface Cache
        Specified by:
        addElement in class GenericCache
        Parameters:
        key - The key referencing the value added to the cache.
        value - The value to add to the cache.