Implement a key-value database that stores to a file for durability.
How can this be made transactional?
How to make it thread-safe?
How to make it safe across processes?
Implement a key-value database that stores to a file for durability.
How can this be made transactional?
How to make it thread-safe?
How to make it safe across processes?
(def db (atom {}))
(add-watch db ::saver (fn [_ _ old new]
(spit "dbsave.edn" (pr-str new))))
(defn read-db []
(clojure.edn/read-string (slurp "dbsave.edn")))
(defn restore-db []
(reset! db (read-db)))
We wanted the writes to be thread-safe.
(def db (atom {}))
(def die? false)
(def t (doto (Thread. (fn []
(loop [old-state @db]
(when (not die?)
(Thread/sleep 1000)
(let [new-state @db]
(when (not= old-state new-state)
(spit "dbsave.edn" (pr-str new-state)))
(recur new-state))))))
(.start)))
(defn read-db []
(clojure.edn/read-string (slurp "dbsave.edn")))
(defn restore-db []
(reset! db (read-db)))
Clojure/North https://clojurenorth.com (I'll be speaking, talking about ClojureScript type inference updates.)