de.caff.generics

## Class RecreatingLeastRecentlyUsedCache<K,V>

- java.lang.Object
- - de.caff.generics.LeastRecentlyUsedCache<K,V>
  - - de.caff.generics.RecreatingLeastRecentlyUsedCache<K,V>

-     public class RecreatingLeastRecentlyUsedCache<K,V>
      extends LeastRecentlyUsedCache<K,V>

  A least recently used cache which also allows to predefine a way to recreate lost entries.

  A good way to use it is construction with a function which is able to recreate the value for a given key and calling only the `get(Object)` method.

  Since:
    May 10, 2021
  Author:
    Rammi

- - ### Field Summary

    - ### Fields inherited from class de.caff.generics.LeastRecentlyUsedCache

      `map`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RecreatingLeastRecentlyUsedCache(int mostRecentLimit, FragileFunction1<V,? extends java.lang.Exception,K> recreator, Procedure2<K,java.lang.Exception> errorHandler)` Constructor.
    `RecreatingLeastRecentlyUsedCache(int mostRecentLimit, java.util.function.Function<K,V> recreator)` Constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `cleanup()` Remove all key-value pairs which are no longer available.
    `V` | `get(K key)` Get the value for the given key.
    `int` | `getNumRecreations()` Get the number of times the recreation function was called.
    `V` | `remove(K key)` Remove a key-value pair from this cache.

    - ### Methods inherited from class de.caff.generics.LeastRecentlyUsedCache

      `clear, computeIfAbsent, getLeastRecentCacheDepth, getLeastRecentlyUsed, getNumGarbageCollected, getNumHits, getNumMisses, isEmpty, keys, put, size`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### RecreatingLeastRecentlyUsedCache

          public RecreatingLeastRecentlyUsedCache(int mostRecentLimit,
                                                  @NotNull
                                                  java.util.function.Function<K,V> recreator)

      Constructor.
      Parameters:
        `mostRecentLimit` - maximal number of cached values, non-negative
        `recreator` - creator for values which are currently not present

    - #### RecreatingLeastRecentlyUsedCache

          public RecreatingLeastRecentlyUsedCache(int mostRecentLimit,
                                                  @NotNull
                                                  FragileFunction1<V,? extends java.lang.Exception,K> recreator,
                                                  @NotNull
                                                  Procedure2<K,java.lang.Exception> errorHandler)

      Constructor.

      This allows to use an recreator which might throw an exception. In case of exceptions obviously nothing is created, but the `errorHandler` will be called with the key an exception.

      Parameters:
        `mostRecentLimit` - maximal number of cached values, non-negative
        `recreator` - creator for values which are currently not present
        `errorHandler` - error handler for exceptions during the recreation

  - ### Method Detail

    - #### get

          @Nullable
          public V get(@NotNull
                                 K key)

      Description copied from class: `LeastRecentlyUsedCache`
      Get the value for the given key.
      Overrides:
        `get` in class `LeastRecentlyUsedCache<K,V>`
      Parameters:
        `key` - key associated with the requested value
      Returns:
        `null` when the required value was never added to this cache or when it was garbage collected in the meantime, otherwise the cached object associated with the given key

    - #### cleanup

          public void cleanup()

      Description copied from class: `LeastRecentlyUsedCache`
      Remove all key-value pairs which are no longer available.
      Overrides:
        `cleanup` in class `LeastRecentlyUsedCache<K,V>`

    - #### remove

          @Nullable
          public V remove(K key)

      Description copied from class: `LeastRecentlyUsedCache`
      Remove a key-value pair from this cache.
      Overrides:
        `remove` in class `LeastRecentlyUsedCache<K,V>`
      Parameters:
        `key` - key of the key-value pair
      Returns:
        removed object if it was still cached, or `null` if the object was either never cached at all or has been garbage collected

    - #### getNumRecreations

          public int getNumRecreations()

      Get the number of times the recreation function was called.
      Returns:
        number of recreations