Class Cache<K,​V>

  • Type Parameters:
    K - search key
    V - cached value
    All Implemented Interfaces:
    java.util.Map<K,​V>

    public class Cache<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    A simple map based cache with timeOut and limit usage example:
      
         public class Demo {
             private Cache<String, Integer> cache;
    
             public Demo() {
                 cache = final Cache<String, Integer> cache = Cache.<String, Integer>builder().withLimit(10)
                      .withTimeout(100, TimeUnit.MILLISECONDS).build();
                 // alternatively:
                 // cache = new Cache(100, TimeUnit.MILLISECONDS, 10);
    
                 String name1 = "Han Solo";
    
                 cache.put(name1, 10);
    
                 System.out.println(name1 + " is cached: " + isCached(name1));
    
                 // Wait 1 second
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
    
                 System.out.println(name1 + " is cached: " + isCached(name1));
    
                 // Wait another second
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
    
                 System.out.println(name1 + " is cached: " + isCached(name1));
             }
    
             private boolean isCached(final String KEY) {
                 return cache.get(KEY).isPresent();
             }
    
             public static void main(String[] args) {
                 new Demo();
             }
         }
     
     
    Original code courtesy from: https://github.com/HanSolo/cache
    Author:
    Gerrit Grunwald (aka. HanSolo, original concept), rstein
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Cache.CacheBuilder<K2,​V2>  
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      Cache​(int limit)  
      Cache​(long timeOut, java.util.concurrent.TimeUnit timeUnit)  
      Cache​(long timeOut, java.util.concurrent.TimeUnit timeUnit, int limit)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <K3,​V3>
      Cache.CacheBuilder<K3,​V3>
      builder()  
      protected void checkSize()  
      protected void checkSize​(int nNewElements)  
      protected void checkTime()  
      protected static int clamp​(int min, int max, int value)  
      protected static long clamp​(long min, long max, long value)  
      void clear()  
      boolean containsKey​(java.lang.Object key)  
      boolean containsValue​(java.lang.Object value)  
      protected static java.time.temporal.ChronoUnit convertToChronoUnit​(java.util.concurrent.TimeUnit timeUnit)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()  
      V get​(java.lang.Object key)  
      V getIfPresent​(K key)  
      long getLimit()  
      java.util.Optional<V> getOptional​(K key)  
      int getSize()  
      long getTimeout()  
      java.util.concurrent.TimeUnit getTimeUnit()  
      boolean isEmpty()  
      java.util.Set<K> keySet()  
      V put​(K key, V value)  
      void putAll​(java.util.Map<? extends K,​? extends V> m)  
      V putIfAbsent​(K key, V value)  
      V remove​(java.lang.Object key)  
      int size()  
      java.util.Collection<V> values()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, remove, replace, replace, replaceAll
    • Constructor Detail

      • Cache

        public Cache​(int limit)
      • Cache

        public Cache​(long timeOut,
                     java.util.concurrent.TimeUnit timeUnit)
      • Cache

        public Cache​(long timeOut,
                     java.util.concurrent.TimeUnit timeUnit,
                     int limit)
    • Method Detail

      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
      • getIfPresent

        public V getIfPresent​(K key)
      • getLimit

        public long getLimit()
      • getOptional

        public java.util.Optional<V> getOptional​(K key)
      • getSize

        public int getSize()
      • getTimeout

        public long getTimeout()
      • getTimeUnit

        public java.util.concurrent.TimeUnit getTimeUnit()
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
      • putIfAbsent

        public V putIfAbsent​(K key,
                             V value)
        Specified by:
        putIfAbsent in interface java.util.Map<K,​V>
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<K,​V>
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
      • checkSize

        protected void checkSize()
      • checkSize

        protected void checkSize​(int nNewElements)
      • checkTime

        protected void checkTime()
      • clamp

        protected static int clamp​(int min,
                                   int max,
                                   int value)
      • clamp

        protected static long clamp​(long min,
                                    long max,
                                    long value)
      • convertToChronoUnit

        protected static java.time.temporal.ChronoUnit convertToChronoUnit​(java.util.concurrent.TimeUnit timeUnit)