Class Cache<K,​V>

  • Direct Known Subclasses:
    TypeSystemAwareCache

    public class Cache<K,​V>
    extends java.lang.Object
    static var MY_CACHE = new Cache( 1000, \ foo -> getBar( foo ) )
    • Field Detail

      • _name

        private final java.lang.String _name
      • _size

        private final int _size
      • _requests

        private final java.util.concurrent.atomic.AtomicInteger _requests
      • _misses

        private final java.util.concurrent.atomic.AtomicInteger _misses
      • _hits

        private final java.util.concurrent.atomic.AtomicInteger _hits
      • _loggingTask

        private java.util.concurrent.ScheduledFuture<?> _loggingTask
    • Constructor Detail

      • Cache

        public Cache​(java.lang.String name,
                     int size,
                     Cache.MissHandler<K,​V> missHandler)
        This will create a new cache
        Parameters:
        name - the name of the cache for logging
        size - the maximum size of the log
        missHandler - how to handle misses, this is required not to be null
    • Method Detail

      • clearCacheImpl

        private void clearCacheImpl()
      • evict

        public V evict​(K key)
        This will evict a specific key from the cache.
        Parameters:
        key - the key to evict
        Returns:
        the current value for that key
      • put

        public V put​(K key,
                     V value)
        This will put a specific entry in the cache
        Parameters:
        key - this is the key
        value - this is the value
        Returns:
        the old value for this key
      • get

        public V get​(K key)
        This will get a specific entry, it will call the missHandler if it is not found.
        Parameters:
        key - the object to find
        Returns:
        the found object (may be null)
      • getConfiguredSize

        public int getConfiguredSize()
      • getUtilizedSize

        public int getUtilizedSize()
      • getRequests

        public int getRequests()
      • getMisses

        public int getMisses()
      • getHits

        public int getHits()
      • getHitRate

        public double getHitRate()
      • logEveryNSeconds

        public Cache<K,​V> logEveryNSeconds​(int seconds,
                                                 ILogger logger)
        Sets up a recurring task every n seconds to report on the status of this cache. This can be useful if you are doing exploratory caching and wish to monitor the performance of this cache with minimal fuss. Consider
        Parameters:
        seconds - how often to log the entry
        logger - the logger to use
        Returns:
        this
      • stopLogging

        public void stopLogging()
      • clear

        public void clear()
      • toString

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

        public static <K,​V> Cache<K,​V> make​(java.lang.String name,
                                                        int size,
                                                        Cache.MissHandler<K,​V> handler)