Class SynchronizedLruCache<K,​V>

  • Type Parameters:
    K - Key
    V - Value
    All Implemented Interfaces:
    InterfaceLruCache<K,​V>

    public class SynchronizedLruCache<K,​V>
    extends java.lang.Object
    Threadsafe synchronized implementation of LruCache based on LinkedHashMap. Threadsafety is provided by method synchronization. This cache implementation should be used with low number of threads. author Damian Momot
    • Constructor Summary

      Constructors 
      Constructor Description
      SynchronizedLruCache​(int capacity, long ttl)
      Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR and DEFAULT_INITIAL_CAPACITY
      SynchronizedLruCache​(int capacity, long ttl, int initialCapacity)
      Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR
      SynchronizedLruCache​(int capacity, long ttl, int initialCapacity, float loadFactor)
      Creates new SynchronizedLruCache
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all entries from cache
      boolean contains​(K key)
      Checks whether cache contains valid entry for key
      protected InterfaceLruCacheEntry<V> createEntry​(V value, long ttl)
      Creates new LruCacheEntry.
      int forceClearOldest()
      Removes all oldest entries from cache (ttl based)
      V get​(K key)
      Returns value cached with key.
      V get​(K key, java.util.concurrent.Callable<V> callback)
      Tries to get element from cache.
      V get​(K key, java.util.concurrent.Callable<V> callback, long ttl)
      Tries to get element from cache.
      int getCapacity()
      Returns cache capacity
      protected InterfaceLruCacheEntry<V> getEntry​(K key)
      Returns LruCacheEntry mapped by key or null if it does not exist
      long getTtl()
      Returns cache TTL
      protected V getValue​(K key)
      Tries to retrieve value by it's key.
      boolean isEmpty()
      Checks whether cache is empty.
      void put​(K key, V value)
      Puts value under key into cache.
      void put​(K key, V value, long ttl)
      Puts value under key into cache with desired TTL
      protected void putEntry​(K key, InterfaceLruCacheEntry<V> entry)
      Puts entry into cache
      V remove​(K key)
      Removes entry from cache (if exists)
      void setNewTtl​(long ttl)
      Set a new TTL (for newly set objects only, not changing old values).
      int size()
      Returns number of entries stored in cache (including invalid ones)
      void updateTtl​(K key)
      Update the TTL of the associated object if it still exists
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_INITIAL_CAPACITY

        public static final int DEFAULT_INITIAL_CAPACITY
        Initial capacity
        See Also:
        Constant Field Values
      • DEFAULT_LOAD_FACTOR

        public static final float DEFAULT_LOAD_FACTOR
        Load factor
        See Also:
        Constant Field Values
    • Constructor Detail

      • SynchronizedLruCache

        public SynchronizedLruCache​(int capacity,
                                    long ttl,
                                    int initialCapacity,
                                    float loadFactor)
        Creates new SynchronizedLruCache
        Parameters:
        capacity - max cache capacity
        ttl - time to live in milliseconds
        initialCapacity - initial cache capacity
        loadFactor -
      • SynchronizedLruCache

        public SynchronizedLruCache​(int capacity,
                                    long ttl,
                                    int initialCapacity)
        Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR
        Parameters:
        capacity - max cache capacity
        ttl - time to live in milliseconds
        initialCapacity - initial cache capacity
      • SynchronizedLruCache

        public SynchronizedLruCache​(int capacity,
                                    long ttl)
        Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR and DEFAULT_INITIAL_CAPACITY
        Parameters:
        capacity - max cache capacity
        ttl - time to live in milliseconds
    • Method Detail

      • clear

        public void clear()
        Description copied from interface: InterfaceLruCache
        Removes all entries from cache
      • get

        public V get​(K key)
        Description copied from interface: InterfaceLruCache
        Returns value cached with key.
        Specified by:
        get in interface InterfaceLruCache<K,​V>
        Returns:
        value or null if key doesn't exist or entry is not valid
      • getCapacity

        public int getCapacity()
        Description copied from interface: InterfaceLruCache
        Returns cache capacity
        Returns:
        capacity of cache
      • getEntry

        protected InterfaceLruCacheEntry<V> getEntry​(K key)
        Returns LruCacheEntry mapped by key or null if it does not exist
        Returns:
        LruCacheEntry
      • size

        public int size()
        Description copied from interface: InterfaceLruCache
        Returns number of entries stored in cache (including invalid ones)
        Returns:
        number of entries
      • put

        public void put​(K key,
                        V value,
                        long ttl)
        Description copied from interface: InterfaceLruCache
        Puts value under key into cache with desired TTL
        Specified by:
        put in interface InterfaceLruCache<K,​V>
        ttl - time to live in milliseconds
      • remove

        public V remove​(K key)
        Description copied from interface: InterfaceLruCache
        Removes entry from cache (if exists)
        Returns:
        the value if it still exists
      • forceClearOldest

        public int forceClearOldest()
        Description copied from interface: InterfaceLruCache
        Removes all oldest entries from cache (ttl based)
        Returns:
        the number of removed entries
      • contains

        public boolean contains​(K key)
        Description copied from interface: InterfaceLruCache
        Checks whether cache contains valid entry for key
        Specified by:
        contains in interface InterfaceLruCache<K,​V>
        Returns:
        true if cache contains key and entry is valid
      • createEntry

        protected InterfaceLruCacheEntry<V> createEntry​(V value,
                                                        long ttl)
        Creates new LruCacheEntry. It can be used to change implementation of LruCacheEntry
        Parameters:
        value -
        ttl -
        Returns:
        LruCacheEntry
      • get

        public V get​(K key,
                     java.util.concurrent.Callable<V> callback)
              throws VitamException
        Description copied from interface: InterfaceLruCache
        Tries to get element from cache. If get fails callback is used to create element and returned value is stored in cache. Default TTL is used
        Specified by:
        get in interface InterfaceLruCache<K,​V>
        Returns:
        Value
        Throws:
        VitamException
      • get

        public V get​(K key,
                     java.util.concurrent.Callable<V> callback,
                     long ttl)
              throws VitamException
        Description copied from interface: InterfaceLruCache
        Tries to get element from cache. If get fails callback is used to create element and returned value is stored in cache
        Specified by:
        get in interface InterfaceLruCache<K,​V>
        ttl - time to live in milliseconds
        Returns:
        Value
        Throws:
        VitamException
      • setNewTtl

        public void setNewTtl​(long ttl)
        Description copied from interface: InterfaceLruCache
        Set a new TTL (for newly set objects only, not changing old values).
        Specified by:
        setNewTtl in interface InterfaceLruCache<K,​V>
      • updateTtl

        public void updateTtl​(K key)
        Description copied from interface: InterfaceLruCache
        Update the TTL of the associated object if it still exists
        Specified by:
        updateTtl in interface InterfaceLruCache<K,​V>
      • getValue

        protected V getValue​(K key)
        Tries to retrieve value by it's key. Automatically removes entry if it's not valid (LruCacheEntry.getValue() returns null)
        Parameters:
        key -
        Returns:
        Value
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: InterfaceLruCache
        Checks whether cache is empty. If any entry exists (including invalid one) this method will return true
        Specified by:
        isEmpty in interface InterfaceLruCache<K,​V>
        Returns:
        true if no entries are stored in cache
      • put

        public void put​(K key,
                        V value)
        Description copied from interface: InterfaceLruCache
        Puts value under key into cache. Default TTL is used
        Specified by:
        put in interface InterfaceLruCache<K,​V>