I'm confused by how calls with carmine should be done. I found the wcar
macro described in carmine's docs:
(defmacro wcar [& body] `(car/with-conn pool spec-server1 ~@body))
Do I really have to call wcar
every time I want to talk to redis in addition to the redis command? Or can I just call it once at the beginning? If so how?
This is what some code with tavisrudd's redis library looked like (from my toy url shortener project's testsuite):
(deftest test_shorten_doesnt_exist_create_new_next
(redis/with-server test-server
(redis/set "url_counter" 51)
(shorten test-url)
(is (= "1g" (redis/get (str "urls|" test-url))))
(is (= test-url (redis/get "shorts|1g")))))
And now I can only get it working with carmine by writing it like this:
(deftest test_shorten_doesnt_exist_create_new_next
(wcar (car/set "url_counter" 51))
(shorten test-url)
(is (= "1g" (wcar (car/get (str "urls|" test-url)))))
(is (= test-url (wcar (car/get "shorts|1g")))))
So what's the right way of using it and what underlying concept am I not getting?
Dan's explanation is correct.
Carmine
uses response pipelining by default, whereasredis-clojure
requires you to ask for pipelining when you want it (using thepipeline
macro).The main reason you'd want pipelining is for performance. Redis is so fast that the bottleneck in using it is often the time it takes for the request+response to travel over the network.
Clojure destructuring provides a convenient way of dealing with the pipelined response, but it does require writing your code differently to
redis-clojure
. The way I'd write your example is something like this (I'm assuming yourshorten
fn has side effects and needs to be called before theGET
s):So we're sending the first (
SET
) request to Redis and waiting for the reply (I'm not certain if that's actually necessary here). We then send the next two (GET
) requests at once, allow Redis to queue the responses, then receive them all back at once as a vector that we'll destructure.At first this may seem like unnecessary extra effort because it requires you to be explicit about when to receive queued responses, but it brings a lot of benefits including performance, clarity, and composable commands.
I'd check out Touchstone on GitHub if you're looking for an example of what I'd consider idiomatic
Carmine
use (just search for thewcar
calls). (Sorry, SO is preventing me from including another link).Otherwise just pop me an email (or file a GitHub issue) if you have any other questions.