1

所以我有一个我想访问的帖子数据库,但我也想缓存查询的结果,所以我不会与数据库建立额外的连接。

到目前为止,我有类似的东西

 ;;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。

开发人员通常如何解决这种情况?

4

1 回答 1

2

如果您的目标是添加缓存层,您可以查看库core.memoize

于 2015-02-22T18:28:24.183 回答