Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; pretend to be a database.
- (def database (atom 0))
- (defn read-database []
- (println "reading from the actual database")
- @database)
- (defn update-database [] (swap! database inc))
- ;; manage a cache that can be refreshed.
- (defn set-up-caching-calculation [fun]
- (atom (delay (future (fun)))))
- (defn reset-caching-calculation [cache fun]
- (fn []
- (reset! cache (delay (future (fun))))))
- (defn get-caching-calculation [cache]
- (-> cache deref deref deref))
- ;; create one
- (def my-cache-calc-1 (set-up-caching-calculation read-database))
- (def reset-calc (reset-caching-calculation my-cache-calc-1 read-database))
- ;; test
- (dotimes [i 10]
- (.start (Thread. (fn []
- (let [data (get-caching-calculation my-cache-calc-1)]
- (println data))))))
- (reset-calc)
- (update-database)
Advertisement
Add Comment
Please, Sign In to add comment