This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rkt
481 lines (365 loc) · 12.5 KB
/
main.rkt
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#lang racket
(provide redis%)
(require "resp.rkt" racket/tcp)
(define redis%
(class
object%
(init-field [ip "127.0.0.1"] [port 6379] [timeout 1])
(field [out null] [in null])
(super-new)
(define/private (send msg)
(display msg out)
(flush-output out))
(define/private (get-response)
(let loop ([resp ""])
(let ([p (sync/timeout timeout in)])
(if (input-port? p)
(let ([s (read-line p)])
(if (eof-object? s)
(if (not (equal? resp ""))
(redis-decode resp)
"ERR timed out")
(loop (string-append resp s "\n"))))
(if (not (equal? resp ""))
(redis-decode resp)
"ERR timed out")))))
(define/private (apply-cmd cmd [args null])
(if (null? args)
(send (string-append cmd "\r\n"))
(send (redis-encode-array (append (list cmd) (if (list? args) args (list args)))))))
(define/public (set-timeout t) (set! timeout t))
(define/public (ping [msg null])
(apply-cmd "PING" msg)
(get-response))
(define/public (auth password)
(apply-cmd "AUTH" password)
(get-response))
(define/public (echo msg)
(apply-cmd "ECHO" msg)
(get-response))
(define/public (select index)
(apply-cmd "SELECT" index)
(get-response))
(define/public (quit)
(apply-cmd "QUIT")
(get-response))
(define/public (exists keys)
(apply-cmd "EXISTS" keys)
(get-response))
(define/public (set key value)
(apply-cmd "SET" (list key value))
(get-response))
(define/public (get key)
(apply-cmd "GET" key)
(get-response))
(define/public (mget keys)
(apply-cmd "MGET" keys)
(get-response))
(define/public (mset data)
(apply-cmd "MSET" data)
(get-response))
(define/public (msetnx data)
(apply-cmd "MSETNX" data)
(get-response))
(define/public (getset key value)
(apply-cmd "GETSET" (list key value))
(get-response))
(define/public (incr key)
(apply-cmd "INCR" key)
(get-response))
(define/public (incrby key value)
(apply-cmd "INCRBY" (list key value))
(get-response))
(define/public (decr key)
(apply-cmd "DECR" key)
(get-response))
(define/public (decrby key value)
(apply-cmd "DECRBY" (list key value))
(get-response))
(define/public (del key)
(apply-cmd "DEL" key)
(get-response))
(define/public (setnx key value)
(apply-cmd "SETNX" (list key value))
(get-response))
(define/public (lpush key value)
(apply-cmd "LPUSH" (if (list? value)
(append (list key) value)
(list key value)))
(get-response))
(define/public (rpush key value)
(apply-cmd "RPUSH" (if (list? value)
(append (list key) value)
(list key value)))
(get-response))
(define/public (lrange key min max)
(apply-cmd "LRANGE" (list key min max))
(get-response))
(define/public (ltrim key start end)
(apply-cmd "LTRIM" (list key start end))
(get-response))
(define/public (lindex key index)
(apply-cmd "LINDEX" (list key index))
(get-response))
(define/public (lset key index value)
(apply-cmd "LSET" (list key index value))
(get-response))
(define/public (lpop key string)
(apply-cmd "LPOP" (list key string))
(get-response))
(define/public (rpop key string)
(apply-cmd "RPOP" (list key string))
(get-response))
(define/public (blpop keys timeout)
(apply-cmd "BLPOP" (append keys (list timeout)))
(get-response))
(define/public (brpop keys timeout)
(apply-cmd "BRPOP" (append keys (list timeout)))
(get-response))
(define/public (rpoplpush srckey destkey)
(apply-cmd "RPOPLPUSH" (list srckey destkey))
(get-response))
(define/public (sadd key member)
(apply-cmd "SADD" (list key member))
(get-response))
(define/public (srem key member)
(apply-cmd "SREM" (list key member))
(get-response))
(define/public (spop key)
(apply-cmd "SPOP" key)
(get-response))
(define/public (srandmember key)
(apply-cmd "SRANDMEMBER" key)
(get-response))
(define/public (smove srckey destkey member)
(apply-cmd "SMOVE" (list srckey destkey member))
(get-response))
(define/public (scard key)
(apply-cmd "SCARD" key)
(get-response))
(define/public (sismember key member)
(apply-cmd "SISMEMBER" (list key member))
(get-response))
(define/public (sinter keys)
(apply-cmd "SINTER" keys)
(get-response))
(define/public (sinterstore destkey srckeys)
(apply-cmd "SINTERSTORE" (list destkey srckeys))
(get-response))
(define/public (sunion keys)
(apply-cmd "SUNION" keys)
(get-response))
(define/public (sunionstore destkey srckeys)
(apply-cmd "SUNIONSTORE" (list destkey srckeys))
(get-response))
(define/public (sdiff keys)
(apply-cmd "SDIFF" keys)
(get-response))
(define/public (sdiffstore destkey srckeys)
(apply-cmd "SDIFFSTORE" (list destkey srckeys))
(get-response))
(define/public (smembers key)
(apply-cmd "SMEMBERS" key)
(get-response))
(define/public (zadd key data)
(apply-cmd "ZADD" (append (list key) data))
(get-response))
(define/public (zrem key member)
(apply-cmd "ZREM" (append (list key) (if (list? member) member (list member))))
(get-response))
(define/public (zincrby key incr member)
(apply-cmd "ZINCRBY" (list key incr member))
(get-response))
(define/public (zrange key start end)
(apply-cmd "ZRANGE" (list key start end))
(get-response))
(define/public (zrevrange key start end)
(apply-cmd "ZREVRANGE" (list key start end))
(get-response))
(define/public (zrangebyscore key min max)
(apply-cmd "ZRANGEBYSCORE" (list key min max))
(get-response))
(define/public (zremrangebyscore key min max)
(apply-cmd "ZREMRANGEBYSCORE" (list key min max))
(get-response))
(define/public (zcard key)
(apply-cmd "ZCARD" key)
(get-response))
(define/public (zscore key member)
(apply-cmd "ZSCORE" (list key member))
(get-response))
(define/public (zlexcount key min max)
(apply-cmd "ZLEXCOUNT" (list key min max))
(get-response))
(define/public (zrangebylex key min max)
(apply-cmd "ZRANGEBYLEX" (list key min max))
(get-response))
(define/public (zinterstore dest keys)
(apply-cmd "ZINTERSTORE" (append (list dest) keys))
(get-response))
(define/public (zcount key min max)
(apply-cmd "ZCOUNT" (list key min max))
(get-response))
(define/public (zrevrank key member)
(apply-cmd "ZREVRANK" (list key member))
(get-response))
(define/public (zrevrangebyscore key max min)
(apply-cmd "ZREVRANGEBYSCORE" (list key max min))
(get-response))
(define/public (zremrangebyrank key start stop)
(apply-cmd "ZREMRANGEBYSCORE" (list key start stop))
(get-response))
(define/public (zremrangebylex key min max)
(apply-cmd "ZREMRANGEBYLEX" (list key min max))
(get-response))
(define/public (zunionstore dest keys)
(apply-cmd "ZUNIONSTORE" (append (list dest) keys))
(get-response))
(define/public (hmset key data)
(apply-cmd "HMSET" (append (list key) data))
(get-response))
(define/public (hvals key)
(apply-cmd "HVALS" key)
(get-response))
(define/public (hdel key fields)
(apply-cmd "HDEL" (append (list key) fields))
(get-response))
(define/public (hsetnx key field value)
(apply-cmd "HSETNX" (list key field value))
(get-response))
(define/public (hget key field)
(apply-cmd "HGET" (list key field))
(get-response))
(define/public (hgetall key)
(apply-cmd "HGETALL" key)
(get-response))
(define/public (hincrby key field increment)
(apply-cmd "HINCRBY" (list key field increment))
(get-response))
(define/public (hexists key field)
(apply-cmd "HEXISTS" (list key field))
(get-response))
(define/public (hkeys key)
(apply-cmd "HKEYS" key)
(get-response))
(define/public (hlen key)
(apply-cmd "HLEN" key)
(get-response))
(define/public (concat key value)
(apply-cmd "APPEND" (list key value))
(get-response))
(define/public (strlen key)
(apply-cmd "STRLEN" key)
(get-response))
(define/public (bitcount key [start "0"] [end (number->string (string-length key))])
(apply-cmd "BITCOUNT" (list key start end))
(get-response))
(define/public (bitop operation destkey key)
(apply-cmd "BITOP" (append (list operation destkey) (if (list? key) key (list key))))
(get-response))
(define/public (bitpos key bit [start null] [end null])
(apply-cmd "BITPOS" (flatten (list key bit start end)))
(get-response))
(define/public (watch key)
(apply-cmd "WATCH" key)
(get-response))
(define/public (unwatch)
(apply-cmd "UNWATCH")
(get-response))
(define/public (getrange key start end)
(apply-cmd "GETRANGE" (list key start end))
(get-response))
(define/public (type key)
(apply-cmd "TYPE" key)
(get-response))
(define/public (keys pattern)
(apply-cmd "KEYS" pattern)
(get-response))
(define/public (randomkey)
(apply-cmd "RANDOMKEY")
(get-response))
(define/public (rename oldkey newkey)
(apply-cmd "RENAME" (list oldkey newkey))
(get-response))
(define/public (renamex oldkey newkey)
(apply-cmd "RENAMEX" (list oldkey newkey))
(get-response))
(define/public (config-get parameter)
(apply-cmd "CONFIG GET" parameter)
(get-response))
(define/public (config-set parameter value)
(apply-cmd "CONFIG SET" (list parameter value))
(get-response))
(define/public (config-rewrite)
(apply-cmd "CONFIG REWRITE")
(get-response))
(define/public (config-resetstat)
(apply-cmd "CONFIG RESETSTAT")
(get-response))
(define/public (dbsize)
(apply-cmd "DBSIZE")
(get-response))
(define/public (expire key seconds)
(apply-cmd "EXPIRE" (list key seconds))
(get-response))
(define/public (expireat key unixtime)
(apply-cmd "EXPIREAT" (list key unixtime))
(get-response))
(define/public (ttl key)
(apply-cmd "TTL" key)
(get-response))
(define/public (move key index)
(apply-cmd "MOVE" (list key index))
(get-response))
(define/public (flushdb)
(apply-cmd "FLUSHDB")
(get-response))
(define/public (flushall)
(apply-cmd "FLUSHALL")
(get-response))
(define/public (save)
(apply-cmd "SAVE")
(get-response))
(define/public (bgsave)
(apply-cmd "BGSAVE")
(get-response))
(define/public (lastsave)
(apply-cmd "LASTSAVE")
(get-response))
(define/public (bgrewriteaof)
(apply-cmd "BGREWRITEAOF")
(get-response))
(define/public (shutdown)
(apply-cmd "SHUTDOWN")
(get-response))
(define/public (info)
(apply-cmd "INFO")
(get-response))
(define/public (monitor)
(apply-cmd "MONITOR")
(get-response))
(define/public (object subcommand [args null])
(apply-cmd "OBJECT" (append (list subcommand) args))
(get-response))
(define/public (slaveof host port)
(apply-cmd "SLAVEOF" (list host port))
(get-response))
(define/public (subscribe channel)
(apply-cmd "SUBSCRIBE" channel)
(get-response))
(define/public (publish channel msg)
(apply-cmd "PUBLISH" (list channel msg))
(get-response))
(define/public (unsubscribe channel)
(apply-cmd "UNSUBSCRIBE" channel)
(get-response))
(define/public (psubscribe pattern)
(apply-cmd "PSUBSCRIBE" pattern)
(get-response))
(define/public (punsubscribe pattern)
(apply-cmd "PUNSUBSCRIBE" pattern)
(get-response))
(define/public (init)
(define-values (i o) (tcp-connect ip port))
(set! in i)
(set! out o))))