tech.v3.datatype.locker

Threadsafe access to a key-value map. Unlike atoms and swap! your update/initialization functions are never ran in parallel which helps if, for instance, initialization is a long running, memory intensive operation there will only be one running even if multiple threads request a value simultaneously.

user> (require '[tech.v3.datatype.locker :as locker])
nil
user> (locker/update! ::latest (fn [k v]
                                 (println (format "k is %s v is %s" k v))
                                 ::updated))
k is :user/latest v is null
:user/updated
user> (locker/update! ::latest (fn [k v]
                                 (println (format "k is %s v is %s" k v))
                                 ::updated))
k is :user/latest v is :user/updated
:user/updated

compute-if-absent!

(compute-if-absent! key init-fn)

Compute a new value if non exists for this key. Key cannot be nil. init-fn will only ever be called once and is passed the key as its first argument.

compute-if-present!

(compute-if-present! key update-fn)

Compute a new value if one already exists for this key. Key cannot be nil. update-fn is passed the key and the preexisting value.

exists?

(exists? key)

locker

(locker)

locker*

remove!

(remove! key)

Remove a key - returns the value if any that existed in that location.

update!

(update! key update-fn)

Update a value. update-fn gets passed the key and the value already present (if any).

value

(value key missing-value)(value key)