net.tomp2p.utils
Class ConcurrentCacheMap<K,V>

java.lang.Object
  extended by net.tomp2p.utils.ConcurrentCacheMap<K,V>
Type Parameters:
K - the type of the key
V - the type of the value
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 Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
static int DEFAULT_TIME_TO_LIVE
          Time to live for a value.
static int MAX_ENTRIES
          Max.
static int SEGMENT_NR
          Number of segments that can be accessed concurrently
 
Constructor Summary
ConcurrentCacheMap()
          Creates a new instance of ConcurrentCacheMap using the supplied values and a CacheMap for the internal data structure.
ConcurrentCacheMap(int timeToLive, int maxEntries)
          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.
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set<Map.Entry<K,V>> entrySet()
           
 int expiredCounter()
           
 V get(Object key)
           
 int hashCode()
           
 boolean isEmpty()
           
 Set<K> keySet()
           
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> inMap)
           
 V putIfAbsent(K key, V value)
           
 V remove(Object key)
           
 boolean remove(Object key, Object value)
           
 V replace(K key, V value)
           
 boolean replace(K key, V oldValue, V newValue)
           
 int size()
           
 Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals
 

Field Detail

SEGMENT_NR

public static final int SEGMENT_NR
Number of segments that can be accessed concurrently

See Also:
Constant Field Values

MAX_ENTRIES

public static final int MAX_ENTRIES
Max. number of entries that the map can hold until the least recently used gets replaced

See Also:
Constant Field Values

DEFAULT_TIME_TO_LIVE

public static final int DEFAULT_TIME_TO_LIVE
Time to live for a value. The value may stay longer in the map, but it is considered invalid.

See Also:
Constant Field Values
Constructor Detail

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,
                          int maxEntries)
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 - Set the maximum number of entries until items gets replaced with LRU

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 1024
refreshTimeout - If set to true, timeout will be reset in case of putIfAbsent(Object, Object)
Method Detail

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

putIfAbsent

public V putIfAbsent(K key,
                     V value)
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>

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 © 2013. All Rights Reserved.