因此,我有一个要访问的帖子数据库,但我还希望缓存查询的结果,这样就不会与数据库建立额外的连接。
到目前为止,我有一些类似的东西
;;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))))这将返回一个结果集合,如下所示
({:_id #<ObjectId 54d927ce9c521eb276553f11>, :post_content "Mermaids and dakinis "},
{ .... },
{ .... },
{ .... })我认为这样做的一个好方法是将结果存储在符号中(var?钥匙?..不确定Clojure的合适说法是什么),然后检查是否设置了该var。
开发人员通常如何处理这种情况?
发布于 2015-02-23 02:28:24
如果您的目标是添加缓存图层,则可以查看库core.memoize
https://stackoverflow.com/questions/28661412
复制相似问题