Skip to content

Instantly share code, notes, and snippets.

@szymonkaliski
Created February 22, 2016 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szymonkaliski/b52cb6bf681e689bc2c3 to your computer and use it in GitHub Desktop.
Save szymonkaliski/b52cb6bf681e689bc2c3 to your computer and use it in GitHub Desktop.
(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)))
@postspectacular
Copy link

Generally looks ok to me, but could be simplified with cache/through and combined cache key of token + path:

(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]
  (let [key [access-token path]]
    (swap! google-cache #(cache/through google-get identity % key))
    (google-cache key)))

@szymonkaliski
Copy link
Author

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 :)

@postspectacular
Copy link

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)))

@szymonkaliski
Copy link
Author

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