forked from redis-collections/redis-collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncable.py
299 lines (231 loc) · 8.72 KB
/
syncable.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
"""
syncable
~~~~~~~~
Persistent Python collections that can be written to and read from Redis.
The collections are kept in memory, so their operations run as fast as their
standard counterparts'.
Use in a ``with`` block to automatically sync to Redis after the block
executes, or call the :func:`sync` method explicitly.
>>> with SyncableDict() as D:
... D['one'] = 1
...
>>> D # Contents are available locally and are stored in Redis
{'one': 1}
>>> D['two'] = 2 # Changes are available locally, but not in Redis...
>>> D.sync() # ...until sync is called.
If you specify a ``key`` pointing to an existing collection, its contents will
be loaded.
>>> D.key
'f4a78a6faacb4d8e97829f9888ac6740'
>>> E = SyncableDict(key='f4a78a6faacb4d8e97829f9888ac6740')
>>> E
{'one': 1, 'two': 2}
"""
import collections.abc as collections_abc
import collections
from .dicts import Counter, DefaultDict, Dict
from .lists import Deque, List
from .sets import Set
class _SyncableBase:
@property
def redis(self):
return self.persistence.redis
@property
def key(self):
return self.persistence.key
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.sync()
class SyncableDict(_SyncableBase, dict):
"""
:class:`dict` subclass whose contents can be synced to Redis.
See Python's `dict documentation
<https://docs.python.org/3/library/stdtypes.html#mapping-types-dict>`_ for
details.
"""
def __init__(self, **kwargs):
self.persistence = Dict(**kwargs)
super().__init__()
self.update(self.persistence)
def sync(self):
self.persistence.clear()
self.persistence.update(self)
class SyncableCounter(_SyncableBase, collections.Counter):
"""
:class:`collections.Counter` subclass whose contents can be synced to
Redis.
See Python's `Counter documentation
<https://docs.python.org/3/library/collections.html#collections.Counter>`_
for details.
"""
def __init__(self, **kwargs):
self.persistence = Counter(**kwargs)
super().__init__()
self.update(self.persistence)
def sync(self):
self.persistence.clear()
self.persistence.update(self)
class SyncableDefaultDict(_SyncableBase, collections.defaultdict):
"""
:class:`collections.defaultdict` subclass whose contents can be synced to
Redis.
See Python's `defaultdict documentation
<https://docs.python.org/3/library/collections.html
#collections.defaultdict>`_ for details.
"""
def __init__(self, *args, **kwargs):
self.persistence = DefaultDict(*args, **kwargs)
super().__init__(args[0] if args else None)
self.update(self.persistence)
def sync(self):
self.persistence.clear()
self.persistence.update(self)
class SyncableList(_SyncableBase, list):
"""
:class:`list` subclass whose contents can be synced to Redis.
See Python's `list documentation
<https://docs.python.org/3/library/stdtypes.html
#sequence-types-list-tuple-range>`_ for details.
"""
def __init__(self, **kwargs):
self.persistence = List(**kwargs)
super().__init__()
self.extend(self.persistence)
def sync(self):
self.persistence.clear()
self.persistence.extend(self)
class SyncableDeque(_SyncableBase, collections.deque):
"""
:class:`deque` subclass whose contents can be synced to Redis.
See Python's `deque documentation
<https://docs.python.org/3/library/collections.html#collections.deque>`_
for details.
"""
def __init__(self, iterable=None, maxlen=None, **kwargs):
self.persistence = Deque(iterable=iterable, maxlen=maxlen, **kwargs)
super().__init__(maxlen=self.persistence.maxlen)
self.extend(self.persistence)
def sync(self):
self.persistence.clear()
self.persistence.extend(self)
class SyncableSet(_SyncableBase, set):
"""
:class:`set` subclass whose contents can be synced to Redis.
See Python's `set documentation
<https://docs.python.org/3/library/stdtypes.html
#set-types-set-frozenset>`_ for details.
"""
def __init__(self, **kwargs):
self.persistence = Set(**kwargs)
super().__init__()
self.update(self.persistence)
def sync(self):
self.persistence.clear()
self.persistence.update(self)
class LRUDict(_SyncableBase, collections_abc.MutableMapping):
"""
:class:`dict`-like class that stores recently-used items in memory
and automatically pushes older items to Redis.
Useful for constraining a Python process's memory footprint when a Redis
server with lots of memory is available.
>>> D = LRUDict(maxsize=2)
>>> D['a'] = 1 # 'a' is stored locally
>>> D['b'] = 2 # 'b' is stored locally
>>> D['c'] = 2 # 'a' is pushed to Redis and 'c' is stored locally
>>> assert D['a'] == 1 # 'a' is retrieved from Redis and replaces 'b'
>>> assert D['b'] == 2 # 'b' is retrieved from Redis and replaces 'c'
>>> D['b'] = -2
>>> D.sync() # All items are copied to Redis
"""
def __init__(self, maxsize=None, **kwargs):
"""
:param maxsize: The number of items to keep in memory.
:type maxsize: integer
:param redis: Redis client instance. If not provided, default Redis
connection is used.
:type redis: :class:`redis.StrictRedis`
:param key: Redis key for the collection. Collections with the same key
point to the same data. If not provided, a random
string is generated.
:type key: str
"""
self.maxsize = None if (maxsize is None) else int(maxsize)
self.cache = collections.OrderedDict()
self.persistence = Dict(**kwargs)
super().__init__()
self.update(self.persistence)
def __contains__(self, key):
return (key in self.cache) or (key in self.persistence)
def __delitem__(self, key):
# Remove the given key from the local cache and the Redis cache
for D in (self.cache, self.persistence):
try:
del D[key]
except KeyError:
pass
def __getitem__(self, key):
# If the item is in the local cache, grab its value and re-insert it
# as the rightmost (i.e. most recently used) item.
# Otherwise, try to get out of Redis and then insert it as the
# rightmost item.
try:
value = self.cache.pop(key)
except KeyError:
value = self.persistence.pop(key)
self[key] = value
return value
def __iter__(self):
local_keys = set(self.cache.keys())
persistence_keys = set(self.persistence.keys())
return iter(local_keys | persistence_keys)
def __len__(self):
return sum(1 for k in self)
def __setitem__(self, key, value):
# If the key is in the local cache, remove it and re-insert it with
# the new value as the rightmost (i.e. most recently used) item.
# Otherwise, we'll add it to the local cache. If the local cache
# is full we'll remove the leftmost (i.e. least recently used) item
# and insert the new values as the rightmost item.
try:
self.cache.pop(key)
except KeyError:
if (self.maxsize is not None) and len(self.cache) >= self.maxsize:
old_key, old_value = self.cache.popitem(last=False)
self.persistence[old_key] = old_value
self.cache[key] = value
def clear(self):
"""
Removes all items from both the local cache and the collection's Redis
key.
"""
self.cache.clear()
self.persistence.clear()
def copy(self, key=None):
"""
Creates another collection with the same items and maxsize with
the given *key*.
"""
other = self.__class__(
maxsize=self.maxsize, redis=self.persistence.redis, key=key
)
other.update(self)
return other
@classmethod
def fromkeys(cls, seq, value=None, **kwargs):
"""
Create a new collection with keys from *seq* and values set to
*value*. The keyword arguments are passed to the persistent ``Dict``.
"""
other = cls(**kwargs)
other.update(((key, value) for key in seq))
return other
def sync(self, clear_cache=False):
"""
Copy items from the local cache to the persistent Dict.
If *clear_cache* is ``True``, clear out the local cache after
pushing its items to Redis.
"""
self.persistence.update(self)
if clear_cache:
self.cache.clear()