Package gw.util.concurrent
Class Cache<K,V>
- java.lang.Object
-
- gw.util.concurrent.Cache<K,V>
-
- Direct Known Subclasses:
TypeSystemAwareCache
public class Cache<K,V> extends java.lang.Objectstatic var MY_CACHE = new Cache( 1000, \ foo -> getBar( foo ) )
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classCache.ConcurrentLinkedHashMap<K,V>AConcurrentMapwith a doubly-linked list running through its entries.static interfaceCache.MissHandler<L,W>
-
Field Summary
Fields Modifier and Type Field Description private Cache.ConcurrentLinkedHashMap<K,V>_cacheImplprivate java.util.concurrent.atomic.AtomicInteger_hitsprivate java.util.concurrent.ScheduledFuture<?>_loggingTaskprivate java.util.concurrent.atomic.AtomicInteger_missesprivate Cache.MissHandler<K,V>_missHandlerprivate java.lang.String_nameprivate java.util.concurrent.atomic.AtomicInteger_requestsprivate int_size
-
Constructor Summary
Constructors Constructor Description Cache(java.lang.String name, int size, Cache.MissHandler<K,V> missHandler)This will create a new cache
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()private voidclearCacheImpl()Vevict(K key)This will evict a specific key from the cache.Vget(K key)This will get a specific entry, it will call the missHandler if it is not found.intgetConfiguredSize()doublegetHitRate()intgetHits()intgetMisses()intgetRequests()intgetUtilizedSize()Cache<K,V>logEveryNSeconds(int seconds, ILogger logger)Sets up a recurring task every n seconds to report on the status of this cache.static <K,V>
Cache<K,V>make(java.lang.String name, int size, Cache.MissHandler<K,V> handler)Vput(K key, V value)This will put a specific entry in the cachevoidstopLogging()java.lang.StringtoString()
-
-
-
Field Detail
-
_cacheImpl
private Cache.ConcurrentLinkedHashMap<K,V> _cacheImpl
-
_missHandler
private final Cache.MissHandler<K,V> _missHandler
-
_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 loggingsize- the maximum size of the logmissHandler- 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 keyvalue- 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 entrylogger- the logger to use- Returns:
- this
-
stopLogging
public void stopLogging()
-
clear
public void clear()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
make
public static <K,V> Cache<K,V> make(java.lang.String name, int size, Cache.MissHandler<K,V> handler)
-
-