forked from dieseldev/diesel
/
redis.py
1220 lines (991 loc) · 32.9 KB
/
redis.py
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from contextlib import contextmanager
from diesel import (Client, call, until_eol, receive,
fire, send, first)
from diesel.util.queue import Queue, QueueTimeout
import time
import operator as op
import itertools
import uuid
def flatten_arg_pairs(l):
o = []
for i in l:
o.extend(i)
return o
REDIS_PORT = 6379
class RedisError(Exception): pass
class RedisClient(Client):
def __init__(self, host='localhost', port=REDIS_PORT, **kw):
Client.__init__(self, host, port, **kw)
##################################################
### GENERAL OPERATIONS
@call
def exists(self, k):
self._send('EXISTS', k)
resp = self._get_response()
return bool(resp)
@call
def delete(self, k):
self._send('DEL', k)
resp = self._get_response()
return bool(resp)
@call
def type(self, k):
self._send('TYPE', k)
resp = self._get_response()
return resp
@call
def keys(self, pat):
self._send('KEYS', pat)
resp = self._get_response()
return set(resp)
@call
def randomkey(self):
self._send('RANDOMKEY')
resp = self._get_response()
return resp
@call
def rename(self, old, new):
self._send('RENAME', old, new)
resp = self._get_response()
return resp
@call
def renamenx(self, old, new):
self._send('RENAMENX', old, new)
resp = self._get_response()
return resp
@call
def dbsize(self):
self._send('DBSIZE')
resp = self._get_response()
return resp
@call
def expire(self, key, seconds):
self._send('EXPIRE', key, seconds)
resp = self._get_response()
return resp
@call
def expireat(self, key, when):
unix_time = time.mktime(when.timetuple())
self._send('EXPIREAT', key, unix_time)
resp = self._get_response()
return resp
@call
def ttl(self, key):
self._send('TTL', key)
resp = self._get_response()
resp = None if resp == -1 else resp
return resp
@call
def select(self, idx):
self._send('SELECT', idx)
resp = self._get_response()
return resp
@call
def move(self, key, idx):
self._send('MOVE', key, idx)
@call
def flushdb(self):
self._send('FLUSHDB')
resp = self._get_response()
return resp
@call
def flushall(self):
self._send('FLUSHALL')
resp = self._get_response()
return resp
##################################################
### TRANSACTION OPERATIONS
### http://redis.io/topics/transactions
@call
def multi(self):
"""Starts a transaction."""
self._send('MULTI')
return self._get_response()
@call
def exec_(self):
"""Atomically executes queued commands in a transaction."""
self._send('EXEC')
return self._get_response()
@call
def discard(self):
"""Discards any queued commands and aborts a transaction."""
self._send('DISCARD')
return self._get_response()
@call
def watch(self, keys):
"""Sets up keys to be watched in preparation for a transaction."""
self._send('WATCH', list=keys)
return self._get_response()
def transaction(self, watch=None):
"""Returns a RedisTransaction context manager.
If watch is supplied, it should be a list of keys to be watched for
changes. The transaction will be aborted if the value of any of the
keys is changed outside of the transaction.
A transaction can be invoked with Python's ``with`` statement for
atomically executing a series of commands.
>>> transaction = client.transaction(watch=['dependent_var_1'])
>>> dv1 = client.get('dependent_var_1')
>>> with transaction as t:
... composite_val = compute(dv1)
... t.set('dependent_var_2', composite_val)
>>> print t.value
"""
return RedisTransaction(self, watch or [])
##################################################
### STRING OPERATIONS
@call
def set(self, k, v):
self._send('SET', k, v)
resp = self._get_response()
return resp
@call
def get(self, k):
self._send('GET', k)
resp = self._get_response()
return resp
@call
def getset(self, k, v):
self._send('GETSET', k, v)
resp = self._get_response()
return resp
@call
def mget(self, keylist):
self._send('MGET', list=keylist)
resp = self._get_response()
return resp
@call
def setnx(self, k, v):
self._send('SETNX', k, v)
resp = self._get_response()
return resp
@call
def setex(self, k, tm, v):
self._send('SETEX', k, tm, v)
resp = self._get_response()
return resp
@call
def mset(self, d):
self._send('MSET', list=flatten_arg_pairs(d.iteritems()))
resp = self._get_response()
return resp
@call
def msetnx(self, d):
self._send('MSETNX', list=flatten_arg_pairs(d.iteritems()))
resp = self._get_response()
return resp
@call
def incr(self, k):
self._send('INCR', k)
resp = self._get_response()
return resp
@call
def incrby(self, k, amt):
self._send('INCRBY', k, amt)
resp = self._get_response()
return resp
@call
def decr(self, k):
self._send('DECR', k)
resp = self._get_response()
return resp
@call
def decrby(self, k, amt):
self._send('DECRBY', k, amt)
resp = self._get_response()
return resp
@call
def append(self, k, value):
self._send('APPEND', k, value)
resp = self._get_response()
return resp
@call
def substr(self, k, start, end):
self._send('SUBSTR', k, start, end)
resp = self._get_response()
return resp
@call
def getbit(self, k, offset):
self._send('GETBIT', k, offset)
resp = self._get_response()
return int(resp)
@call
def setbit(self, k, offset, value):
self._send('SETBIT', k, offset, value)
resp = self._get_response()
return resp
@call
def strlen(self, k):
self._send('STRLEN', k)
resp = self._get_response()
return int(resp)
##################################################
### LIST OPERATIONS
@call
def rpush(self, k, v):
self._send('RPUSH', k, v)
resp = self._get_response()
return resp
@call
def lpush(self, k, v):
self._send('LPUSH', k, v)
resp = self._get_response()
return resp
@call
def llen(self, k):
self._send('LLEN', k)
resp = self._get_response()
return resp
@call
def lrange(self, k, start, end):
self._send('LRANGE', k, start, end)
resp = self._get_response()
return resp
@call
def ltrim(self, k, start, end):
self._send('LTRIM', k, start, end)
resp = self._get_response()
return resp
@call
def lindex(self, k, idx):
self._send('LINDEX', k, idx)
resp = self._get_response()
return resp
@call
def lset(self, k, idx, v):
self._send('LSET', k, idx, v)
resp = self._get_response()
return resp
@call
def lrem(self, k, v, count=0):
self._send('LREM', k, count, v)
resp = self._get_response()
return resp
@call
def lpop(self, k):
self._send('LPOP', k)
resp = self._get_response()
return resp
@call
def rpop(self, k):
self._send('RPOP', k)
resp = self._get_response()
return resp
@call
def blpop(self, keylist, timeout=0):
self._send('BLPOP', list=list(keylist) + [timeout])
resp = self._get_response()
if resp:
assert len(resp) == 2
resp = tuple(resp)
else:
resp = None
return resp
@call
def brpop(self, keylist, timeout=0):
self._send('BRPOP', list=list(keylist) + [timeout])
resp = self._get_response()
if resp:
assert len(resp) == 2
resp = tuple(resp)
else:
resp = None
return resp
@call
def rpoplpush(self, src, dest):
self._send('RPOPLPUSH', src, dest)
resp = self._get_response()
return resp
##################################################
### SET OPERATIONS
@call
def sadd(self, k, v):
self._send('SADD', k, v)
resp = self._get_response()
return resp
@call
def srem(self, k, v):
self._send('SREM', k, v)
resp = self._get_response()
return bool(resp)
@call
def spop(self, k):
self._send('SPOP', k)
resp = self._get_response()
return resp
@call
def smove(self, src, dst, v):
self._send('SMOVE', src, dst, v)
resp = self._get_response()
return resp
@call
def scard(self, k):
self._send('SCARD', k)
resp = self._get_response()
return resp
@call
def sismember(self, k, v):
self._send('SISMEMBER', k, v)
resp = self._get_response()
return bool(resp)
@call
def sinter(self, keylist):
self._send('SINTER', list=keylist)
resp = self._get_response()
return set(resp)
@call
def sinterstore(self, dst, keylist):
flist = [dst] + list(keylist)
self._send('SINTERSTORE', list=flist)
resp = self._get_response()
return resp
@call
def sunion(self, keylist):
self._send('SUNION', list=keylist)
resp = self._get_response()
return set(resp)
@call
def sunionstore(self, dst, keylist):
flist = [dst] + list(keylist)
self._send('SUNIONSTORE', list=flist)
resp = self._get_response()
return resp
@call
def sdiff(self, keylist):
self._send('SDIFF', list=keylist)
resp = self._get_response()
return set(resp)
@call
def sdiffstore(self, dst, keylist):
flist = [dst] + list(keylist)
self._send('SDIFFSTORE', list=flist)
resp = self._get_response()
return resp
@call
def smembers(self, key):
self._send('SMEMBERS', key)
resp = self._get_response()
return set(resp)
@call
def srandmember(self, key):
self._send('SRANDMEMBER', key)
resp = self._get_response()
return resp
##################################################
### ZSET OPERATIONS
def __pair_with_scores(self, resp):
return [(resp[x], float(resp[x+1]))
for x in xrange(0, len(resp), 2)]
@call
def zadd(self, key, score, member):
self._send('ZADD', key, score, member)
resp = self._get_response()
return resp
@call
def zrem(self, key, member):
self._send('ZREM', key, member)
resp = self._get_response()
return bool(resp)
@call
def zrange(self, key, start, end):
self._send('ZRANGE', key, start, end)
resp = self._get_response()
return resp
@call
def zrevrange(self, key, start, end, with_scores=False):
args = 'ZREVRANGE', key, start, end
if with_scores:
args += 'WITHSCORES',
self._send(*args)
resp = self._get_response()
if with_scores:
return self.__pair_with_scores(resp)
return resp
@call
def zcard(self, key):
self._send('ZCARD', key)
resp = self._get_response()
return int(resp)
@call
def zscore(self, key, member):
self._send('ZSCORE', key, member)
resp = self._get_response()
return float(resp) if resp is not None else None
@call
def zincrby(self, key, increment, member):
self._send('ZINCRBY', key, increment, member)
resp = self._get_response()
return float(resp)
@call
def zrank(self, key, member):
self._send('ZRANK', key, member)
resp = self._get_response()
return resp
@call
def zrevrank(self, key, member):
self._send('ZREVRANK', key, member)
resp = self._get_response()
return resp
@call
def zrangebyscore(self, key, min, max, offset=None, count=None, with_scores=False):
args = 'ZRANGEBYSCORE', key, min, max
if offset:
assert count is not None, "if offset specified, count must be as well"
args += 'LIMIT', offset, count
if with_scores:
args += 'WITHSCORES',
self._send(*args)
resp = self._get_response()
if with_scores:
return self.__pair_with_scores(resp)
return resp
@call
def zcount(self, key, min, max):
self._send('ZCOUNT', key, min, max)
resp = self._get_response()
return resp
@call
def zremrangebyrank(self, key, min, max):
self._send('ZREMRANGEBYRANK', key, min, max)
resp = self._get_response()
return resp
@call
def zremrangebyscore(self, key, min, max):
self._send('ZREMRANGEBYSCORE', key, min, max)
resp = self._get_response()
return resp
##################################################
### HASH OPERATIONS
@call
def hset(self, key, field, value):
self._send('HSET', key, field, value)
resp = self._get_response()
return bool(resp)
@call
def hget(self, key, field):
self._send('HGET', key, field)
resp = self._get_response()
return resp
@call
def hmset(self, key, d):
if not d:
return True
args = [key] + flatten_arg_pairs(d.iteritems())
self._send('HMSET', list=args)
resp = self._get_response()
return bool(resp)
@call
def hmget(self, key, l):
if not l:
return {}
args = [key] + l
self._send('HMGET', list=args)
resp = self._get_response()
return dict(zip(l, resp))
@call
def hincrby(self, key, field, amt):
self._send('HINCRBY', key, field, amt)
resp = self._get_response()
return resp
@call
def hexists(self, key, field):
self._send('HEXISTS', key, field)
resp = self._get_response()
return bool(resp)
@call
def hdel(self, key, field):
self._send('HDEL', key, field)
resp = self._get_response()
return bool(resp)
@call
def hlen(self, key):
self._send('HLEN', key)
resp = self._get_response()
return resp
@call
def hkeys(self, key):
self._send('HKEYS', key)
resp = self._get_response()
return set(resp)
@call
def hvals(self, key):
self._send('HVALS', key)
resp = self._get_response()
return resp
@call
def hgetall(self, key):
self._send('HGETALL', key)
resp = self._get_response()
return dict(resp[x:x+2] for x in xrange(0, len(resp), 2))
##################################################
### Sorting...
@call
def sort(self, key, pattern=None, limit=None,
get=None, order='ASC', alpha=False, store=None):
args = [key]
if pattern:
args += ['BY', pattern]
if limit:
args += ['LIMIT'] + list(limit)
if get:
args += ['GET', get]
args += [order]
if alpha:
args += 'ALPHA'
if store:
args += ['STORE', store]
self._send('SORT', *args)
resp = self._get_response()
return resp
@call
def subscribe(self, *channels):
'''Subscribe to the given channels.
Note: assumes subscriptions succeed
'''
self._send('SUBSCRIBE', *channels)
return None
@call
def unsubscribe(self, *channels):
'''Unsubscribe from the given channels, or all of them if none are given.
Note: assumes subscriptions don't succeed
'''
self._send('UNSUBSCRIBE', *channels)
return None
@call
def psubscribe(self, *channels):
'''Subscribe to the given glob pattern-matched channels.
Note: assumes subscriptions succeed
'''
self._send('PSUBSCRIBE', *channels)
return None
@call
def punsubscribe(self, *channels):
'''Unsubscribe from the given glob pattern-matched channels, or all of them if none are given.
Note: assumes subscriptions don't succeed
'''
self._send('PUNSUBSCRIBE', *channels)
return None
@call
def get_from_subscriptions(self, wake_sig=None):
'''Wait for a published message on a subscribed channel.
Returns a tuple consisting of:
* The subscription pattern which matched
(the same as the channel for non-glob subscriptions)
* The channel the message was received from.
* The message itself.
-- OR -- None, if wake_sig was fired
NOTE: The message will always be a string. Handle this as you see fit.
NOTE: subscribe/unsubscribe acks are ignored here
'''
while True:
r = self._get_response(wake_sig)
if r:
if r[0] == 'message':
return [r[1]] + r[1:]
elif r[0] == 'pmessage':
return r[1:]
else:
return None
@call
def publish(self, channel, message):
'''Publish a message on the given channel.
Returns the number of clients that received the message.
'''
self._send('PUBLISH', channel, message)
resp = self._get_response()
return resp
@call
def send_raw_command(self, arguments):
cmd, rest = arguments[0], arguments[1:]
self._send(cmd, list=rest)
line_one = until_eol()
if line_one[0] in ('+', '-', ':'):
return line_one
if line_one[0] == '$':
amt = int(line_one[1:])
if amt == -1:
return line_one
return line_one + receive(amt) + until_eol()
if line_one[0] == '*':
nargs = int(line_one[1:])
if nargs == -1:
return line_one
out = line_one
for x in xrange(nargs):
head = until_eol()
out += head
out += receive(int(head[1:])) + until_eol()
return out
def _send(self, cmd, *args, **kwargs):
if 'list' in kwargs:
args = kwargs['list']
all = (cmd,) + tuple(str(s) for s in args)
send('*%s\r\n' % len(all))
for i in all:
send(('$%s\r\n' % len(i)) + i + '\r\n')
def _get_response(self, wake_sig=None):
if wake_sig:
ev, val = first(until_eol=True, waits=[wake_sig])
if ev != 'until_eol':
return None
fl = val.strip()
else:
fl = until_eol().strip()
c = fl[0]
if c == '+':
return fl[1:]
elif c == '$':
l = int(fl[1:])
if l == -1:
resp = None
else:
resp = receive(l)
until_eol() # noop
return resp
elif c == '*':
count = int(fl[1:])
resp = []
if count == -1:
return None
for x in xrange(count):
hl = until_eol()
assert hl[0] in ['$', ':', '+']
if hl[0] == '$':
l = int(hl[1:])
if l == -1:
resp.append(None)
else:
resp.append(receive(l))
until_eol() # noop
elif hl[0] == ':':
resp.append(int(hl[1:]))
elif hl[0] == '+':
resp.append(hl[1:].strip())
return resp
elif c == ':':
return int(fl[1:])
elif c == '-':
e_message = fl[1:]
raise RedisError(e_message)
class RedisTransaction(object):
"""A context manager for doing transactions with a RedisClient."""
def __init__(self, client, watch_keys):
"""Returns a new RedisTransaction instance.
The client argument should be a RedisClient instance and watch_keys
should be a list of keys to watch.
Handles calling the Redis WATCH, MULTI, EXEC and DISCARD commands to
manage transactions. Calls WATCH to watch keys for changes, MULTI to
start the transaction, EXEC to complete it or DISCARD to abort if there
was an exception.
Instances proxy method calls to the client instance. If the transaction
is successful, the value attribute will contain the results.
See http://redis.io/topics/transactions for more details.
"""
self.client = client
self.value = None
self.watching = watch_keys
self.aborted = False
if watch_keys:
self.client.watch(watch_keys)
def __getattr__(self, name):
return getattr(self.client, name)
def __enter__(self):
# Begin the transaction.
self.client.multi()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if any([exc_type, exc_val, exc_tb]):
# There was an error. Abort the transaction.
self.client.discard()
self.aborted = True
else:
# Try and execute the transaction.
self.value = self.client.exec_()
if self.value is None:
self.aborted = True
msg = 'A watched key changed before the transaction completed.'
raise RedisTransactionError(msg)
# Instruct Python not to swallow exceptions generated in the
# transaction block.
return False
class RedisTransactionError(Exception): pass
#########################################
## Hub, an abstraction of sub behavior, etc
class RedisSubHub(object):
def __init__(self, host='127.0.0.1', port=REDIS_PORT):
self.host = host
self.port = port
self.sub_wake_signal = uuid.uuid4().hex
self.sub_adds = []
self.sub_rms = []
self.subs = {}
def make_client(self):
client = RedisClient(self.host, self.port)
return client
def __isglob(self, glob):
return '*' in glob or '?' in glob or ('[' in glob and ']' and glob)
def __call__(self):
with self.make_client() as conn:
subs = self.subs
for sub in subs:
if self.__isglob(sub):
conn.psubscribe(sub)
else:
conn.subscribe(sub)
while True:
new = rm = None
if self.sub_adds:
sa = self.sub_adds[:]
self.sub_adds = []
new_subs, new_glob_subs = set(), set()
for k, q in sa:
new = new_glob_subs if self.__isglob(k) else new_subs
if k not in subs:
new.add(k)
subs[k] = set([q])
else:
subs[k].add(q)
if new_subs:
conn.subscribe(*new_subs)
if new_glob_subs:
conn.psubscribe(*new_glob_subs)
if self.sub_rms:
sr = self.sub_rms[:]
self.sub_rms = []
rm_subs, rm_glob_subs = set(), set()
for k, q in sr:
rm = rm_glob_subs if self.__isglob(k) else rm_subs
subs[k].remove(q)
if not subs[k]:
del subs[k]
rm.add(k)
if rm_subs:
conn.unsubscribe(*rm_subs)
if rm_glob_subs:
conn.punsubscribe(*rm_glob_subs)
if not self.sub_rms and not self.sub_adds:
r = conn.get_from_subscriptions(self.sub_wake_signal)
if r:
cls, key, msg = r
if cls in subs:
for q in subs[cls]:
q.put((key, msg))
@contextmanager
def sub(self, classes):
if type(classes) not in (set, list, tuple):
classes = [classes]
hb = self
q = Queue()
class Poller(object):
def __init__(self):
for cls in classes:
hb.sub_adds.append((cls, q))
fire(hb.sub_wake_signal)
def fetch(self, timeout=None):
try:
qn, msg = q.get(timeout=timeout)
except QueueTimeout:
return (None, None)
else:
return (qn, msg)
def close(self):
for cls in classes:
hb.sub_rms.append((cls, q))
pl = Poller()
try:
yield pl
finally:
pl.close()
if __name__ == '__main__':
from diesel import Application, Loop
a = Application()
def do_set():
r = RedisClient()
r.select(11)
r.flushdb()
print '--BASIC--'
assert r.get('newdb') == None
r.set('newdb', '1')
r.set('foo3', 'bar')
assert r.exists('foo3')
r.delete('foo3')
assert not r.exists('foo3')
for x in xrange(5000):
r.set('foo', 'bar')
assert r.get('foo') == 'bar'
assert r.get('foo2') == None
assert r.exists('foo') == True
assert r.exists('foo2') == False
assert r.type('foo') == 'string'
assert r.type('foo2') == 'none'
assert r.keys('fo*') == set(['foo'])
assert r.keys('bo*') == set()
assert r.randomkey()
r.rename('foo', 'bar')
assert r.get('foo') == None
assert r.get('bar') == 'bar'