Use taoensso.carmine to check existance of multiple keys

215 Views Asked by At

I am using taoensso.carmine redis client and want to achieve the following: given sequence s, get all its elements that aren't exist in redis. (I mean for which redis's EXISTS command return false)

At first I thought to do the following:

(wcar conn
  (remove #(car/exists %) s))

but it returns sequence of car/exists responses rather than filtering my sequence by them

(remove #(wcar conn (car exists %)) s)

Does the job but takes a lot of time because no-pipeling and using new connection each time.

So I end up with some tangled map manipulation below, but I believe there should be simplier way to achieve it. How?

(let [s (range 1 100)
      existance (wcar conn
                      (doall
                       (for [i s]
                         (car/exists i))))
      existance-map (zipmap s existance)]
  (mapv first (remove (fn [[k v]] (= v 1)) existance-map)))
2

There are 2 best solutions below

0
On

Could you reexamine you're first solution? I don't know what wcar does, but this example shows that you're on the right track:

> (remove #(odd? %) (range 9))
(0 2 4 6 8)

The anonymous function #(odd? %) returns either true or false results which are used to determine which numbers to keep. However, it is the original numbers that are returned by (remove...), not true/false.

0
On

Your remove function is lazy, so it won't do anything. You also can't do data manipulation inside the wcar macro so I'd so something like this:

(let [keys ["exists" "not-existing"]]
  (zipmap keys
          (mapv pos?
                (car/wcar redis-db
                          (mapv (fn [key]
                                  (car/exists key))
                                keys)))))