Class NCCache

  • All Implemented Interfaces:

    
    public class NCCache<K, V>
    
                        

    Modify by DragonJ on 14/11/28. Add queue for change the way of recycle.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      NCCache(int maxSize)
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      void resize(int maxSize) Sets the size of the cache.
      final V get(K key) Returns the value for key if it exists in the cache or can be created by #create.
      final V put(K key, V value) Caches value for key.
      final V remove(K key) Removes the entry for key if it exists.
      void clear()
      void entryRemoved(boolean evicted, K key, V oldValue, V newValue) Called for entries that have been evicted or removed.
      V create(K key) Called after a cache miss to compute a value for the corresponding key.
      int sizeOf(K key, V value) Returns the size of the entry for key and value in user-defined units.
      final void evictAll() Clear the cache, calling entryRemoved on each removed entry.
      final synchronized int size() For caches that do not override sizeOf, this returns the number of entries in the cache.
      final synchronized int maxSize() For caches that do not override sizeOf, this returns the maximum number of entries in the cache.
      final synchronized int hitCount() Returns the number of times get returned a value that was already present in the cache.
      final synchronized int missCount() Returns the number of times get returned null or required a new value to be created.
      final synchronized int createCount() Returns the number of times create returned a value.
      final synchronized int putCount() Returns the number of times put was called.
      final synchronized int evictionCount() Returns the number of values that have been evicted.
      final synchronized Map<K, V> snapshot() Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.
      final synchronized String toString()
      • Methods inherited from class java.lang.Object

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

      • NCCache

        NCCache(int maxSize)
        Parameters:
        maxSize - for caches that do not override sizeOf, this is the maximum number of entries in the cache.
    • Method Detail

      • resize

         void resize(int maxSize)

        Sets the size of the cache.

        Parameters:
        maxSize - The new maximum size.
      • get

         final V get(K key)

        Returns the value for key if it exists in the cache or can be created by #create. If a value was returned, it is moved to the head of the queue. This returns null if a value is not cached and cannot be created.

      • put

         final V put(K key, V value)

        Caches value for key. The value is moved to the head of the queue.

        Returns:

        the previous value mapped by key.

      • remove

         final V remove(K key)

        Removes the entry for key if it exists.

        Returns:

        the previous value mapped by key.

      • entryRemoved

         void entryRemoved(boolean evicted, K key, V oldValue, V newValue)

        Called for entries that have been evicted or removed. This method is invoked when a value is evicted to make space, removed by a call to remove, or replaced by a call to put. The default implementation does nothing.

        The method is called without synchronization: other threads may access the cache while this method is executing.

        Parameters:
        evicted - true if the entry is being removed to make space, false if the removal was caused by a put or remove.
        newValue - the new value for key, if it exists.
      • create

         V create(K key)

        Called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.

        The method is called without synchronization: other threads may access the cache while this method is executing.

        If a value for key exists in the cache when this method returns, the created value will be released with entryRemoved and discarded. This can occur when multiple threads request the same key at the same time (causing multiple values to be created), or when one thread calls put while another is creating a value for the same key.

      • sizeOf

         int sizeOf(K key, V value)

        Returns the size of the entry for key and value in user-defined units. The default implementation returns 1 so that size is the number of entries and max size is the maximum number of entries.

        An entry's size must not change while it is in the cache.

      • size

         final synchronized int size()

        For caches that do not override sizeOf, this returns the number of entries in the cache. For all other caches, this returns the sum of the sizes of the entries in this cache.

      • maxSize

         final synchronized int maxSize()

        For caches that do not override sizeOf, this returns the maximum number of entries in the cache. For all other caches, this returns the maximum sum of the sizes of the entries in this cache.

      • hitCount

         final synchronized int hitCount()

        Returns the number of times get returned a value that was already present in the cache.

      • missCount

         final synchronized int missCount()

        Returns the number of times get returned null or required a new value to be created.

      • createCount

         final synchronized int createCount()

        Returns the number of times create returned a value.

      • putCount

         final synchronized int putCount()

        Returns the number of times put was called.

      • evictionCount

         final synchronized int evictionCount()

        Returns the number of values that have been evicted.

      • snapshot

         final synchronized Map<K, V> snapshot()

        Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.