initial support for caching markdown->html results

This commit is contained in:
Gered 2014-03-29 09:21:15 -04:00
parent a959779872
commit 5fc46d12e9
3 changed files with 59 additions and 33 deletions

View file

@ -26,7 +26,7 @@
</div>
<div class="content">
{{ post.body|md_to_html }}
{{ post.html_body }}
</div>
<div class="footer">

View file

@ -2,11 +2,29 @@
(:use [blarg.models.db]
[blarg.datetime]
[slugger.core])
(:require [com.ashafa.clutch :as couch]))
(:require [com.ashafa.clutch :as couch]
[clj-time.core :refer [after?]]
[blarg.util :refer [md->html]]))
(def per-page 5)
(def timestamp-fields [:created_at])
(defonce post-md-cache (atom {}))
(defn- cache-post! [{:keys [_id created_at body] :as post}]
(let [cached-post (get @post-md-cache _id)]
(if (or (not cached-post)
(after? created_at (:created_at cached-post)))
(swap! post-md-cache assoc _id {:created_at created_at
:html_body (md->html body)})
cached-post)))
(defn- merge-cached-post-md! [post]
(let [cached-post (cache-post! post)]
(merge
post
(select-keys cached-post [:html_body]))))
(defmacro ->post-list [& body]
`(string->date
(->view-values
@ -22,29 +40,34 @@
~timestamp-fields))
(defn get-post [id]
(merge-cached-post-md!
(->single-post
(couch/with-db posts
(couch/get-document id))))
(couch/get-document id)))))
(defn get-post-by-date-slug [date slug]
(merge-cached-post-md!
(->single-post
(couch/with-db posts
(couch/get-view "posts" "listPostsBySlug" {:key [date, slug]}))))
(couch/get-view "posts" "listPostsBySlug" {:key [date, slug]})))))
(defn add-post [title body user tags]
(merge-cached-post-md!
(->single-post
(couch/with-db posts
(couch/put-document {:title title
(couch/put-document
{:title title
:slug (->slug title)
:body body
:user user
:tags tags
:created_at (get-timestamp)
:published false
:type "post"}))))
:type "post"})))))
(defn update-post [id title body user tags published? reset-date?]
(if-let [post (get-post id)]
(merge-cached-post-md!
(->single-post
(couch/with-db posts
(couch/update-document
@ -54,7 +77,7 @@
(merge (if user {:user user}))
(merge (if tags {:tags tags}))
(merge (if-not (nil? published?) {:published published?}))
(merge (if reset-date? {:created_at (get-timestamp)}))))))))
(merge (if reset-date? {:created_at (get-timestamp)})))))))))
(defn delete-post [id]
(if-let [post (get-post id)]
@ -78,9 +101,11 @@
(when n {:limit n})
(when offset {:skip offset}))
viewName (if unpublished? "listPosts" "listPublishedPosts")]
(map
merge-cached-post-md!
(->post-list
(couch/with-db posts
(couch/get-view "posts" viewName params))))))
(couch/get-view "posts" viewName params)))))))
(defn list-posts-by-tag [unpublished? tag]
(let [view-name (if unpublished? "listPostsByTag" "listPublishedPostsByTag")]

View file

@ -41,10 +41,11 @@
(let [totalpages (postcount->pagecount )
lastpage (make-in-range (- totalpages 1) 0 totalpages)
currentpage (make-in-range page 0 lastpage)
offset (* currentpage posts/per-page)]
offset (* currentpage posts/per-page)
posts (posts/list-posts (auth/logged-in?) posts/per-page offset)]
(layout/render
"posts/list.html"
:params {:posts (posts/list-posts (auth/logged-in?) posts/per-page offset)
:params {:posts posts
:prevpage (- currentpage 1)
:nextpage (+ currentpage 1)
:atlastpage (= currentpage lastpage)