Class LRUHybridCache<K,​V>

  • Type Parameters:
    K - The type for the keys in the cache
    V - The type for the values in the cache
    All Implemented Interfaces:
    Computable<K,​HybridCacheEntry<V>>

    public class LRUHybridCache<K,​V>
    extends Object
    implements Computable<K,​HybridCacheEntry<V>>
    Hybrid cache that allows explicit removals of included entries as well as implicit removal of entries that have been least recently accessed. The implicit removal happens only in case the internal cache runs out of space. Maximum number of items that can be kept in the cache is invariant for a single cache instance, and can be set in the cache constructor. The cache deals with HybridCacheEntry items than could even instruct the cache to drop them as they get computed. The cache will still make sure such items get computed just once in given time (based on computation length) for given key. Desired value will only be computed once and computed value stored in the cache. The implementation is based on an example from the "Java Concurrency in Practice" book authored by Brian Goetz and company.
    Author:
    Jakub Podlesak (jakub.podlesak at oracle.com)
    • Constructor Detail

      • LRUHybridCache

        public LRUHybridCache​(int maxCacheSize,
                              Computable<K,​HybridCacheEntry<V>> computable)
        Create new cache with given computable to compute values.
        Parameters:
        maxCacheSize - The maximum number of entries in the cache
        computable - The thing that can create the entry
      • LRUHybridCache

        public LRUHybridCache​(int maxCacheSize,
                              Computable<K,​HybridCacheEntry<V>> computable,
                              LRUHybridCache.CycleHandler<K> cycleHandler)
        Create new cache with given computable and cycleHandler.
        Parameters:
        maxCacheSize - The maximum number of entries in the cache
        computable - The thing that can create the entry
        cycleHandler - What to do if a cycle is detected
    • Method Detail

      • createCacheEntry

        public HybridCacheEntry<V> createCacheEntry​(K k,
                                                    V v,
                                                    boolean dropMe)
        Create cache entry for given values.
        Parameters:
        k - cache entry key.
        v - cache entry value.
        dropMe - should this entry be kept in the cache this must be set to false.
        Returns:
        an instance of cache entry.
      • compute

        public HybridCacheEntry<V> compute​(K key)
        Description copied from interface: Computable
        Defines an expensive computation to retrieve value V from key K.
        Specified by:
        compute in interface Computable<K,​V>
        Parameters:
        key - input data.
        Returns:
        output from the computation.
      • clear

        public void clear()
        Empty the cache.
      • size

        public int size()
        Return the size of the cache
        Returns:
        The current size of the cache
      • getMaximumCacheSize

        public int getMaximumCacheSize()
      • containsKey

        public boolean containsKey​(K key)
        Returns true if the key has already been cached.
        Parameters:
        key -
        Returns:
        true if given key is present in the cache.
      • remove

        public void remove​(K key)
        Remove given item from the cache.
        Parameters:
        key - item key.
      • releaseMatching

        public void releaseMatching​(CacheKeyFilter<K> filter)
        This method will remove all cache entries for which this filter matches
        Parameters:
        filter - Entries in the cache that match this filter will be removed from the cache. If filter is null nothing will be removed from the cache