Class JdkLruCache<K,​V>

  • All Implemented Interfaces:
    ClickHouseCache<K,​V>

    public class JdkLruCache<K,​V>
    extends Object
    implements ClickHouseCache<K,​V>
    A simple thread-safe LRU cache based on LinkedHashMap. It's not as effient as the one in Caffeine/Guava, but it requires no extra dependency.
    • Constructor Detail

      • JdkLruCache

        protected JdkLruCache​(Map<K,​V> cache,
                              Function<K,​V> loadFunc)
    • Method Detail

      • create

        public static <K,​V> ClickHouseCache<K,​V> create​(int capacity,
                                                                    Function<K,​V> loadFunc)
        Creates a cache with given capacity and load function.
        Type Parameters:
        K - type of the key
        V - type of the value
        Parameters:
        capacity - capacity
        loadFunc - load function
        Returns:
        cache
      • get

        public V get​(K key)
        Description copied from interface: ClickHouseCache
        Gets value from cache if it exists.
        Specified by:
        get in interface ClickHouseCache<K,​V>
        Parameters:
        key - key, in genernal should NOT be null
        Returns:
        non-null value in general
      • unwrap

        public <T> T unwrap​(Class<T> clazz)
        Description copied from interface: ClickHouseCache
        Gets inner cache object to gain more access.
        Specified by:
        unwrap in interface ClickHouseCache<K,​V>
        Type Parameters:
        T - type of the cache
        Parameters:
        clazz - non-null class of the cache
        Returns:
        inner cache object