Guest User

refreshing cache

a guest
Mar 7th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; pretend to be a database.
  2. (def database (atom 0))        
  3. (defn read-database []        
  4.   (println "reading from the actual database")
  5.   @database)
  6. (defn update-database [] (swap! database inc))
  7.  
  8. ;; manage a cache that can be refreshed.
  9. (defn set-up-caching-calculation [fun]
  10.   (atom (delay (future (fun)))))  
  11. (defn reset-caching-calculation [cache fun]
  12.   (fn []
  13.     (reset! cache (delay (future (fun))))))
  14. (defn get-caching-calculation [cache]
  15.   (-> cache deref deref deref))
  16.  
  17. ;; create one
  18. (def my-cache-calc-1 (set-up-caching-calculation read-database))
  19. (def reset-calc (reset-caching-calculation my-cache-calc-1 read-database))
  20.  
  21. ;; test
  22. (dotimes [i 10]
  23.   (.start (Thread. (fn []
  24.     (let [data (get-caching-calculation my-cache-calc-1)]
  25.       (println data))))))
  26.  
  27. (reset-calc)
  28. (update-database)
Advertisement
Add Comment
Please, Sign In to add comment