K - search keyV - cached valuepublic class Cache<K,V> extends Object implements Map<K,V>
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| Modifier and Type | Class and Description |
|---|---|
static class |
Cache.CacheBuilder<K2,V2> |
| Constructor and Description |
|---|
Cache(int limit) |
Cache(long timeOut,
TimeUnit timeUnit) |
Cache(long timeOut,
TimeUnit timeUnit,
int limit) |
| Modifier and Type | Method and 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(Object key) |
boolean |
containsValue(Object value) |
protected static ChronoUnit |
convertToChronoUnit(TimeUnit timeUnit) |
Set<Map.Entry<K,V>> |
entrySet() |
V |
get(Object key) |
V |
getIfPresent(K key) |
long |
getLimit() |
Optional<V> |
getOptional(K key) |
int |
getSize() |
long |
getTimeout() |
TimeUnit |
getTimeUnit() |
boolean |
isEmpty() |
Set<K> |
keySet() |
V |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> m) |
V |
putIfAbsent(K key,
V value) |
V |
remove(Object key) |
int |
size() |
Collection<V> |
values() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, remove, replace, replace, replaceAllpublic Cache(int limit)
public Cache(long timeOut,
TimeUnit timeUnit)
public Cache(long timeOut,
TimeUnit timeUnit,
int limit)
public boolean containsKey(Object key)
containsKey in interface Map<K,V>public boolean containsValue(Object value)
containsValue in interface Map<K,V>public long getLimit()
public int getSize()
public long getTimeout()
public TimeUnit getTimeUnit()
protected void checkSize()
protected void checkSize(int nNewElements)
protected void checkTime()
public static <K3,V3> Cache.CacheBuilder<K3,V3> builder()
protected static int clamp(int min,
int max,
int value)
protected static long clamp(long min,
long max,
long value)
protected static ChronoUnit convertToChronoUnit(TimeUnit timeUnit)
Copyright © 2020 GSI Helmholtzzentrum für Schwerionenforschung GmbH. All rights reserved.