Class LongLruCache<E>

  • Type Parameters:
    E - the type of element that this cache holds.
    All Implemented Interfaces:
    java.lang.AutoCloseable

    public final class LongLruCache<E>
    extends java.lang.Object
    implements java.lang.AutoCloseable
    A fixed capacity cache of long keyed values that evicts the least-recently-used element when it runs out of space.

    When an element is evicted it is closed by calling the closer function with the element as an argument.

    When a new key arrives the factory function is called in order to create the new element associated with that key.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int capacity  
      private java.util.function.Consumer<E> closer  
      private java.util.function.LongFunction<E> factory  
      private long[] keys  
      private int size  
      private java.lang.Object[] values  
    • Constructor Summary

      Constructors 
      Constructor Description
      LongLruCache​(int capacity, java.util.function.LongFunction<E> factory, java.util.function.Consumer<E> closer)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int capacity()
      Returns cache capacity.
      void close()
      E lookup​(long key)
      Looks up an element in the cache, creating a new element if it doesn't exist and evicting the least recently used element if there's no space left in the cache.
      private void makeMostRecent​(long key, java.lang.Object value, int fromIndex)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • capacity

        private final int capacity
      • factory

        private final java.util.function.LongFunction<E> factory
      • closer

        private final java.util.function.Consumer<E> closer
      • keys

        private final long[] keys
      • values

        private final java.lang.Object[] values
      • size

        private int size
    • Constructor Detail

      • LongLruCache

        public LongLruCache​(int capacity,
                            java.util.function.LongFunction<E> factory,
                            java.util.function.Consumer<E> closer)
        Constructor.
        Parameters:
        capacity - this is the fixed capacity of the cache.
        factory - a function for constructing new elements based upon keys.
        closer - a function for cleaning up resources associated with elements.
    • Method Detail

      • lookup

        public E lookup​(long key)
        Looks up an element in the cache, creating a new element if it doesn't exist and evicting the least recently used element if there's no space left in the cache.
        Parameters:
        key - the key to look up the element by.
        Returns:
        the element associated with this key.
      • makeMostRecent

        private void makeMostRecent​(long key,
                                    java.lang.Object value,
                                    int fromIndex)
      • capacity

        public int capacity()
        Returns cache capacity.
        Returns:
        cache capacity.
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable