Skip to content
This repository has been archived by the owner on Dec 26, 2020. It is now read-only.

Commit

Permalink
Lock when ISBN is already in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroopch committed Jun 2, 2012
1 parent c5fe508 commit 3d35d6d
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/isbnnetinclj/models/stores.clj
Expand Up @@ -20,9 +20,23 @@


(defonce book-data-cache (atom (cache/ttl-cache-factory (* 60 60 24) {})))
(defonce book-in-progress-lock (atom {}))
(def book-data-collection "book_data")


(def book-data-collection "book_data_log")
(defn get-book-in-progress
[isbn]
(get @book-in-progress-lock isbn))


(defn set-book-in-progress
[isbn]
(swap! book-in-progress-lock assoc isbn true))


(defn done-book-in-progress
[isbn]
(swap! book-in-progress-lock dissoc isbn))


(defn get-in-memory-book-data
Expand Down Expand Up @@ -68,12 +82,16 @@

(defn fetch-book-data
[isbn]
(doseq [f (map #(future (fetch-book-data-from-one-store isbn %)) sites)]
(deref f))
(swap! book-data-cache assoc-in [isbn :when] (java.util.Date.))
(let [data (get-in-memory-book-data isbn)]
(future (mc/insert book-data-collection data))
data))
(if-not (get-book-in-progress isbn)
(do
(set-book-in-progress isbn)
(doseq [f (map #(future (fetch-book-data-from-one-store isbn %)) sites)]
(deref f))
(swap! book-data-cache assoc-in [isbn :when] (java.util.Date.))
(done-book-in-progress isbn)
(let [data (get-in-memory-book-data isbn)]
(future (mc/insert book-data-collection data))
data))))


(defn book-data
Expand Down

0 comments on commit 3d35d6d

Please sign in to comment.