Skip to content

Commit

Permalink
wip: adds an in-memory cache to curl requests
Browse files Browse the repository at this point in the history
Caches repeated requests via curl with an in-memory cache.
This allows the tests to make each request only once, and more tests can
be written without encroaching further on third-party service
rate-limits.

Note that this in-memory cache lives for the life of the program, so
long-running services that use neil as a library would need to
periodically clear the cache, unless some other clearing mechanism is
implemented.

In this model, typical bb one-off usages should always start with an
empty cache, so there should be little change for end-users.
  • Loading branch information
russmatney committed Oct 15, 2022
1 parent ca073c4 commit fb5d718
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
24 changes: 22 additions & 2 deletions neil
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,35 @@
(def dev-github-token (System/getenv "BABASHKA_NEIL_DEV_GITHUB_TOKEN"))

(def curl-opts
(merge {:throw false
(merge {:throw false
:compressed (not (fs/windows?))}
(when (and dev-github-user dev-github-token)
{:basic-auth [dev-github-user dev-github-token]})))

(defn curl-get-json [url]
(defn do-curl-get-json [url]
(-> (curl/get url curl-opts)
:body (cheshire/parse-string true)))

(def curl-cache
"An in-memory cache reducing repeated api calls.
Intended to support test code."
(atom {}))

(defn clear-cache []
(reset! curl-cache {}))

;; TODO flag via env var
(def dev-cache-enabled true)

(defn curl-get-json [url]
(if dev-cache-enabled
(if-let [cached (@curl-cache url)]
cached
(let [result (do-curl-get-json url)]
(swap! curl-cache assoc url result)
result))
(do-curl-get-json url)))

(ns babashka.neil.git
{:no-doc true}
(:require [babashka.fs :as fs]
Expand Down
24 changes: 22 additions & 2 deletions src/babashka/neil/curl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,31 @@
(def dev-github-token (System/getenv "BABASHKA_NEIL_DEV_GITHUB_TOKEN"))

(def curl-opts
(merge {:throw false
(merge {:throw false
:compressed (not (fs/windows?))}
(when (and dev-github-user dev-github-token)
{:basic-auth [dev-github-user dev-github-token]})))

(defn curl-get-json [url]
(defn do-curl-get-json [url]
(-> (curl/get url curl-opts)
:body (cheshire/parse-string true)))

(def curl-cache
"An in-memory cache reducing repeated api calls.
Intended to support test code."
(atom {}))

(defn clear-cache []
(reset! curl-cache {}))

;; TODO flag via env var
(def dev-cache-enabled true)

(defn curl-get-json [url]
(if dev-cache-enabled
(if-let [cached (@curl-cache url)]
cached
(let [result (do-curl-get-json url)]
(swap! curl-cache assoc url result)
result))
(do-curl-get-json url)))

0 comments on commit fb5d718

Please sign in to comment.