initial support for caching markdown->html results
This commit is contained in:
parent
a959779872
commit
5fc46d12e9
|
@ -26,7 +26,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
{{ post.body|md_to_html }}
|
{{ post.html_body }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="footer">
|
<div class="footer">
|
||||||
|
|
|
@ -2,11 +2,29 @@
|
||||||
(:use [blarg.models.db]
|
(:use [blarg.models.db]
|
||||||
[blarg.datetime]
|
[blarg.datetime]
|
||||||
[slugger.core])
|
[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 per-page 5)
|
||||||
(def timestamp-fields [:created_at])
|
(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]
|
(defmacro ->post-list [& body]
|
||||||
`(string->date
|
`(string->date
|
||||||
(->view-values
|
(->view-values
|
||||||
|
@ -22,39 +40,44 @@
|
||||||
~timestamp-fields))
|
~timestamp-fields))
|
||||||
|
|
||||||
(defn get-post [id]
|
(defn get-post [id]
|
||||||
(->single-post
|
(merge-cached-post-md!
|
||||||
(couch/with-db posts
|
(->single-post
|
||||||
(couch/get-document id))))
|
(couch/with-db posts
|
||||||
|
(couch/get-document id)))))
|
||||||
|
|
||||||
(defn get-post-by-date-slug [date slug]
|
(defn get-post-by-date-slug [date slug]
|
||||||
(->single-post
|
(merge-cached-post-md!
|
||||||
(couch/with-db posts
|
(->single-post
|
||||||
(couch/get-view "posts" "listPostsBySlug" {:key [date, slug]}))))
|
(couch/with-db posts
|
||||||
|
(couch/get-view "posts" "listPostsBySlug" {:key [date, slug]})))))
|
||||||
|
|
||||||
(defn add-post [title body user tags]
|
(defn add-post [title body user tags]
|
||||||
(->single-post
|
(merge-cached-post-md!
|
||||||
(couch/with-db posts
|
(->single-post
|
||||||
(couch/put-document {:title title
|
(couch/with-db posts
|
||||||
:slug (->slug title)
|
(couch/put-document
|
||||||
:body body
|
{:title title
|
||||||
:user user
|
:slug (->slug title)
|
||||||
:tags tags
|
:body body
|
||||||
:created_at (get-timestamp)
|
:user user
|
||||||
:published false
|
:tags tags
|
||||||
:type "post"}))))
|
:created_at (get-timestamp)
|
||||||
|
:published false
|
||||||
|
:type "post"})))))
|
||||||
|
|
||||||
(defn update-post [id title body user tags published? reset-date?]
|
(defn update-post [id title body user tags published? reset-date?]
|
||||||
(if-let [post (get-post id)]
|
(if-let [post (get-post id)]
|
||||||
(->single-post
|
(merge-cached-post-md!
|
||||||
(couch/with-db posts
|
(->single-post
|
||||||
(couch/update-document
|
(couch/with-db posts
|
||||||
(-> (date->string post timestamp-fields)
|
(couch/update-document
|
||||||
(merge (if title {:title title :slug (->slug title)}))
|
(-> (date->string post timestamp-fields)
|
||||||
(merge (if body {:body body}))
|
(merge (if title {:title title :slug (->slug title)}))
|
||||||
(merge (if user {:user user}))
|
(merge (if body {:body body}))
|
||||||
(merge (if tags {:tags tags}))
|
(merge (if user {:user user}))
|
||||||
(merge (if-not (nil? published?) {:published published?}))
|
(merge (if tags {:tags tags}))
|
||||||
(merge (if reset-date? {:created_at (get-timestamp)}))))))))
|
(merge (if-not (nil? published?) {:published published?}))
|
||||||
|
(merge (if reset-date? {:created_at (get-timestamp)})))))))))
|
||||||
|
|
||||||
(defn delete-post [id]
|
(defn delete-post [id]
|
||||||
(if-let [post (get-post id)]
|
(if-let [post (get-post id)]
|
||||||
|
@ -78,9 +101,11 @@
|
||||||
(when n {:limit n})
|
(when n {:limit n})
|
||||||
(when offset {:skip offset}))
|
(when offset {:skip offset}))
|
||||||
viewName (if unpublished? "listPosts" "listPublishedPosts")]
|
viewName (if unpublished? "listPosts" "listPublishedPosts")]
|
||||||
(->post-list
|
(map
|
||||||
(couch/with-db posts
|
merge-cached-post-md!
|
||||||
(couch/get-view "posts" viewName params))))))
|
(->post-list
|
||||||
|
(couch/with-db posts
|
||||||
|
(couch/get-view "posts" viewName params)))))))
|
||||||
|
|
||||||
(defn list-posts-by-tag [unpublished? tag]
|
(defn list-posts-by-tag [unpublished? tag]
|
||||||
(let [view-name (if unpublished? "listPostsByTag" "listPublishedPostsByTag")]
|
(let [view-name (if unpublished? "listPostsByTag" "listPublishedPostsByTag")]
|
||||||
|
|
|
@ -41,10 +41,11 @@
|
||||||
(let [totalpages (postcount->pagecount )
|
(let [totalpages (postcount->pagecount )
|
||||||
lastpage (make-in-range (- totalpages 1) 0 totalpages)
|
lastpage (make-in-range (- totalpages 1) 0 totalpages)
|
||||||
currentpage (make-in-range page 0 lastpage)
|
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
|
(layout/render
|
||||||
"posts/list.html"
|
"posts/list.html"
|
||||||
:params {:posts (posts/list-posts (auth/logged-in?) posts/per-page offset)
|
:params {:posts posts
|
||||||
:prevpage (- currentpage 1)
|
:prevpage (- currentpage 1)
|
||||||
:nextpage (+ currentpage 1)
|
:nextpage (+ currentpage 1)
|
||||||
:atlastpage (= currentpage lastpage)
|
:atlastpage (= currentpage lastpage)
|
||||||
|
|
Reference in a new issue