Created
February 22, 2016 21:41
-
-
Save szymonkaliski/b52cb6bf681e689bc2c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def google-get-cache (atom (cache/ttl-cache-factory {} :ttl (* 1000 60 60 1)))) | |
(defn google-get [access-token path] | |
(-> (client/get (str "https://www.googleapis.com/calendar/v3/" path) | |
{:query-params {:access_token access-token} | |
:accept :json}) | |
:body | |
(json/read-str :key-fn #(-> % ->kebab-case keyword)))) | |
(defn google-get-cached [access-token path] | |
(if (cache/has? @google-get-cache path) | |
(get (cache/hit @google-get-cache path) path) | |
(let [results (google-get access-token path)] | |
(swap! google-get-cache #(cache/miss % path results)) | |
results))) |
Oh, yeah, but the thing is that I want to cache it only by the path, because access-key
can change, but the results will still be the same (the key might be refreshed any time), that's why I'm using only path
for caching :)
Oopps. of course, forgot that crucial part at that late hour... :) Then just curry that google-get
fn when invoking from cache/through
...
(def google-cache (atom (cache/ttl-cache-factory {} :ttl (* 1000 60 60 1))))
(defn google-get [access-token path]
(-> (str "https://www.googleapis.com/calendar/v3/" path)
(client/get {:query-params {:access_token access-token}
:accept :json})
:body
(json/read-str :key-fn #(-> % ->kebab-case keyword))))
;; https://crossclj.info/fun/clojure.core.cache/through.html
(defn google-get-cached [access-token path]
(-> google-cache
(swap! #(cache/through (partial google-get access-token) identity % path))
(get path)))
Thanks, needed small fix (removed identity
), but works:
;; https://crossclj.info/fun/clojure.core.cache/through.html
(defn google-get-cached [access-token path]
(-> google-cache
(swap! #(cache/through (partial google-get access-token) % path))
(get path)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generally looks ok to me, but could be simplified with
cache/through
and combined cache key of token + path: