]>
Commit | Line | Data |
---|---|---|
e59229a2 | 1 | ;(add-classpath "file:///Users/ragge/Projects/clojure/redis-clojure/src/") |
2 | ||
3 | (set! *warn-on-reflection* true) | |
4 | ||
5 | (ns redis | |
6 | (:refer-clojure :exclude [get set type keys sort]) | |
7 | (:use redis.internal)) | |
8 | ||
9 | (defmacro with-server | |
10 | "Evaluates body in the context of a new connection to a Redis server | |
11 | then closes the connection. | |
12 | ||
13 | server-spec is a map with any of the following keys: | |
14 | :host hostname (default \"127.0.0.1\") | |
15 | :port port (default 6379) | |
16 | :db database to use (default 0)" | |
17 | [server-spec & body] | |
18 | `(with-server* ~server-spec (fn [] | |
19 | (do | |
20 | (redis/select (:db *server*)) | |
21 | ~@body)))) | |
22 | ||
23 | ||
24 | ;; | |
25 | ;; Reply conversion functions | |
26 | ;; | |
27 | (defn int-to-bool | |
28 | "Convert integer reply to a boolean value" | |
29 | [int] | |
30 | (= 1 int)) | |
31 | ||
32 | (defn string-to-keyword | |
33 | "Convert a string reply to a keyword" | |
34 | [string] | |
35 | (keyword string)) | |
36 | ||
37 | (defn string-to-seq | |
38 | "Convert a space separated string to a sequence of words" | |
39 | [#^String string] | |
40 | (if (empty? string) | |
41 | nil | |
42 | (re-seq #"\S+" string))) | |
43 | ||
44 | (defn string-to-map | |
45 | "Convert strings with format 'key:value\r\n'+ to a map with {key | |
46 | value} pairs" | |
47 | [#^String string] | |
48 | (let [lines (.split string "(\\r\\n|:)")] | |
49 | (apply hash-map lines))) | |
50 | ||
51 | (defn int-to-date | |
52 | "Return a Date representation of a UNIX timestamp" | |
53 | [int] | |
54 | (new java.util.Date (long int))) | |
55 | ||
56 | (defn seq-to-set | |
57 | [sequence] | |
58 | (clojure.core/set sequence)) | |
59 | ||
60 | ;; | |
61 | ;; Commands | |
62 | ;; | |
63 | (defcommands | |
64 | ;; Connection handling | |
65 | (auth [] :inline) | |
66 | (quit [password] :inline) | |
67 | (ping [] :inline) | |
68 | ;; String commands | |
69 | (set [key value] :bulk) | |
70 | (get [key] :inline) | |
71 | (getset [key value] :bulk) | |
72 | (setnx [key value] :bulk int-to-bool) | |
73 | (incr [key] :inline) | |
74 | (incrby [key integer] :inline) | |
75 | (decr [key] :inline) | |
76 | (decrby [key integer] :inline) | |
77 | (exists [key] :inline int-to-bool) | |
78 | (mget [key & keys] :inline) | |
79 | (del [key] :inline int-to-bool) | |
80 | ;; Key space commands | |
81 | (type [key] :inline string-to-keyword) | |
82 | (keys [pattern] :inline string-to-seq) | |
83 | (randomkey [] :inline) | |
84 | (rename [oldkey newkey] :inline) | |
85 | (renamenx [oldkey newkey] :inline int-to-bool) | |
86 | (dbsize [] :inline) | |
87 | (expire [key seconds] :inline int-to-bool) | |
88 | (ttl [key] :inline) | |
89 | ;; List commands | |
90 | (rpush [key value] :bulk) | |
91 | (lpush [key value] :bulk) | |
92 | (llen [key] :inline) | |
93 | (lrange [key start end] :inline) | |
94 | (ltrim [key start end] :inline) | |
95 | (lindex [key index] :inline) | |
96 | (lset [key index value] :bulk) | |
97 | (lrem [key count value] :bulk) | |
98 | (lpop [key] :inline) | |
99 | (rpop [key] :inline) | |
100 | ;; Set commands | |
101 | (sadd [key member] :bulk int-to-bool) | |
102 | (srem [key member] :bulk int-to-bool) | |
103 | (smove [srckey destkey member] :bulk int-to-bool) | |
104 | (scard [key] :inline) | |
105 | (sismember [key member] :bulk int-to-bool) | |
106 | (sinter [key & keys] :inline seq-to-set) | |
107 | (sinterstore [destkey key & keys] :inline) | |
108 | (sunion [key & keys] :inline seq-to-set) | |
109 | (sunionstore [destkey key & keys] :inline) | |
110 | (sdiff [key & keys] :inline seq-to-set) | |
111 | (sdiffstore [destkey key & keys] :inline) | |
112 | (smembers [key] :inline seq-to-set) | |
113 | ;; Multiple database handling commands | |
114 | (select [index] :inline) | |
115 | (move [key dbindex] :inline) | |
116 | (flushdb [] :inline) | |
117 | (flushall [] :inline) | |
118 | ;; Sorting | |
119 | (sort [key & options] :sort) | |
120 | ;; Persistence | |
121 | (save [] :inline) | |
122 | (bgsave [] :inline) | |
123 | (lastsave [] :inline int-to-date) | |
124 | (shutdown [] :inline) | |
125 | (info [] :inline string-to-map) | |
126 | ;;(monitor [] :inline)) | |
127 | ) |