Reputation: 5660
So I have a database of posts I want to access, but I also want to cache the results of the query so I'm not making extra connections to the database.
So far I have something like
;;talk with the database and get posts by their [count]
(defn posts-from-db []
(let [conn (mg/connect {:host "127.0.0.1" :port 27272})
db (mg/get-db conn "submitted-content")
coll "posts"]
(with-collection db coll
(find {})
(fields [:post_content :id])
;; it is VERY IMPORTANT to use array maps with sort
(sort (array-map :tags -1 :post_content 1))
(limit numberOfPosts))))
This returns a collection of results that look like
({:_id #<ObjectId 54d927ce9c521eb276553f11>, :post_content "Mermaids and dakinis "},
{ .... },
{ .... },
{ .... })
I think a good way to do this is to store the result in a symbol (var? key? .. not sure what the appropriate verbiage is for Clojure) and then check if that var is set.
How does a developer normally address this situation?
Upvotes: 1
Views: 81
Reputation: 588
If your goal is to add a cache layer, you can look at the library core.memoize
Upvotes: 2