const classafIoc::ConcurrentCache
sys::Obj afIoc::ConcurrentCache
A helper class that wraps a Map
providing fast reads and synchronised writes betweeen threads. It's an application of ConcurrentState for use when reads far out number the writes.
The cache wraps a map stored in an AtomicRef through which all reads are made. All writes are made via ConcurrentState ensuring synchronised access. Writing makes a rw
copy of the map and is thus a more expensive operation.
Note that all objects held in the map have to be immutable.
@since 1.4.2
- clear
This clear()
Remove all key/value pairs from the map. Return this.
- containsKey
Returns
true
if the cache contains the given key- get
@
Operator
Obj? get(Obj key, Obj? def := null)Returns the value associated with the given key. If key is not mapped, then return the value of the
def
parameter. Ifdef
is omitted it defaults tonull
.- getOrAdd
Obj? getOrAdd(Obj key, |->Obj? valFunc)
Returns the value associated with the given key. If it doesn't exist then it is added from the value function.
This method is NOT thread safe. If two actors call this method at the same time, the value function could be called twice for the same key. @since 1.4.6
- isEmpty
Bool isEmpty()
Return
true
if size() == 0- keys
Obj[] keys()
Returns a list of all the mapped keys.
- make
new make(|This? f := null)
- makeWithMap
Make a
ConcurrentCache
using the given immutable map. Use when you need a case insensitive map. @since 1.4.6- map
A read-only copy of the cache map.
- remove
Remove the key/value pair identified by the specified key from the map and return the value. If the key was not mapped then return
null
.- replace
Obj:Obj? replace(Obj:Obj? newMap)
Replaces the entire content of the cache with the given map. The existing map is returned.
- set
@
Operator
Void set(Obj key, Obj val)Sets the key / value pair, ensuring no data is lost during multi-threaded race conditions. Though the same key may be overridden. Both the
key
andval
must be immutable.- size
Int size()
Get the number of key/value pairs in the map.
- vals
Obj[] vals()
Returns a list of all the mapped values.