Class LruCache<K,​V>

  • Type Parameters:
    K - type of the keys of the map
    V - type of the values of the map

    public final class LruCache<K,​V>
    extends Object
    Least recently used cache. This cache has a capacity. When the capacity is reached, the oldest record is removed from the cache when a new one is added.
    • Field Detail


        public static final int DEFAULT_CAPACITY
        Default capacity of the cache: 10000.
        See Also:
        Constant Field Values
    • Method Detail

      • builder

        public static <K,​V> LruCache.Builder<K,​V> builder()
        Create a new builder.
        Type Parameters:
        K - key type
        V - value type
        a new fluent API builder instance
      • create

        public static <K,​V> LruCache<K,​V> create()
        Create an instance with default configuration.
        Type Parameters:
        K - key type
        V - value type
        a new cache instance
        See Also:
      • get

        public Optional<V> get​(K key)
        Get a value from the cache.
        key - key to retrieve
        value if present or empty
      • remove

        public Optional<V> remove​(K key)
        Remove a value from the cache.
        key - key of the record to remove
        the value that was mapped to the key, or empty if none was
      • put

        public Optional<V> put​(K key,
                               V value)
        Put a value to the cache.
        key - key to add
        value - value to add
        value that was already mapped or empty if the value was not mapped
      • computeValue

        public Optional<V> computeValue​(K key,
                                        Supplier<Optional<V>> valueSupplier)
        Either return a cached value or compute it and cache it. In case this method is called in parallel for the same key, the value actually present in the map may be from any of the calls. This method always returns either the existing value from the map, or the value provided by the supplier. It never returns a result from another thread's supplier.
        key - key to check/insert value for
        valueSupplier - supplier called if the value is not yet cached, or is invalid
        current value from the cache, or computed value from the supplier
      • size

        public int size()
        Current size of the map.
        number of records currently cached
      • capacity

        public int capacity()
        Capacity of this cache.
        configured capacity of this cache