-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisstr.py
84 lines (62 loc) · 1.98 KB
/
redisstr.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
from dbase import *
import random, string
import contextlib
from utils import *
class RedisStr(dbase):
def __init__(self, s=None):
dbase.__init__(self)
# save when initializing
if s:
ret = self.client.set(self._addr_, s)
if not ret:
raise RedisOperationFailure()
def initialize(self):
self._type_ = "dmem:str"
def getvalue(self):
# refresh the value
s = self.client.get(self._addr_)
return s
def setvalue(self, s):
ret = self.client.set(self._addr_, s)
if not ret:
raise RedisOperationFailure()
def __iadd__(self, more):
if isinstance(more, RedisStr):
more = more.getvalue()
if not isinstance(more, basestring):
raise TypeError("The argument is not a string")
ret = self.client.append(self._addr_, more)
if not ret:
raise RedisOperationFailure()
return self
def __add__(self, more):
# return a new RedisStr
s = self.getvalue()
if isinstance(more, RedisStr):
more = more.getvalue()
news = RedisStr(s+more)
return news
def __str__(self):
return self.getvalue()
def __contains__(self, sub):
return sub in self.getvalue()
def __getslice__(self, i, j):
ret = self.client.getrange(self._addr_, i, j)
if not ret:
raise RedisOperationFailure()
return ret
def __setslice__(self, i, j, val):
ret = self.client.setrange(self._addr_, i, j, val)
if not ret:
raise RedisOperationFailure()
def __len__(self):
ret = self.client.strlen(self._addr_)
return ret
def __getitem__(self, idx):
s = self.getvalue()
return s[idx]
def __getattr__(self, attr):
# delegate everything else to string value
s = self.getvalue()
if hasattr(s, attr):
return getattr(s, attr)