Class WeakCARCacheImpl<K,​V>

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

    public class WeakCARCacheImpl<K,​V>
    extends java.lang.Object
    implements WeakCARCache<K,​V>
    Implements the CAR algorithm as found here: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.105.6057
    • Constructor Summary

      Constructors 
      Constructor Description
      WeakCARCacheImpl​(Computable<K,​V> computable, int maxSize, boolean isWeak)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the current cache, making the current size zero
      void clearStaleReferences()
      Causes stale references to be cleared from the data structures.
      V compute​(K key)
      The method used to get or add values to this cache
      java.lang.String dumpAllLists()
      Returns a string that will contain all the elements of the four lists
      int getB1Size()
      Returns the number of items in the B1 LRU
      int getB2Size()
      Returns the number of items in the B2 LRU
      Computable<K,​V> getComputable()
      The computable associated with this cache
      double getHitRate()
      Returns the hit rate from the last time clear was called
      int getKeySize()
      Returns the current number of keys in the cache.
      int getMaxSize()
      Gets the current maximum size of the cache (the maximum number of values that will be kept by the cache).
      int getP()
      Returns the value of p from the CAR algorithm, which is the target size of the t1 clock
      int getT1Size()
      Returns the number of items in the T1 clock
      int getT2Size()
      Returns the number of items in the T2 clock
      private V getValueFromT​(K key)  
      int getValueSize()
      Returns the current number of values in the cache.
      void releaseMatching​(CacheKeyFilter<K> filter)
      Releases all key/value pairs that match the filter
      boolean remove​(K key)
      Used to remove a single key and value from the cache (if the value is available)
      private void replace()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • WeakCARCacheImpl

        public WeakCARCacheImpl​(Computable<K,​V> computable,
                                int maxSize,
                                boolean isWeak)
    • Method Detail

      • getValueFromT

        private V getValueFromT​(K key)
      • compute

        public V compute​(K key)
        Description copied from interface: WeakCARCache
        The method used to get or add values to this cache
        Specified by:
        compute in interface WeakCARCache<K,​V>
        Parameters:
        key - The key to add to the cache. If the value is not found, then the computable will be called to get the value. May not be null
        Returns:
        The calculated return value. May not be null
      • replace

        private void replace()
      • getKeySize

        public int getKeySize()
        Description copied from interface: WeakCARCache
        Returns the current number of keys in the cache. Note that the number of keys can be up to 2x the maximum size of the cache
        Specified by:
        getKeySize in interface WeakCARCache<K,​V>
        Returns:
        The current number of key entries in the cache
      • getValueSize

        public int getValueSize()
        Description copied from interface: WeakCARCache
        Returns the current number of values in the cache. Note that the number of values can be up the maximum size of the cache
        Specified by:
        getValueSize in interface WeakCARCache<K,​V>
        Returns:
        The current number of value entries in the cache
      • clear

        public void clear()
        Description copied from interface: WeakCARCache
        Clears the current cache, making the current size zero
        Specified by:
        clear in interface WeakCARCache<K,​V>
      • getMaxSize

        public int getMaxSize()
        Description copied from interface: WeakCARCache
        Gets the current maximum size of the cache (the maximum number of values that will be kept by the cache). Note that the number of keys kept will be 2x, where x is the maximum size of the cache (see CAR algorithm which keeps a key history)
        Specified by:
        getMaxSize in interface WeakCARCache<K,​V>
        Returns:
        The maximum size of the cache
      • getComputable

        public Computable<K,​V> getComputable()
        Description copied from interface: WeakCARCache
        The computable associated with this cache
        Specified by:
        getComputable in interface WeakCARCache<K,​V>
        Returns:
        The computable associated with this cache
      • remove

        public boolean remove​(K key)
        Description copied from interface: WeakCARCache
        Used to remove a single key and value from the cache (if the value is available)
        Specified by:
        remove in interface WeakCARCache<K,​V>
        Parameters:
        key - The key to remove. May not be null
        Returns:
        true if a key was found and removed
      • releaseMatching

        public void releaseMatching​(CacheKeyFilter<K> filter)
        Description copied from interface: WeakCARCache
        Releases all key/value pairs that match the filter
        Specified by:
        releaseMatching in interface WeakCARCache<K,​V>
        Parameters:
        filter - A non-null filter that can be used to delete every key/value pair that matches the filter
      • clearStaleReferences

        public void clearStaleReferences()
        Description copied from interface: WeakCARCache
        Causes stale references to be cleared from the data structures. Since this is a weak cache the references can go away at any time, which happens whenever any operation has been performed. However, it may be the case that no operation will be performed for a while and so this method is provided to have a no-op operation to call in order to clear out any stale references
        Specified by:
        clearStaleReferences in interface WeakCARCache<K,​V>
      • getT1Size

        public int getT1Size()
        Description copied from interface: WeakCARCache
        Returns the number of items in the T1 clock
        Specified by:
        getT1Size in interface WeakCARCache<K,​V>
        Returns:
        The current number of items in the T1 clock
      • getT2Size

        public int getT2Size()
        Description copied from interface: WeakCARCache
        Returns the number of items in the T2 clock
        Specified by:
        getT2Size in interface WeakCARCache<K,​V>
        Returns:
        The current number of items in the T2 clock
      • getB1Size

        public int getB1Size()
        Description copied from interface: WeakCARCache
        Returns the number of items in the B1 LRU
        Specified by:
        getB1Size in interface WeakCARCache<K,​V>
        Returns:
        The current number of items in the B1 LRU
      • getB2Size

        public int getB2Size()
        Description copied from interface: WeakCARCache
        Returns the number of items in the B2 LRU
        Specified by:
        getB2Size in interface WeakCARCache<K,​V>
        Returns:
        The current number of items in the B2 LRU
      • getP

        public int getP()
        Description copied from interface: WeakCARCache
        Returns the value of p from the CAR algorithm, which is the target size of the t1 clock
        Specified by:
        getP in interface WeakCARCache<K,​V>
        Returns:
        The current value of P
      • dumpAllLists

        public java.lang.String dumpAllLists()
        Description copied from interface: WeakCARCache
        Returns a string that will contain all the elements of the four lists
        Specified by:
        dumpAllLists in interface WeakCARCache<K,​V>
        Returns:
        A String containing the values of T1, T2, B1 and B2
      • getHitRate

        public double getHitRate()
        Description copied from interface: WeakCARCache
        Returns the hit rate from the last time clear was called
        Specified by:
        getHitRate in interface WeakCARCache<K,​V>
        Returns:
        The Hit rate from the last time clear was called or 0 if there is no data
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object