-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisdict.py
179 lines (154 loc) · 4.87 KB
/
redisdict.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
from dbase import *
import random, string
import contextlib
from utils import *
POP_ITEM_LUA_SCRIPT = """
local addr = KEYS[1]
local taddr = KEYS[2]
local k = ARGV[1]
v = redis.call('hget', addr, k)
redis.call('hdel', addr, k)
redis.call('hdel', taddr, k)
return v
"""
class RedisDict(dbase):
def __init__(self, _dict=None):
dbase.__init__(self)
if _dict:
self.update(_dict)
def initialize(self):
self._type_addr_ = "_type_" + self._addr_
self._type_ = "dmem:dict"
self.cache = None
def _load_objects_and_types(self):
objdict = self.client.hgetall(self._addr_)
tdict = self.client.hgetall(self._type_addr_)
return objdict, tdict
def _load(self):
od, td = self._load_objects_and_types()
_dict = {}
for key in od:
obj = od[key]
t = td[key]
v = get_value_from_object_and_type(obj, t)
_dict[key] = v
return _dict
def destroy(self):
dbase.destroy(self)
self.client.delete(self._type_addr_)
@contextlib.contextmanager
def loaded(self):
self.cache = self._load()
try:
yield self.cache
finally:
self.cache = None
def __iter__(self):
if self.cache:
return iter(self.cache)
return iter(self.keys())
def __contains__(self, key):
if self.cache:
return key in self.cache
return self.client.hexists(self._addr_, key)
def __len__(self):
if self.cache:
return len(self.cache)
return self.client.hlen(self._addr_)
def __getitem__(self, key):
if self.cache:
return self.cache[key]
with self.client.pipeline() as pipe:
pipe.hget(self._addr_, key)
pipe.hget(self._type_addr_, key)
[obj, t] = pipe.execute()
v = get_value_from_object_and_type(obj, t)
return v
def __setitem__(self, key, value):
if not isinstance(key, basestring):
raise KeyError("Only string key is supported")
if self.cache:
self.cache[key] = value
obj, t = get_redis_object_and_type(value)
with self.client.pipeline() as pipe:
pipe.hset(self._addr_, key, obj)
pipe.hset(self._type_addr_, key, t)
pipe.execute()
def __delitem__(self, key):
if self.cache:
del self.cache[key]
with self.client.pipeline() as pipe:
pipe.hdel(self._addr_, key)
pipe.hdel(self._type_addr_, key)
pipe.execute()
def clear(self):
if self.cache:
self.cache = {}
self.client.delete(self._addr_)
self.client.delete(self._type_addr_)
def copy(self):
# shallow copy of self
return RedisDict(self._load())
@classmethod
def fromkeys(cls, keys, val):
d = {key:val for key in keys}
return RedisDict(d)
def get(self, key, default=None):
v = self.__getitem__(key)
if not v:
return default
return v
def has_key(self, key):
return key in self
def items(self):
if self.cache:
return self.cache.items()
d = self._load()
return d.items()
def keys(self):
if self.cache:
return self.cache.keys()
return self.client.hkeys(self._addr_)
def values(self):
if self.cache:
return self.cache.values()
with self.client.pipeline() as pipe:
pipe.hvals(self._addr_)
pipe.hvals(self._type_addr_)
objs, ts = pipe.execute()
return [get_value_from_object_and_type(obj, t) for obj, t in zip(objs, ts)]
# Doesn't seem necessary to implement iterator
def iteritems(self):
return self.items()
def iterkeys(self):
return self.keys()
def itervalues(self):
return self.values()
def update(self, updates):
if self.cache:
self.cache.update(updates)
if hasattr(updates, "items"): # has to be a list [(k, v), (k, v) ...]
updates = updates.items()
objdict ={}
tdict = {}
for k, v in updates:
obj, t = get_redis_object_and_type(v)
objdict[k] = obj
tdict[k] = t
with self.client.pipeline() as pipe:
pipe.hmset(self._addr_, objdict)
pipe.hmset(self._type_addr_, tdict)
pipe.execute()
def setdefault(self, k, d):
if self.cache:
self.cache.setdefault(k, d)
if self.has_key(k):
return self.__getitem__(k)
else:
self.__setitem__(k, d)
return d
def pop(self, k, d=None):
if self.cache:
self.cache.pop(k)
v = self.client.eval(POP_ITEM_LUA_SCRIPT, 2, self._addr_, self._type_addr_, k)
return v