Skip to content

Commit

Permalink
db cached
Browse files Browse the repository at this point in the history
  • Loading branch information
nossal committed Aug 5, 2018
1 parent 9f9a197 commit 1342e65
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
3 changes: 1 addition & 2 deletions project.clj
Expand Up @@ -84,8 +84,7 @@

:profiles {:production {:env {:dev false :production true}
:prep-tasks [["cljsbuild" "once" "app" "sw"] ["garden" "once"]]}

:dev {:env {:dev "true", :production "false", :database-url "postgres://localhost/nossal"}
:dev {:env {:dev "true", :production "false", :database-url "postgres://nossal:nossal@localhost:5432/nossal"}
; :prep-tasks [["garden" "once"]]
:cljsbuild {:builds
{:app {:source-paths ["src/clojurescript/nossal/app"]
Expand Down
4 changes: 1 addition & 3 deletions src/clojure/nossal/api/shortner.clj
Expand Up @@ -20,10 +20,8 @@
(-> (res/response (json/write-str (encode (:id (first (data/insert-url db {:url url}))))))
(res/content-type "text/plain")))

(def get-url (memoize (fn [id] (data/url-by-id db {:id (decode encoded-id)}))))

(defn redirect [encoded-id]
(let [url (:url (get-url encoded-id))]
(let [url (:url (data/get-data data/url-by-id {:id (decode encoded-id)}))]
(log/info url)
(do
(client/post "https://www.google-analytics.com/collect"
Expand Down
11 changes: 11 additions & 0 deletions src/clojure/nossal/db.clj
@@ -1,6 +1,7 @@
(ns nossal.db
(:require [hugsql.core :as hugsql]
[clojure.string :as s]
[clojure.core.cache :as cache]
[environ.core :refer [env]]
[heroku-database-url-to-jdbc.core :as h]))

Expand All @@ -13,3 +14,13 @@
(hugsql/def-db-fns "sql/schema.sql")

(hugsql/def-sqlvec-fns "sql/schema.sql")


(defonce cache-store (atom (cache/lru-cache-factory {})))

(defn get-data [func key]
(cache/lookup (swap! cache-store
#(if (cache/has? % key)
(cache/hit % key)
(cache/miss % key (func db key))))
key))

0 comments on commit 1342e65

Please sign in to comment.