受这篇关于 Event Sourcing 的博客文章的启发,我想知道是否可以实现与 Java ConcurrentHashMap 等效的 Clojure 结构。将 Rich 的这篇文章作为 Clojure 恶意列表的基础 ,我提出了以下部分实现,但我不确定它是否是并发安全的?如果不是,任何人都可以提出一种更好的方法来构建这种结构吗?
(defn ^M h []
(let [m (atom {})]
(reify M
(put [_ k v] (swap! m assoc k v))
(get [_ k] (get @m k))
(putIfAbsent [_ k v]
(swap! m #(if (get % k)
%
(assoc % k v))))
(replace [_ k old-v new-v]
(swap! m #(if (= old-v (get % k))
(assoc % k new-v)
%))))))
(definterface M
(put [k v])
(get [k])
(putIfAbsent [k v])
(replace [k old-v new-v]))
参考:
http://www.jayway.com/2013/04/02/event-sourcing-in-clojure/
https://groups.google.com/d/msg/clojure/dK6x_QpCpvo/OitIryoFSAgJ