net.tomp2p.utils
Class ConcurrentCacheMap<K,V>
java.lang.Object
net.tomp2p.utils.ConcurrentCacheMap<K,V>
- All Implemented Interfaces:
- ConcurrentMap<K,V>, Map<K,V>
public class ConcurrentCacheMap<K,V>
- extends Object
- implements ConcurrentMap<K,V>
A map with expiration and more or less LRU. Since the maps are separated in
segments, the LRU is done for each segment. A segment is chosen based on the
hash of the key. If one segments is more loaded than another, then an entry
of the loaded segment may get evicted before an entry used least recently
from an other segment.
The expiration is done best effort. There is no thread checking for timed out
entries since the cache has a fixed size. Once an entry times out, it remains
in the map until it either is accessed or evicted.
A test showed that for the default entry size of 1024, this map has a size of
967 if 1024 items are inserted. This is due to the segmentation and hashing.
- Author:
- Thomas Bocek
| Nested classes/interfaces inherited from interface java.util.Map |
Map.Entry<K,V> |
|
Constructor Summary |
ConcurrentCacheMap()
Creates a new instance of ConcurrentCacheMap using the supplied values
and a CacheMap for the internal data structure. |
ConcurrentCacheMap(int timeToLive)
Creates a new instance of ConcurrentCacheMap using the supplied values
and a CacheMap for the internal data structure. |
ConcurrentCacheMap(int timeToLive,
int maxEntries,
boolean refreshTimeout)
Creates a new instance of ConcurrentCacheMap using the supplied values
and a CacheMap for the internal data structure. |
SEGMENT_NR
public static final int SEGMENT_NR
- See Also:
- Constant Field Values
MAX_ENTRIES
public static final int MAX_ENTRIES
- See Also:
- Constant Field Values
DEFAULT_TIME_TO_LIVE
public static final int DEFAULT_TIME_TO_LIVE
- See Also:
- Constant Field Values
ConcurrentCacheMap
public ConcurrentCacheMap()
- Creates a new instance of ConcurrentCacheMap using the supplied values
and a
CacheMap for the internal data structure.
ConcurrentCacheMap
public ConcurrentCacheMap(int timeToLive)
- Creates a new instance of ConcurrentCacheMap using the supplied values
and a
CacheMap for the internal data structure.
- Parameters:
timeToLive - The time-to-live value (seconds)
ConcurrentCacheMap
public ConcurrentCacheMap(int timeToLive,
int maxEntries,
boolean refreshTimeout)
- Creates a new instance of ConcurrentCacheMap using the supplied values
and a
CacheMap for the internal data structure.
- Parameters:
timeToLive - The time-to-live value (seconds)maxEntries - The maximum entries to keep in cache, default is 1024refreshTimeout - If set to true, timeout will be reset in case of
putIfAbsent(Object, Object)
put
public V put(K key,
V value)
- Specified by:
put in interface Map<K,V>
putIfAbsent
public V putIfAbsent(K key,
V value)
- This does not reset the timer!
- Specified by:
putIfAbsent in interface ConcurrentMap<K,V>
get
public V get(Object key)
- Specified by:
get in interface Map<K,V>
remove
public V remove(Object key)
- Specified by:
remove in interface Map<K,V>
remove
public boolean remove(Object key,
Object value)
- Specified by:
remove in interface ConcurrentMap<K,V>
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey in interface Map<K,V>
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue in interface Map<K,V>
size
public int size()
- Specified by:
size in interface Map<K,V>
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty in interface Map<K,V>
clear
public void clear()
- Specified by:
clear in interface Map<K,V>
hashCode
public int hashCode()
- Specified by:
hashCode in interface Map<K,V>- Overrides:
hashCode in class Object
keySet
public Set<K> keySet()
- Specified by:
keySet in interface Map<K,V>
equals
public boolean equals(Object obj)
- Specified by:
equals in interface Map<K,V>- Overrides:
equals in class Object
putAll
public void putAll(Map<? extends K,? extends V> inMap)
- Specified by:
putAll in interface Map<K,V>
values
public Collection<V> values()
- Specified by:
values in interface Map<K,V>
entrySet
public Set<Map.Entry<K,V>> entrySet()
- Specified by:
entrySet in interface Map<K,V>
replace
public boolean replace(K key,
V oldValue,
V newValue)
- Specified by:
replace in interface ConcurrentMap<K,V>
replace
public V replace(K key,
V value)
- Specified by:
replace in interface ConcurrentMap<K,V>
expiredCounter
public int expiredCounter()
- Returns:
- The number of expired objects
Copyright © 2012. All Rights Reserved.