Class LRUCacheImpl<K,​V extends Closeable>

  • Type Parameters:
    K - key
    V - value
    All Implemented Interfaces:
    ILRUCache<K,​V>

    public class LRUCacheImpl<K,​V extends Closeable>
    extends Object
    implements ILRUCache<K,​V>
    Simple and thread-safe LRU cache implementation, supporting time to live and reference counting for entry. in current implementation, entry expiration and purge(mark and sweep) is triggered by put operation, and resource closing after mark and sweep is done in async way.
    Author:
    bulldog
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static long DEFAULT_TTL  
    • Constructor Summary

      Constructors 
      Constructor Description
      LRUCacheImpl​(Clock clock)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void CloseExecutorService()
      Shutdown the internal ExecutorService, Call this only after you have closed your bigqueue instance.
      V get​(K key)
      Get a cached resource with specific key This call will increment the reference counter of the keyed resource.
      Collection<V> getValues()
      All values cached
      void put​(K key, V value)
      Put a keyed resource with default ttl into the cache This call will increment the reference counter of the keyed resource.
      void put​(K key, V value, long ttlInMilliSeconds)
      Put a keyed resource with specific ttl into the cache This call will increment the reference counter of the keyed resource.
      void release​(K key)
      Release the cached resource with specific key This call will decrement the reference counter of the keyed resource.
      V remove​(K key)
      Remove the resource with specific key from the cache and close it synchronously afterwards.
      void removeAll()
      Remove all cached resource from the cache and close them asynchronously afterwards.
      int size()
      The size of the cache, equals to current total number of cached resources.
    • Constructor Detail

      • LRUCacheImpl

        public LRUCacheImpl​(Clock clock)
    • Method Detail

      • CloseExecutorService

        public static void CloseExecutorService()
        Shutdown the internal ExecutorService, Call this only after you have closed your bigqueue instance.
      • put

        public void put​(K key,
                        V value,
                        long ttlInMilliSeconds)
        Description copied from interface: ILRUCache
        Put a keyed resource with specific ttl into the cache This call will increment the reference counter of the keyed resource.
        Specified by:
        put in interface ILRUCache<K,​V extends Closeable>
        Parameters:
        key - the key of the cached resource
        value - the to be cached resource
        ttlInMilliSeconds - time to live in milliseconds
      • put

        public void put​(K key,
                        V value)
        Description copied from interface: ILRUCache
        Put a keyed resource with default ttl into the cache This call will increment the reference counter of the keyed resource.
        Specified by:
        put in interface ILRUCache<K,​V extends Closeable>
        Parameters:
        key - the key of the cached resource
        value - the to be cached resource
      • get

        public V get​(K key)
        Description copied from interface: ILRUCache
        Get a cached resource with specific key This call will increment the reference counter of the keyed resource.
        Specified by:
        get in interface ILRUCache<K,​V extends Closeable>
        Parameters:
        key - the key of the cached resource
        Returns:
        cached resource if exists
      • release

        public void release​(K key)
        Description copied from interface: ILRUCache
        Release the cached resource with specific key This call will decrement the reference counter of the keyed resource.
        Specified by:
        release in interface ILRUCache<K,​V extends Closeable>
        Parameters:
        key - key of resource to release
      • size

        public int size()
        Description copied from interface: ILRUCache
        The size of the cache, equals to current total number of cached resources.
        Specified by:
        size in interface ILRUCache<K,​V extends Closeable>
        Returns:
        the size of the cache
      • removeAll

        public void removeAll()
                       throws IOException
        Description copied from interface: ILRUCache
        Remove all cached resource from the cache and close them asynchronously afterwards.
        Specified by:
        removeAll in interface ILRUCache<K,​V extends Closeable>
        Throws:
        IOException - exception thrown if there is any IO error
      • remove

        public V remove​(K key)
                 throws IOException
        Description copied from interface: ILRUCache
        Remove the resource with specific key from the cache and close it synchronously afterwards.
        Specified by:
        remove in interface ILRUCache<K,​V extends Closeable>
        Parameters:
        key - the key of the cached resource
        Returns:
        the removed resource if exists
        Throws:
        IOException - exception thrown if there is any IO error