Interface Cache<K,​V>


  • public interface Cache<K,​V>
    A cache from keys to values.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      static <K,​V>
      Cache<K,​V>
      bounded​(int capacity)
      Returns new bounded cache.
      V computeIfAbsent​(K key, Function<? super K,​? extends V> mappingFunction)
      Returns the cached value associated with the provided key.
      V get​(K key)
      Returns the cached value associated with the provided key if present, or null otherwise.
      void put​(K key, V value)
      Puts the value into the cache for the key.
      void remove​(K key)
      Removes a value for key if present.
      static <K,​V>
      Cache<K,​V>
      weak()
      Returns new unbounded cache
    • Method Detail

      • weak

        static <K,​V> Cache<K,​V> weak()
        Returns new unbounded cache

        Keys are referenced weakly and compared using identity comparison, not Object.equals(Object).

      • bounded

        static <K,​V> Cache<K,​V> bounded​(int capacity)
        Returns new bounded cache.

        Both keys and values are strongly referenced.

      • computeIfAbsent

        V computeIfAbsent​(K key,
                          Function<? super K,​? extends V> mappingFunction)
        Returns the cached value associated with the provided key. If no value is cached yet, computes the value using mappingFunction, stores the result, and returns it.
      • get

        @Nullable
        V get​(K key)
        Returns the cached value associated with the provided key if present, or null otherwise.
      • put

        void put​(K key,
                 V value)
        Puts the value into the cache for the key.
      • remove

        void remove​(K key)
        Removes a value for key if present.