tweaks to hopefully remove any potential concurrency issues
trying to reduce the number of times i de-reference session-atom, as well as reduce the number of bits of data i obtain from it before/after a call to swap! within the same function. was able to cut out a few here which i think should be pretty good. :-)
This commit is contained in:
parent
52dd71f26b
commit
6550dc4450
|
@ -83,15 +83,17 @@
|
||||||
|
|
||||||
SessionStore
|
SessionStore
|
||||||
(read-session [_ key]
|
(read-session [_ key]
|
||||||
(when-let [existing-entry (get @session-atom key)]
|
(when (contains? @session-atom key)
|
||||||
(let [session-map (swap! session-atom process-read-entry ttl key refresh-on-read)]
|
(let [[previous current] (swap-vals! session-atom process-read-entry ttl key refresh-on-read)]
|
||||||
; if the entry we were about to read had expired, session-map will not have it anymore at this point
|
; if the entry we were about to read had expired, session-map will not have it anymore at this point
|
||||||
(if (contains? session-map key)
|
(if (contains? current key)
|
||||||
(-> session-map ; note: performs faster than get-in
|
(-> current ; note: performs faster than get-in
|
||||||
(get key)
|
(get key)
|
||||||
(get :value))
|
(get :value))
|
||||||
(when on-removal
|
(when (and on-removal
|
||||||
(on-removal key (:value existing-entry) :expired)
|
; just in case there was a last second change by other concurrently running actions ...
|
||||||
|
(contains? previous key))
|
||||||
|
(on-removal key (-> previous (get key) :value) :expired)
|
||||||
nil)))))
|
nil)))))
|
||||||
|
|
||||||
(write-session [_ key data]
|
(write-session [_ key data]
|
||||||
|
@ -99,13 +101,13 @@
|
||||||
(swap! op-counter inc))
|
(swap! op-counter inc))
|
||||||
(let [key (or key (unique-id))]
|
(let [key (or key (unique-id))]
|
||||||
(if on-removal
|
(if on-removal
|
||||||
; when we have an on-removal listener, we need to check if we are about to overwrite an entry
|
; when we have an on-removal listener, we need to check if we are overwriting an entry
|
||||||
; that has already expired, and if so, call on-removal for it
|
; that has already expired, and if so, call on-removal for it
|
||||||
; (note that if it has ALREADY expired, yes, we're about to overwrite this entry anyway, but
|
; (note that if it has ALREADY expired, yes, we're about to overwrite this entry anyway, but
|
||||||
; we DO need to treat it as an expiry, because the old value expired ...)
|
; we DO need to treat it as an expiry, because the old value expired ...)
|
||||||
(let [existing-entry (get @session-atom key)
|
(let [[previous current] (swap-vals! session-atom process-write-entry key data refresh-on-write)
|
||||||
expired? (entry-expired? ttl existing-entry)]
|
existing-entry (get previous key)
|
||||||
(swap! session-atom process-write-entry key data refresh-on-write)
|
expired? (entry-expired? ttl existing-entry)]
|
||||||
(if expired?
|
(if expired?
|
||||||
(on-removal key (:value existing-entry) :expired)))
|
(on-removal key (:value existing-entry) :expired)))
|
||||||
; if there's no on-removal listener, we can simply process the write
|
; if there's no on-removal listener, we can simply process the write
|
||||||
|
|
Loading…
Reference in a new issue