const classafConcurrent::SynchronizedFileMap
sys::Obj afConcurrent::SynchronizedFileMap
A Synchronized
cache whose values update should their associated file key be modified. Values are updated upon calling getOrAddOrUpdate()
- they do not update themselves autonomously!
To prevent excessive polling of the file system, and given every call to File.exists()
typically takes at least 8ms-12ms, you can set a timeout Duration
. Every file in the cache notes when it polled the file system last, and waits at least this amount of time before polling again.
Note that values are only added to the cache should the file actually exist. Given there are an infinite number of files that don't exist, this prevents your cache from growing unbounded. (Also, how do you tell if a file has been modified if it doesn't exist!?)
Upon calling set()
, getOrAdd()
or getOrAddOrUpdate()
should a file not exist (or has subsequently been deleted) then the value function is still executed and its result returned. However nothing is stored in the cache, and any previous value keyed against the file is removed. This ensures that the content of the cache only relates to files that exist.
Although surprising, the above behaviour is usually what you want and works very well.
Note that all objects held in the cache have to be immutable.
- clear
This clear()
Remove all key/value pairs from the map. Return this.
- containsKey
Returns
true
if the map contains the given file- def
const Obj? def := null
The default value to use for get when a key isn't mapped.
- each
Call the specified function for every key/value in the map.
- get
@
Operator
Obj? get(File key, Obj? def := this.def)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(File key, |File->Obj? valFunc)
Returns the value associated with the given key. If the key is not mapped then it is added from the value function.
If the file does not exist, the
valFunc
is executed but nothing is added to the cache.Note that
valFunc
should be immutable and, if used, is executed in a different thread to the calling thread.- getOrAddOrUpdate
Obj? getOrAddOrUpdate(File key, |File->Obj? valFunc)
Returns the value associated with the given file. If it doesn't exist, or the file has been modified since the last call to
getOrAddOrUpdate()
, then it is added from the given value function.Set
timeout
in the ctor to avoid hitting the file system on every call to this method.If the file does not exist, the
valFunc
is executed but nothing is added to the cache.Note that
valFunc
should be immutable and, if used, is executed in a different thread to the calling thread.- isEmpty
Bool isEmpty()
Return
true
if size() == 0- isModified
Returns
true
if a subsequent call togetOrAddOrUpdate()
would result in thevalFunc
being executed. This method does not modify any state.- keys
Obj[] keys()
Returns a list of all the mapped keys.
- lock
const Synchronized lock
The
lock
object should you need tosynchronize
on the file map.- make
new make(ActorPool actorPool, Duration? timeout := 30sec, |This? f := null)
Creates a
SynchronizedMap
with the givenActorPool
andtimeout
.- 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
.- rw
Get a read-write, mutable Map instance with the same contents.
- set
@
Operator
Void set(File key, Obj? item)Sets the key / value pair, ensuring no data is lost during multi-threaded race conditions.
Nothing is added should the file not exist.
- size
Int size()
Get the number of key/value pairs in the map.
- timeout
const Duration? timeout
The duration between individual file checks. Use to avoid excessive reads of the file system. Set to
null
to check the file every time.- val
Gets or sets a read-only copy of the backing map.
- valType
const Type valType := sys::Obj?#
Used to parameterize the backing map.
- vals
Obj?[] vals()
Returns a list of all the mapped values.