I'm using HTTP Kit to make requests, and I want them to be asynchronous, but I also want to cache the responses. The reason I want the requests to be asynchronous is that I am making several concurrently and I want them to operate in parallel.
Here's my function that makes several requests in parallel.
(defn parallel-requests-1 [urls]
(let [; Dispatch all requests to run concurrently.
responses (doall (map #(http/get %) urls))
; Realise all promises.
realised (doall (map deref responses))
; Extract response body.
bodies (map :body realised)]
bodies))
(parallel-requests-1 ["http://example.com", "http://example.net"])
This is purely for illustrative purposes to demonstrate that I don't want to just deref
the promise and memoize that.
Now I want to add caching using memoize
. I tried this:
(def memoized-get (memoize http/get))
(defn parallel-requests-2 [urls]
(let [; Dispatch all requests to run concurrently.
responses (doall (map #(memoized-get %) urls))
; Realise all promises.
realised (doall (map deref responses))
; Extract response body.
bodies (map :body realised)]
bodies))
All the signs show that this works well.
Is this a sensible solution? My concern is that caching a promise might constitute some kind of resource leak.