-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.clj
70 lines (49 loc) 路 1.49 KB
/
redis.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(ns omega-red.redis
"Represents a redis connection spec,
so we can use it when using Carmine, unlike the
regular `wcar*` macro, recommended in Carmine docs."
(:require
[com.stuartsierra.component :as component]
[omega-red.protocol :as proto]
[taoensso.carmine :as carmine]))
(defn get-redis-fn*
"Finds actual function instance, based on a keyword.
So:
:hset -> taoensso.carmine/hset
which then can be used as normal function"
[fn-name-keyword]
(ns-resolve 'taoensso.carmine (symbol fn-name-keyword)))
(def get-redis-fn (memoize get-redis-fn*))
(defn execute*
[conn redis-fn+args]
(let [[redis-fn & args] redis-fn+args]
(carmine/wcar conn (apply (get-redis-fn redis-fn) args))))
(defn execute-pipeline*
[conn redis-fns+args]
(carmine/wcar conn
:as-pipeline
(mapv (fn [cmd+args]
(let [redis-fn (get-redis-fn (first cmd+args))]
(apply redis-fn (rest cmd+args))))
redis-fns+args)))
(defrecord Redis
[pool spec conn]
component/Lifecycle
(start
[this]
(assoc this :conn {:pool pool :spec spec}))
(stop
[this]
(assoc this :conn nil))
proto/Redis
(execute
[_ redis-fn+args]
(execute* conn redis-fn+args))
(execute-pipeline
[_ redis-fns+args]
(execute-pipeline* conn redis-fns+args)))
(defn create
[{:keys [host port]}]
{:pre [(string? host)
(number? port)]}
(->Redis {} {:host host :port port} nil))