Package org.jgroups.blocks
Class Cache<K,V>
- java.lang.Object
-
- org.jgroups.blocks.Cache<K,V>
-
public class Cache<K,V> extends java.lang.ObjectSimple cache which maintains keys and value. A reaper can be enabled which periodically evicts expired entries. Also, when the cache is configured to be bounded, entries in excess of the max size will be evicted on put().
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCache.ChangeListenerprivate classCache.Reaperstatic classCache.Value<V>
-
Field Summary
Fields Modifier and Type Field Description private java.util.Set<Cache.ChangeListener>change_listenersprivate java.util.concurrent.atomic.AtomicBooleanis_reapingprivate static Loglogprivate java.util.concurrent.ConcurrentMap<K,Cache.Value<V>>mapprivate intmax_num_entriesThe maximum number of keys, When this value is exceeded we evict older entries, until we drop below this mark again.private java.util.concurrent.Futuretaskprivate java.util.concurrent.ScheduledThreadPoolExecutortimer
-
Constructor Summary
Constructors Constructor Description Cache()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddChangeListener(Cache.ChangeListener l)voiddisableReaping()java.lang.Stringdump()voidenableReaping(long interval)Runs the reaper every interval ms, evicts expired itemsjava.util.Set<java.util.Map.Entry<K,Cache.Value<V>>>entrySet()private voidevict()Vget(K key)Cache.Value<V>getEntry(K key)java.util.concurrent.ConcurrentMap<K,Cache.Value<V>>getInternalMap()This method should not be used to add or remove elements ! It was just added because ReplCacheDemo requires it for its data modelintgetMaxNumberOfEntries()intgetSize()private VgetValue(Cache.Value<V> val)private booleanisExpired(Cache.Value<V> val)booleanisReapingEnabled()private voidnotifyChangeListeners()Vput(K key, V val, long caching_time)Vremove(K key)voidremoveChangeListener(Cache.ChangeListener l)voidsetMaxNumberOfEntries(int max_num_entries)voidstart()voidstop()java.lang.StringtoString()
-
-
-
Field Detail
-
log
private static final Log log
-
map
private final java.util.concurrent.ConcurrentMap<K,Cache.Value<V>> map
-
timer
private java.util.concurrent.ScheduledThreadPoolExecutor timer
-
task
private java.util.concurrent.Future task
-
is_reaping
private final java.util.concurrent.atomic.AtomicBoolean is_reaping
-
change_listeners
private final java.util.Set<Cache.ChangeListener> change_listeners
-
max_num_entries
private int max_num_entries
The maximum number of keys, When this value is exceeded we evict older entries, until we drop below this mark again. This effectively maintains a bounded cache. A value of 0 means don't bound the cache.
-
-
Method Detail
-
getMaxNumberOfEntries
public int getMaxNumberOfEntries()
-
setMaxNumberOfEntries
public void setMaxNumberOfEntries(int max_num_entries)
-
addChangeListener
public void addChangeListener(Cache.ChangeListener l)
-
removeChangeListener
public void removeChangeListener(Cache.ChangeListener l)
-
getSize
public int getSize()
-
isReapingEnabled
public boolean isReapingEnabled()
-
enableReaping
public void enableReaping(long interval)
Runs the reaper every interval ms, evicts expired items
-
disableReaping
public void disableReaping()
-
start
public void start()
-
stop
public void stop()
-
put
public V put(K key, V val, long caching_time)
- Parameters:
key-val-caching_time- Number of milliseconds to keep an entry in the cache. -1 means don't cache (if reaping is enabled, we'll evict an entry with -1 caching time), 0 means never evict. In the latter case, we can still evict an entry with 0 caching time: when we have a bounded cache, we evict in order of insertion no matter what the caching time is.
-
getInternalMap
public java.util.concurrent.ConcurrentMap<K,Cache.Value<V>> getInternalMap()
This method should not be used to add or remove elements ! It was just added because ReplCacheDemo requires it for its data model- Returns:
-
getEntry
public Cache.Value<V> getEntry(K key)
-
entrySet
public java.util.Set<java.util.Map.Entry<K,Cache.Value<V>>> entrySet()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
dump
public java.lang.String dump()
-
evict
private void evict()
-
notifyChangeListeners
private void notifyChangeListeners()
-
getValue
private V getValue(Cache.Value<V> val)
-
isExpired
private boolean isExpired(Cache.Value<V> val)
-
-