Class ClockMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
org.apache.cayenne.util.ClockMap<K,V>
- All Implemented Interfaces:
ConcurrentMap<K,V>, Map<K, V>
A bounded concurrent map that evicts the entries least recently read. It is a
ConcurrentHashMap with the
CLOCK ("second chance") replacement policy on top: each entry carries a "referenced" bit that a read sets, and once
the map outgrows its maximum size, a sweep walks the entries, clearing the bit of those that were read since its last
pass and removing those that were not. The policy approximates LRU, with a read costing one hash lookup and, at most
once per sweep revolution, one bit write: no allocation, no queue and no linked list is touched on a read.
The sweep runs on the thread that inserts past the limit and is budgeted, so that no single insert pays for a whole revolution. The map may therefore briefly hold a few entries more than its maximum size. A single sweep runs at a time; inserts that find it running skip it, and the next insert catches up.
Iteration views are weakly consistent, like those of ConcurrentHashMap, and do not count as reads.
- Since:
- 5.0
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) entrySet()booleanisEmpty()keySet()Atomic, likeConcurrentHashMap.merge(Object, Object, BiFunction): the remapping function runs under the lock of the key's hash bin and must be short and free of side effects on this map.putIfAbsent(K key, V value) booleanbooleanintsize()Methods inherited from class AbstractMap
clone, containsValue, equals, hashCode, putAll, toString, valuesMethods inherited from interface ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, replaceAll
-
Constructor Details
-
ClockMap
public ClockMap(int maxSize) - Parameters:
maxSize- the number of entries past which inserts evict. Zero or less keeps the map empty.
-
-
Method Details
-
get
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
put
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceConcurrentMap<K,V> - Specified by:
putIfAbsentin interfaceMap<K,V>
-
remove
-
remove
-
replace
-
replace
-
merge
Atomic, likeConcurrentHashMap.merge(Object, Object, BiFunction): the remapping function runs under the lock of the key's hash bin and must be short and free of side effects on this map. -
size
-
isEmpty
-
clear
-
keySet
-
entrySet
-