Class CacheLIRS<K,​V>

  • Type Parameters:
    K - the key type
    V - the value type
    All Implemented Interfaces:
    java.util.function.Function<K,​V>, org.apache.jackrabbit.guava.common.base.Function<K,​V>, org.apache.jackrabbit.guava.common.cache.Cache<K,​V>, org.apache.jackrabbit.guava.common.cache.LoadingCache<K,​V>

    @Internal(since="1.1.1")
    @Deprecated(since="2022-12-01")
    public class CacheLIRS<K,​V>
    extends java.lang.Object
    implements org.apache.jackrabbit.guava.common.cache.LoadingCache<K,​V>
    Deprecated.
    The Jackrabbit Oak Cache library is designed for Oak-internal use only and thus deprecated. It will not be part of the AEM SDK after April 2023.
    For Oak internal use only. Do not use outside Oak components.

    A scan resistant cache. It is meant to cache objects that are relatively costly to acquire, for example file content.

    This implementation is multi-threading safe and supports concurrent access. Null keys or null values are not allowed. The map fill factor is at most 75%.

    Each entry is assigned a distinct memory size, and the cache will try to use at most the specified amount of memory. The memory unit is not relevant, however it is suggested to use bytes as the unit.

    This class implements an approximation of the the LIRS replacement algorithm invented by Xiaodong Zhang and Song Jiang as described in http://www.cse.ohio-state.edu/~zhang/lirs-sigmetrics-02.html with a few smaller changes: An additional queue for non-resident entries is used, to prevent unbound memory usage. The maximum size of this queue is at most the size of the rest of the stack. About 6.25% of the mapped entries are cold.

    Internally, the cache is split into a number of segments, and each segment is an individual LIRS cache.

    Accessed entries are only moved to the top of the stack if at least a number of other entries have been moved to the front (1% by default). Write access and moving entries to the top of the stack is synchronized per segment.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CacheLIRS.Builder<K,​V>
      Deprecated.
      The Jackrabbit Oak Cache library is designed for Oak-internal use only and thus deprecated.
      static interface  CacheLIRS.EvictionCallback<K,​V>
      Deprecated.
      The Jackrabbit Oak Cache library is designed for Oak-internal use only and thus deprecated.
    • Constructor Summary

      Constructors 
      Constructor Description
      CacheLIRS​(int maxEntries)
      Deprecated.
      Create a new cache with the given number of entries, and the default settings (an average size of 1 per entry, 16 segments, and stack move distance equals to the maximum number of entries divided by 100).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      V apply​(K key)
      Deprecated.
       
      java.util.concurrent.ConcurrentMap<K,​V> asMap()
      Deprecated.
       
      void cleanUp()
      Deprecated.
       
      boolean containsKey​(java.lang.Object key)
      Deprecated.
      Check whether there is a resident entry for the given key.
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Deprecated.
      Get the entry set for all resident entries.
      V get​(K key)
      Deprecated.
      Get the value, loading it if needed.
      V get​(K key, java.util.concurrent.Callable<? extends V> valueLoader)
      Deprecated.
       
      org.apache.jackrabbit.guava.common.collect.ImmutableMap<K,​V> getAll​(java.lang.Iterable<? extends K> keys)
      Deprecated.
       
      org.apache.jackrabbit.guava.common.collect.ImmutableMap<K,​V> getAllPresent​(java.lang.Iterable<?> keys)
      Deprecated.
       
      int getAverageMemory()
      Deprecated.
      Get the average memory used per entry.
      V getIfPresent​(java.lang.Object key)
      Deprecated.
      Get the value for the given key if the entry is cached.
      long getMaxMemory()
      Deprecated.
      Get the maximum memory to use.
      int getMemory​(K key)
      Deprecated.
      Get the memory used for the given key.
      V getUnchecked​(K key)
      Deprecated.
      Get the value, loading it if needed.
      long getUsedMemory()
      Deprecated.
      Get the currently used memory.
      void invalidate​(java.lang.Object key)
      Deprecated.
      Remove an entry.
      void invalidateAll()
      Deprecated.
      Remove all entries.
      void invalidateAll​(java.lang.Iterable<?> keys)
      Deprecated.
       
      boolean isEmpty()
      Deprecated.
       
      java.util.List<K> keys​(boolean cold, boolean nonResident)
      Deprecated.
      Get the list of keys.
      java.util.Set<K> keySet()
      Deprecated.
      Get the set of keys for resident entries.
      static <K,​V>
      CacheLIRS.Builder<K,​V>
      newBuilder()
      Deprecated.
      Create a builder.
      V peek​(K key)
      Deprecated.
      Get the value for the given key if the entry is cached.
      void put​(K key, V value)
      Deprecated.
      Add an entry to the cache.
      V put​(K key, V value, int memory)
      Deprecated.
      Add an entry to the cache.
      void putAll​(java.util.Map<? extends K,​? extends V> m)
      Deprecated.
       
      void refresh​(K key)
      Deprecated.
      Re-load the value for the given key.
      V remove​(java.lang.Object key)
      Deprecated.
      Remove an entry.
      void setAverageMemory​(int averageMemory)
      Deprecated.
      Set the average memory used per entry.
      void setMaxMemory​(long maxMemory)
      Deprecated.
      Set the maximum memory this cache should use.
      long size()
      Deprecated.
      Get the number of resident entries.
      int sizeHot()
      Deprecated.
      Get the number of hot entries in the cache.
      int sizeMapArray()
      Deprecated.
      Get the length of the internal map array.
      int sizeNonResident()
      Deprecated.
      Get the number of non-resident entries in the cache.
      org.apache.jackrabbit.guava.common.cache.CacheStats stats()
      Deprecated.
       
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.function.Function

        andThen, compose
      • Methods inherited from interface org.apache.jackrabbit.guava.common.base.Function

        equals
    • Constructor Detail

      • CacheLIRS

        public CacheLIRS​(int maxEntries)
        Deprecated.
        Create a new cache with the given number of entries, and the default settings (an average size of 1 per entry, 16 segments, and stack move distance equals to the maximum number of entries divided by 100).
        Parameters:
        maxEntries - the maximum number of entries
    • Method Detail

      • invalidateAll

        public void invalidateAll()
        Deprecated.
        Remove all entries.
        Specified by:
        invalidateAll in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Deprecated.
        Check whether there is a resident entry for the given key. This method does not adjust the internal state of the cache.
        Parameters:
        key - the key (may not be null)
        Returns:
        true if there is a resident entry
      • peek

        public V peek​(K key)
        Deprecated.
        Get the value for the given key if the entry is cached. This method does not modify the internal state.
        Parameters:
        key - the key (may not be null)
        Returns:
        the value, or null if there is no resident entry
      • put

        public V put​(K key,
                     V value,
                     int memory)
        Deprecated.
        Add an entry to the cache. This method is an explicit memory size (weight), and not using the weigher even if configured. The entry may or may not exist in the cache yet. This method will usually mark unknown entries as cold and known entries as hot.
        Parameters:
        key - the key (may not be null)
        value - the value (may not be null)
        memory - the memory used for the given entry
        Returns:
        the old value, or null if there was no resident entry
      • put

        public void put​(K key,
                        V value)
        Deprecated.
        Add an entry to the cache. If a weigher is specified, it is used, otherwise the average memory size is used.
        Specified by:
        put in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
        Parameters:
        key - the key (may not be null)
        value - the value (may not be null)
      • get

        public V get​(K key,
                     java.util.concurrent.Callable<? extends V> valueLoader)
              throws java.util.concurrent.ExecutionException
        Deprecated.
        Specified by:
        get in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
        Throws:
        java.util.concurrent.ExecutionException
      • getUnchecked

        public V getUnchecked​(K key)
        Deprecated.
        Get the value, loading it if needed.

        If there is an exception loading, an UncheckedExecutionException is thrown.

        Specified by:
        getUnchecked in interface org.apache.jackrabbit.guava.common.cache.LoadingCache<K,​V>
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        org.apache.jackrabbit.guava.common.util.concurrent.UncheckedExecutionException
      • get

        public V get​(K key)
              throws java.util.concurrent.ExecutionException
        Deprecated.
        Get the value, loading it if needed.
        Specified by:
        get in interface org.apache.jackrabbit.guava.common.cache.LoadingCache<K,​V>
        Parameters:
        key - the key
        Returns:
        the value
        Throws:
        java.util.concurrent.ExecutionException
      • refresh

        public void refresh​(K key)
        Deprecated.
        Re-load the value for the given key.

        If there is an exception while loading, it is logged and ignored. This method calls CacheLoader.reload, but synchronously replaces the old value.

        Specified by:
        refresh in interface org.apache.jackrabbit.guava.common.cache.LoadingCache<K,​V>
        Parameters:
        key - the key
      • getIfPresent

        @Nullable
        public V getIfPresent​(java.lang.Object key)
        Deprecated.
        Get the value for the given key if the entry is cached. This method adjusts the internal state of the cache sometimes, to ensure commonly used entries stay in the cache.
        Specified by:
        getIfPresent in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
        Parameters:
        key - the key (may not be null)
        Returns:
        the value, or null if there is no resident entry
      • invalidate

        public void invalidate​(java.lang.Object key)
        Deprecated.
        Remove an entry. Both resident and non-resident entries can be removed.
        Specified by:
        invalidate in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
        Parameters:
        key - the key (may not be null)
      • remove

        public V remove​(java.lang.Object key)
        Deprecated.
        Remove an entry. Both resident and non-resident entries can be removed.
        Parameters:
        key - the key (may not be null)
        Returns:
        the old value or null
      • invalidateAll

        public void invalidateAll​(java.lang.Iterable<?> keys)
        Deprecated.
        Specified by:
        invalidateAll in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
      • getMemory

        public int getMemory​(K key)
        Deprecated.
        Get the memory used for the given key.
        Parameters:
        key - the key (may not be null)
        Returns:
        the memory, or 0 if there is no resident entry
      • getUsedMemory

        public long getUsedMemory()
        Deprecated.
        Get the currently used memory.
        Returns:
        the used memory
      • setMaxMemory

        public void setMaxMemory​(long maxMemory)
        Deprecated.
        Set the maximum memory this cache should use. This will not immediately cause entries to get removed however; it will only change the limit. To resize the internal array, call the clear method.
        Parameters:
        maxMemory - the maximum size (1 or larger)
      • setAverageMemory

        public void setAverageMemory​(int averageMemory)
        Deprecated.
        Set the average memory used per entry. It is used to calculate the length of the internal array.
        Parameters:
        averageMemory - the average memory used (1 or larger)
      • getAverageMemory

        public int getAverageMemory()
        Deprecated.
        Get the average memory used per entry.
        Returns:
        the average memory
      • getMaxMemory

        public long getMaxMemory()
        Deprecated.
        Get the maximum memory to use.
        Returns:
        the maximum memory
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Deprecated.
        Get the entry set for all resident entries.
        Returns:
        the entry set
      • keySet

        public java.util.Set<K> keySet()
        Deprecated.
        Get the set of keys for resident entries.
        Returns:
        the set of keys
      • sizeNonResident

        public int sizeNonResident()
        Deprecated.
        Get the number of non-resident entries in the cache.
        Returns:
        the number of non-resident entries
      • sizeMapArray

        public int sizeMapArray()
        Deprecated.
        Get the length of the internal map array.
        Returns:
        the size of the array
      • sizeHot

        public int sizeHot()
        Deprecated.
        Get the number of hot entries in the cache.
        Returns:
        the number of hot entries
      • size

        public long size()
        Deprecated.
        Get the number of resident entries.
        Specified by:
        size in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
        Returns:
        the number of entries
      • keys

        public java.util.List<K> keys​(boolean cold,
                                      boolean nonResident)
        Deprecated.
        Get the list of keys. This method allows to read the internal state of the cache.
        Parameters:
        cold - if true, only keys for the cold entries are returned
        nonResident - true for non-resident entries
        Returns:
        the key list
      • stats

        public org.apache.jackrabbit.guava.common.cache.CacheStats stats()
        Deprecated.
        Specified by:
        stats in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
      • newBuilder

        public static <K,​V> CacheLIRS.Builder<K,​V> newBuilder()
        Deprecated.
        Create a builder.
        Returns:
        the builder
      • getAllPresent

        public org.apache.jackrabbit.guava.common.collect.ImmutableMap<K,​V> getAllPresent​(java.lang.Iterable<?> keys)
        Deprecated.
        Specified by:
        getAllPresent in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
      • asMap

        public java.util.concurrent.ConcurrentMap<K,​V> asMap()
        Deprecated.
        Specified by:
        asMap in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
        Specified by:
        asMap in interface org.apache.jackrabbit.guava.common.cache.LoadingCache<K,​V>
      • cleanUp

        public void cleanUp()
        Deprecated.
        Specified by:
        cleanUp in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Deprecated.
        Specified by:
        putAll in interface org.apache.jackrabbit.guava.common.cache.Cache<K,​V>
      • getAll

        public org.apache.jackrabbit.guava.common.collect.ImmutableMap<K,​V> getAll​(java.lang.Iterable<? extends K> keys)
                                                                                  throws java.util.concurrent.ExecutionException
        Deprecated.
        Specified by:
        getAll in interface org.apache.jackrabbit.guava.common.cache.LoadingCache<K,​V>
        Throws:
        java.util.concurrent.ExecutionException
      • apply

        public V apply​(K key)
        Deprecated.
        Specified by:
        apply in interface java.util.function.Function<K,​V>
        Specified by:
        apply in interface org.apache.jackrabbit.guava.common.base.Function<K,​V>
        Specified by:
        apply in interface org.apache.jackrabbit.guava.common.cache.LoadingCache<K,​V>
      • isEmpty

        public boolean isEmpty()
        Deprecated.