Package de.gsi.dataset.utils
Class Cache<K,V>
- java.lang.Object
-
- de.gsi.dataset.utils.Cache<K,V>
-
- Type Parameters:
K- search keyV- 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:
Original code courtesy from: https://github.com/HanSolo/cachepublic 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(); } }- Author:
- Gerrit Grunwald (aka. HanSolo, original concept), rstein
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCache.CacheBuilder<K2,V2>
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <K3,V3>
Cache.CacheBuilder<K3,V3>builder()protected voidcheckSize()protected voidcheckSize(int nNewElements)protected voidcheckTime()protected static intclamp(int min, int max, int value)protected static longclamp(long min, long max, long value)voidclear()booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)protected static java.time.temporal.ChronoUnitconvertToChronoUnit(java.util.concurrent.TimeUnit timeUnit)java.util.Set<java.util.Map.Entry<K,V>>entrySet()Vget(java.lang.Object key)VgetIfPresent(K key)longgetLimit()java.util.Optional<V>getOptional(K key)intgetSize()longgetTimeout()java.util.concurrent.TimeUnitgetTimeUnit()booleanisEmpty()java.util.Set<K>keySet()Vput(K key, V value)voidputAll(java.util.Map<? extends K,? extends V> m)VputIfAbsent(K key, V value)Vremove(java.lang.Object key)intsize()java.util.Collection<V>values()
-
-
-
Method Detail
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
getLimit
public long getLimit()
-
getSize
public int getSize()
-
getTimeout
public long getTimeout()
-
getTimeUnit
public java.util.concurrent.TimeUnit getTimeUnit()
-
values
public java.util.Collection<V> values()
-
checkSize
protected void checkSize()
-
checkSize
protected void checkSize(int nNewElements)
-
checkTime
protected void checkTime()
-
builder
public static <K3,V3> Cache.CacheBuilder<K3,V3> builder()
-
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)
-
-