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

Source

This clear()

Remove all key/value pairs from the map. Return this.

containsKey

Source

Bool containsKey(File key)

Returns true if the map contains the given file

def

Source

const Obj? def := null

The default value to use for get when a key isn't mapped.

each

Source

Void each(|Obj?,File c)

Call the specified function for every key/value in the map.

get

Source

@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. If def is omitted it defaults to null.

getOrAdd

Source

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

Source

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

Source

Bool isEmpty()

Return true if size() == 0

isModified

Source

Bool isModified(File key)

Returns true if a subsequent call to getOrAddOrUpdate() would result in the valFunc being executed. This method does not modify any state.

keys

Source

Obj[] keys()

Returns a list of all the mapped keys.

lock

Source

const Synchronized lock

The lock object should you need to synchronize on the file map.

make

Source

new make(ActorPool actorPool, Duration? timeout := 30sec, |This? f := null)

Creates a SynchronizedMap with the given ActorPool and timeout.

remove

Source

Obj? remove(File key)

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

Source

Obj:Obj? rw()

Get a read-write, mutable Map instance with the same contents.

set

Source

@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

Source

Int size()

Get the number of key/value pairs in the map.

timeout

Source

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

Source

File:Obj? val { private set }

Gets or sets a read-only copy of the backing map.

valType

Source

const Type valType := sys::Obj?#

Used to parameterize the backing map.

vals

Source

Obj?[] vals()

Returns a list of all the mapped values.