dynamoredis 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,51 @@
1
+ Metadata-Version: 2.4
2
+ Name: dynamoredis
3
+ Version: 0.1.0
4
+ Summary: A truly serverless Redis on your own AWS account: Lambda URL endpoint, DynamoDB storage, ≈ $0 when idle. redis-py API: from dynamoredis import redis.
5
+ Author: Frank Zhang
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/cloudformer/dynamoredis
8
+ Project-URL: Repository, https://github.com/cloudformer/dynamoredis
9
+ Project-URL: Deploy, https://github.com/cloudformer/dynamoredis#quick-start
10
+ Project-URL: Compatibility, https://github.com/cloudformer/dynamoredis/blob/main/tests/compat/REPORT.md
11
+ Project-URL: Changelog, https://github.com/cloudformer/dynamoredis/blob/main/CHANGELOG.md
12
+ Project-URL: Issues, https://github.com/cloudformer/dynamoredis/issues
13
+ Keywords: serverless,aws,lambda,dynamodb,redis,cache,key-value
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Topic :: Database
19
+ Requires-Python: >=3.9
20
+ Description-Content-Type: text/markdown
21
+
22
+ # dynamoredis
23
+
24
+ **Python client for [dynamoredis](https://github.com/cloudformer/dynamoredis) — a truly serverless Redis:
25
+ one DynamoDB table, a Lambda Function URL as the endpoint, ≈ $0 when idle.** The redis-py API, standard
26
+ library only. Part of [Redefine AWS Serverless](https://github.com/topics/redefine-aws-serverless).
27
+
28
+ ```bash
29
+ pip install dynamoredis
30
+ ```
31
+
32
+ ```python
33
+ from dynamoredis import redis # later: `import redis` against a real Redis, nothing else changes
34
+
35
+ r = redis.Redis(host="xxx.lambda-url.us-east-2.on.aws", username="admin", password="...")
36
+ r.set("greeting", "hello", ex=60)
37
+ r.get("greeting") # b"hello"
38
+ r.incr("hits") # atomic (DynamoDB ADD)
39
+ r.hset("user:1", mapping={"name": "alice", "age": 30})
40
+ r.zadd("board", {"alice": 10, "bob": 20}); r.zrange("board", 0, -1, withscores=True)
41
+ with r.pipeline(transaction=False) as p:
42
+ p.set("a", 1); p.get("a"); p.execute() # one request
43
+ ```
44
+
45
+ Strings, keys/TTL, hashes, lists, sets, sorted sets, pipelines. Not supported (raises `NotSupportedError`):
46
+ pub/sub, blocking commands, Lua, `MULTI`/`EXEC`/`WATCH`, streams — those need a server that holds
47
+ connections; that is the moment to move to a real Redis.
48
+
49
+ The endpoint comes from deploying the [main repository](https://github.com/cloudformer/dynamoredis) —
50
+ same as any client needs its server. Compatibility with redis-py against Redis 7:
51
+ [REPORT.md](https://github.com/cloudformer/dynamoredis/blob/main/tests/compat/REPORT.md).
@@ -0,0 +1,30 @@
1
+ # dynamoredis
2
+
3
+ **Python client for [dynamoredis](https://github.com/cloudformer/dynamoredis) — a truly serverless Redis:
4
+ one DynamoDB table, a Lambda Function URL as the endpoint, ≈ $0 when idle.** The redis-py API, standard
5
+ library only. Part of [Redefine AWS Serverless](https://github.com/topics/redefine-aws-serverless).
6
+
7
+ ```bash
8
+ pip install dynamoredis
9
+ ```
10
+
11
+ ```python
12
+ from dynamoredis import redis # later: `import redis` against a real Redis, nothing else changes
13
+
14
+ r = redis.Redis(host="xxx.lambda-url.us-east-2.on.aws", username="admin", password="...")
15
+ r.set("greeting", "hello", ex=60)
16
+ r.get("greeting") # b"hello"
17
+ r.incr("hits") # atomic (DynamoDB ADD)
18
+ r.hset("user:1", mapping={"name": "alice", "age": 30})
19
+ r.zadd("board", {"alice": 10, "bob": 20}); r.zrange("board", 0, -1, withscores=True)
20
+ with r.pipeline(transaction=False) as p:
21
+ p.set("a", 1); p.get("a"); p.execute() # one request
22
+ ```
23
+
24
+ Strings, keys/TTL, hashes, lists, sets, sorted sets, pipelines. Not supported (raises `NotSupportedError`):
25
+ pub/sub, blocking commands, Lua, `MULTI`/`EXEC`/`WATCH`, streams — those need a server that holds
26
+ connections; that is the moment to move to a real Redis.
27
+
28
+ The endpoint comes from deploying the [main repository](https://github.com/cloudformer/dynamoredis) —
29
+ same as any client needs its server. Compatibility with redis-py against Redis 7:
30
+ [REPORT.md](https://github.com/cloudformer/dynamoredis/blob/main/tests/compat/REPORT.md).
@@ -0,0 +1,8 @@
1
+ """dynamoredis — a truly serverless Redis: Lambda Function URL endpoint, DynamoDB storage, ≈ $0 when idle.
2
+
3
+ from dynamoredis import redis # later: `import redis` against a real Redis, nothing else changes
4
+ r = redis.Redis(host="xxx.lambda-url.us-east-2.on.aws", username="admin", password="...")
5
+ """
6
+ from . import redis # noqa: F401
7
+
8
+ __version__ = "0.1.0"
@@ -0,0 +1,564 @@
1
+ """dynamoredis.redis — the redis-py API (redis.Redis, from_url, pipeline, exceptions) over HTTP to the
2
+ dynamoredis Lambda URL. Standard library only.
3
+
4
+ from dynamoredis import redis
5
+ r = redis.Redis(host="xxx.lambda-url.us-east-2.on.aws", username="admin", password="...")
6
+ r.set("k", "v", ex=60)
7
+ r.get("k") # b"v" (decode_responses=True -> "v")
8
+ r.incr("hits")
9
+ r.hset("user:1", mapping={"name": "alice", "age": 30})
10
+ with r.pipeline(transaction=False) as p:
11
+ p.set("a", 1); p.get("a")
12
+ p.execute() # [True, b"1"]
13
+
14
+ Values are Redis strings: str / int / float / bytes in, bytes out (or str with decode_responses=True).
15
+ Errors are the redis-py classes: ResponseError (WRONGTYPE, ERR ...), ConnectionError, TimeoutError,
16
+ AuthenticationError; what needs a real server raises NotSupportedError (a ResponseError).
17
+
18
+ Retry policy: a request is re-sent once only when it provably never reached the server (an idle
19
+ keep-alive connection closed by the peer); a timeout raises TimeoutError with the result unknown.
20
+ """
21
+ import base64
22
+ import http.client
23
+ import json
24
+ import socket
25
+ import urllib.parse
26
+
27
+ __version__ = "0.1.0"
28
+
29
+
30
+ # ---------------- exceptions (redis-py hierarchy) ----------------
31
+
32
+ class RedisError(Exception): pass
33
+ class ConnectionError(RedisError): pass # noqa: A001
34
+ class TimeoutError(ConnectionError): pass # noqa: A001
35
+ class AuthenticationError(ConnectionError): pass
36
+ class ResponseError(RedisError): pass
37
+ class DataError(RedisError): pass
38
+ class NotSupportedError(ResponseError):
39
+ """Needs a real Redis server (pub/sub, blocking commands, Lua, MULTI/EXEC, streams)."""
40
+
41
+
42
+ class exceptions: # redis.exceptions.X, as in redis-py
43
+ RedisError, ConnectionError, TimeoutError = RedisError, ConnectionError, TimeoutError
44
+ AuthenticationError, ResponseError, DataError, NotSupportedError = AuthenticationError, ResponseError, DataError, NotSupportedError
45
+
46
+
47
+ class _NotDelivered(Exception):
48
+ def __init__(self, cause, reused):
49
+ super().__init__(cause)
50
+ self.reused = reused
51
+
52
+
53
+ # ---------------- value encoding ----------------
54
+
55
+ def _arg(v):
56
+ """Python value -> wire value (Redis strings; bytes that are not UTF-8 travel as {"$b": base64})."""
57
+ if isinstance(v, bool): # redis-py refuses bool too
58
+ raise DataError("Invalid input of type: 'bool'. Convert to a bytes, string, int or float first.")
59
+ if isinstance(v, (int, float, str)):
60
+ return v
61
+ if isinstance(v, (bytes, bytearray, memoryview)):
62
+ b = bytes(v)
63
+ try:
64
+ return b.decode("utf-8")
65
+ except UnicodeDecodeError:
66
+ return {"$b": base64.b64encode(b).decode()}
67
+ raise DataError(f"Invalid input of type: '{type(v).__name__}'. Convert to a bytes, string, int or float first.")
68
+
69
+
70
+ def _bytes(v):
71
+ if isinstance(v, dict) and "$b" in v:
72
+ return base64.b64decode(v["$b"])
73
+ return str(v).encode()
74
+
75
+
76
+ class Redis:
77
+ def __init__(self, host="localhost", port=None, db=0, password=None, username=None,
78
+ decode_responses=False, socket_timeout=30, **_ignored):
79
+ """Same signature as redis.Redis; host is the Lambda URL (with or without https://)."""
80
+ url = host if host.startswith("http") else "https://" + host
81
+ p = urllib.parse.urlparse(url.rstrip("/"))
82
+ self._host, self._base_path = p.netloc, p.path
83
+ self._auth = base64.b64encode(f"{username or ''}:{password or ''}".encode()).decode()
84
+ self.decode_responses = decode_responses
85
+ self.timeout = socket_timeout or 30
86
+ self._conn = None
87
+ self.last = {} # {"ms": server time, "cu": DynamoDB capacity units} of the last call
88
+
89
+ @classmethod
90
+ def from_url(cls, url, **kw):
91
+ """redis://user:password@host/0 or https://user:password@host/ — the host is the Lambda URL."""
92
+ p = urllib.parse.urlparse(url)
93
+ return cls(host="https://" + p.hostname + (p.path if p.scheme.startswith("http") else ""),
94
+ username=p.username, password=p.password, **kw)
95
+
96
+ # ---- transport ----
97
+
98
+ def close(self):
99
+ try:
100
+ if self._conn:
101
+ self._conn.close()
102
+ finally:
103
+ self._conn = None
104
+
105
+ def __enter__(self):
106
+ return self
107
+
108
+ def __exit__(self, *a):
109
+ self.close()
110
+
111
+ def _send(self, path, body):
112
+ reused = self._conn is not None
113
+ if self._conn is None:
114
+ self._conn = http.client.HTTPSConnection(self._host, timeout=self.timeout)
115
+ headers = {"content-type": "application/json", "authorization": f"Basic {self._auth}"}
116
+ try:
117
+ self._conn.request("POST", self._base_path + path, body, headers)
118
+ except (socket.timeout, TimeoutError):
119
+ raise
120
+ except (OSError, http.client.HTTPException) as e:
121
+ raise _NotDelivered(e, reused) from None
122
+ try:
123
+ resp = self._conn.getresponse()
124
+ except (http.client.RemoteDisconnected, http.client.BadStatusLine) as e:
125
+ raise _NotDelivered(e, reused) from None
126
+ raw = resp.read()
127
+ try:
128
+ data = json.loads(raw)
129
+ except ValueError:
130
+ data = {"error": raw[:200].decode(errors="replace")}
131
+ return resp.status, data
132
+
133
+ def _post(self, path, payload):
134
+ body = json.dumps(payload)
135
+ try:
136
+ status, data = self._send(path, body)
137
+ except _NotDelivered as e:
138
+ self.close()
139
+ if not e.reused:
140
+ raise ConnectionError(f"Error connecting to {self._host}: {e}") from None
141
+ status, data = self._send(path, body)
142
+ except (socket.timeout, TimeoutError) as e:
143
+ self.close()
144
+ raise TimeoutError(f"Timeout talking to {self._host}; result unknown ({e})") from None
145
+ self.last = {"ms": data.get("ms"), "cu": data.get("cu")}
146
+ if status == 401:
147
+ raise AuthenticationError("invalid username-password pair")
148
+ if status >= 400:
149
+ raise _error(data)
150
+ return data
151
+
152
+ def execute_command(self, cmd, *args):
153
+ """Run any command; the reply is the raw wire value (str / int / list / dict / None)."""
154
+ return self._post("/api/cmd", {"cmd": cmd, "args": [_arg(a) for a in args]})["result"]
155
+
156
+ # ---- result shaping ----
157
+
158
+ def _s(self, v):
159
+ """A Redis string reply."""
160
+ if v is None:
161
+ return None
162
+ b = _bytes(v)
163
+ return b.decode() if self.decode_responses else b
164
+
165
+ def _ls(self, v):
166
+ return [self._s(x) for x in v]
167
+
168
+ # ---- strings ----
169
+
170
+ def ping(self):
171
+ return self.execute_command("PING") == "PONG"
172
+
173
+ def set(self, name, value, ex=None, px=None, nx=False, xx=False, keepttl=False, get=False):
174
+ if get:
175
+ raise NotSupportedError("SET ... GET is not supported; use getset()")
176
+ opts = []
177
+ if ex is not None:
178
+ opts += ["EX", int(ex.total_seconds()) if hasattr(ex, "total_seconds") else int(ex)]
179
+ if px is not None:
180
+ opts += ["PX", int(px.total_seconds() * 1000) if hasattr(px, "total_seconds") else int(px)]
181
+ if nx:
182
+ opts.append("NX")
183
+ if xx:
184
+ opts.append("XX")
185
+ if keepttl:
186
+ opts.append("KEEPTTL")
187
+ return self.execute_command("SET", name, value, *opts)
188
+
189
+ def setex(self, name, time, value):
190
+ return self.set(name, value, ex=time)
191
+
192
+ def psetex(self, name, time_ms, value):
193
+ return self.set(name, value, px=time_ms)
194
+
195
+ def setnx(self, name, value):
196
+ return bool(self.execute_command("SETNX", name, value))
197
+
198
+ def get(self, name):
199
+ return self._s(self.execute_command("GET", name))
200
+
201
+ def getdel(self, name):
202
+ return self._s(self.execute_command("GETDEL", name))
203
+
204
+ def getset(self, name, value):
205
+ return self._s(self.execute_command("GETSET", name, value))
206
+
207
+ def mget(self, keys, *args):
208
+ keys = list(keys) if isinstance(keys, (list, tuple)) else [keys]
209
+ return self._ls(self.execute_command("MGET", *keys, *args))
210
+
211
+ def mset(self, mapping):
212
+ flat = [x for k, v in mapping.items() for x in (k, v)]
213
+ return self.execute_command("MSET", *flat) is True
214
+
215
+ def strlen(self, name):
216
+ return self.execute_command("STRLEN", name)
217
+
218
+ def append(self, key, value):
219
+ return self.execute_command("APPEND", key, value)
220
+
221
+ def incr(self, name, amount=1):
222
+ return self.execute_command("INCRBY", name, amount)
223
+
224
+ def incrby(self, name, amount=1):
225
+ return self.execute_command("INCRBY", name, amount)
226
+
227
+ def decr(self, name, amount=1):
228
+ return self.execute_command("DECRBY", name, amount)
229
+
230
+ def decrby(self, name, amount=1):
231
+ return self.execute_command("DECRBY", name, amount)
232
+
233
+ # ---- keys ----
234
+
235
+ def delete(self, *names):
236
+ return self.execute_command("DEL", *names)
237
+
238
+ def unlink(self, *names):
239
+ return self.execute_command("DEL", *names)
240
+
241
+ def exists(self, *names):
242
+ return self.execute_command("EXISTS", *names)
243
+
244
+ def expire(self, name, time):
245
+ secs = int(time.total_seconds()) if hasattr(time, "total_seconds") else int(time)
246
+ return bool(self.execute_command("EXPIRE", name, secs))
247
+
248
+ def pexpire(self, name, time):
249
+ ms = int(time.total_seconds() * 1000) if hasattr(time, "total_seconds") else int(time)
250
+ return bool(self.execute_command("PEXPIRE", name, ms))
251
+
252
+ def expireat(self, name, when):
253
+ ts = int(when.timestamp()) if hasattr(when, "timestamp") else int(when)
254
+ return bool(self.execute_command("EXPIREAT", name, ts))
255
+
256
+ def ttl(self, name):
257
+ return self.execute_command("TTL", name)
258
+
259
+ def pttl(self, name):
260
+ return self.execute_command("PTTL", name)
261
+
262
+ def persist(self, name):
263
+ return bool(self.execute_command("PERSIST", name))
264
+
265
+ def type(self, name):
266
+ return self._s(self.execute_command("TYPE", name))
267
+
268
+ def rename(self, src, dst):
269
+ return self.execute_command("RENAME", src, dst)
270
+
271
+ def keys(self, pattern="*"):
272
+ return self._ls(self.execute_command("KEYS", pattern))
273
+
274
+ def scan(self, cursor=0, match=None, count=None):
275
+ opts = []
276
+ if match is not None:
277
+ opts += ["MATCH", match]
278
+ if count is not None:
279
+ opts += ["COUNT", count]
280
+ nxt, found = self.execute_command("SCAN", cursor, *opts)
281
+ return (0 if nxt == "0" else nxt), self._ls(found)
282
+
283
+ def scan_iter(self, match=None, count=None):
284
+ cursor = 0
285
+ while True:
286
+ cursor, found = self.scan(cursor, match=match, count=count)
287
+ yield from found
288
+ if cursor == 0:
289
+ return
290
+
291
+ def dbsize(self):
292
+ return self.execute_command("DBSIZE")
293
+
294
+ def flushdb(self, asynchronous=False):
295
+ return self.execute_command("FLUSHDB")
296
+
297
+ def flushall(self, asynchronous=False):
298
+ return self.execute_command("FLUSHDB")
299
+
300
+ # ---- hashes ----
301
+
302
+ def hset(self, name, key=None, value=None, mapping=None, items=None):
303
+ flat = []
304
+ if key is not None:
305
+ flat += [key, value]
306
+ if mapping:
307
+ flat += [x for k, v in mapping.items() for x in (k, v)]
308
+ if items:
309
+ flat += list(items)
310
+ if not flat:
311
+ raise DataError("'hset' with no key value pairs")
312
+ return self.execute_command("HSET", name, *flat)
313
+
314
+ def hmset(self, name, mapping):
315
+ self.hset(name, mapping=mapping)
316
+ return True
317
+
318
+ def hsetnx(self, name, key, value):
319
+ return bool(self.execute_command("HSETNX", name, key, value))
320
+
321
+ def hget(self, name, key):
322
+ return self._s(self.execute_command("HGET", name, key))
323
+
324
+ def hmget(self, name, keys, *args):
325
+ keys = list(keys) if isinstance(keys, (list, tuple)) else [keys]
326
+ return self._ls(self.execute_command("HMGET", name, *keys, *args))
327
+
328
+ def hgetall(self, name):
329
+ return {self._s(k): self._s(v) for k, v in self.execute_command("HGETALL", name).items()}
330
+
331
+ def hkeys(self, name):
332
+ return self._ls(self.execute_command("HKEYS", name))
333
+
334
+ def hvals(self, name):
335
+ return self._ls(self.execute_command("HVALS", name))
336
+
337
+ def hlen(self, name):
338
+ return self.execute_command("HLEN", name)
339
+
340
+ def hexists(self, name, key):
341
+ return bool(self.execute_command("HEXISTS", name, key))
342
+
343
+ def hdel(self, name, *keys):
344
+ return self.execute_command("HDEL", name, *keys)
345
+
346
+ def hincrby(self, name, key, amount=1):
347
+ return self.execute_command("HINCRBY", name, key, amount)
348
+
349
+ # ---- lists ----
350
+
351
+ def lpush(self, name, *values):
352
+ return self.execute_command("LPUSH", name, *values)
353
+
354
+ def rpush(self, name, *values):
355
+ return self.execute_command("RPUSH", name, *values)
356
+
357
+ def lpushx(self, name, *values):
358
+ return self.execute_command("LPUSHX", name, *values)
359
+
360
+ def rpushx(self, name, *values):
361
+ return self.execute_command("RPUSHX", name, *values)
362
+
363
+ def lpop(self, name, count=None):
364
+ r = self.execute_command("LPOP", name) if count is None else self.execute_command("LPOP", name, count)
365
+ return self._s(r) if count is None else (None if r is None else self._ls(r))
366
+
367
+ def rpop(self, name, count=None):
368
+ r = self.execute_command("RPOP", name) if count is None else self.execute_command("RPOP", name, count)
369
+ return self._s(r) if count is None else (None if r is None else self._ls(r))
370
+
371
+ def lrange(self, name, start, end):
372
+ return self._ls(self.execute_command("LRANGE", name, start, end))
373
+
374
+ def llen(self, name):
375
+ return self.execute_command("LLEN", name)
376
+
377
+ def lindex(self, name, index):
378
+ return self._s(self.execute_command("LINDEX", name, index))
379
+
380
+ def lset(self, name, index, value):
381
+ return self.execute_command("LSET", name, index, value)
382
+
383
+ def ltrim(self, name, start, end):
384
+ return self.execute_command("LTRIM", name, start, end)
385
+
386
+ # ---- sets ----
387
+
388
+ def sadd(self, name, *values):
389
+ return self.execute_command("SADD", name, *values)
390
+
391
+ def srem(self, name, *values):
392
+ return self.execute_command("SREM", name, *values)
393
+
394
+ def smembers(self, name):
395
+ return set(self._ls(self.execute_command("SMEMBERS", name)))
396
+
397
+ def sismember(self, name, value):
398
+ return bool(self.execute_command("SISMEMBER", name, value))
399
+
400
+ def scard(self, name):
401
+ return self.execute_command("SCARD", name)
402
+
403
+ # ---- sorted sets ----
404
+
405
+ def zadd(self, name, mapping, nx=False, xx=False, ch=False, incr=False):
406
+ opts = [o for o, on in (("NX", nx), ("XX", xx), ("CH", ch), ("INCR", incr)) if on]
407
+ flat = [x for member, score in mapping.items() for x in (score, member)]
408
+ r = self.execute_command("ZADD", name, *opts, *flat)
409
+ return float(r) if incr and r is not None else r
410
+
411
+ def zincrby(self, name, amount, value):
412
+ return float(self.execute_command("ZINCRBY", name, amount, value))
413
+
414
+ def zscore(self, name, value):
415
+ r = self.execute_command("ZSCORE", name, value)
416
+ return None if r is None else float(r)
417
+
418
+ def zcard(self, name):
419
+ return self.execute_command("ZCARD", name)
420
+
421
+ def zrem(self, name, *values):
422
+ return self.execute_command("ZREM", name, *values)
423
+
424
+ def zrank(self, name, value):
425
+ return self.execute_command("ZRANK", name, value)
426
+
427
+ def zcount(self, name, min, max):
428
+ return self.execute_command("ZCOUNT", name, min, max)
429
+
430
+ def _zpairs(self, r, withscores, score_cast_func):
431
+ if withscores:
432
+ return [(self._s(m), score_cast_func(s)) for m, s in r]
433
+ return self._ls(r)
434
+
435
+ def zrange(self, name, start, end, desc=False, withscores=False, score_cast_func=float):
436
+ cmd = "ZREVRANGE" if desc else "ZRANGE"
437
+ r = self.execute_command(cmd, name, start, end, *(["WITHSCORES"] if withscores else []))
438
+ return self._zpairs(r, withscores, score_cast_func)
439
+
440
+ def zrevrange(self, name, start, end, withscores=False, score_cast_func=float):
441
+ return self.zrange(name, start, end, desc=True, withscores=withscores, score_cast_func=score_cast_func)
442
+
443
+ def zrangebyscore(self, name, min, max, start=None, num=None, withscores=False, score_cast_func=float):
444
+ opts = ["WITHSCORES"] if withscores else []
445
+ if start is not None and num is not None:
446
+ opts += ["LIMIT", start, num]
447
+ r = self.execute_command("ZRANGEBYSCORE", name, min, max, *opts)
448
+ return self._zpairs(r, withscores, score_cast_func)
449
+
450
+ # ---- pipeline ----
451
+
452
+ def pipeline(self, transaction=True, shard_hint=None):
453
+ if transaction:
454
+ raise NotSupportedError("MULTI/EXEC needs a server session; use pipeline(transaction=False) for batching")
455
+ return Pipeline(self)
456
+
457
+
458
+ class _Captured(Exception):
459
+ def __init__(self, cmd):
460
+ self.cmd = cmd
461
+
462
+
463
+ class _Recorder(Redis):
464
+ """A Redis whose execute_command captures the command instead of sending it."""
465
+ def __init__(self):
466
+ pass
467
+
468
+ def execute_command(self, cmd, *args):
469
+ raise _Captured([cmd, *[_arg(a) for a in args]])
470
+
471
+
472
+ _RECORDER = _Recorder()
473
+
474
+
475
+ class Pipeline:
476
+ """Queues commands (the same methods as Redis, each returns the pipeline) and sends them in one
477
+ request on execute(); replies are shaped like the single-call methods would shape them."""
478
+
479
+ def __init__(self, parent):
480
+ self._parent = parent
481
+ self._queue = []
482
+
483
+ def __getattr__(self, name):
484
+ method = getattr(Redis, name, None)
485
+ if method is None or name.startswith("_") or name in ("pipeline", "execute", "close"):
486
+ raise AttributeError(name)
487
+
488
+ def queue(*args, **kwargs):
489
+ try:
490
+ method(_RECORDER, *args, **kwargs)
491
+ except _Captured as c:
492
+ self._queue.append(c.cmd)
493
+ else:
494
+ raise NotSupportedError(f"{name} cannot be pipelined")
495
+ return self
496
+ return queue
497
+
498
+ def execute_command(self, cmd, *args):
499
+ self._queue.append([cmd, *[_arg(a) for a in args]])
500
+ return self
501
+
502
+ def __len__(self):
503
+ return len(self._queue)
504
+
505
+ def reset(self):
506
+ self._queue = []
507
+
508
+ def __enter__(self):
509
+ return self
510
+
511
+ def __exit__(self, *a):
512
+ self.reset()
513
+
514
+ def execute(self, raise_on_error=True):
515
+ if not self._queue:
516
+ return []
517
+ cmds, self._queue = self._queue, []
518
+ data = self._parent._post("/api/pipeline", {"cmds": cmds})
519
+ out = []
520
+ for c, r in zip(cmds, data["results"]):
521
+ if "error" in r:
522
+ err = _error(r)
523
+ if raise_on_error:
524
+ raise err
525
+ out.append(err)
526
+ else:
527
+ out.append(_shape(self._parent, c[0], r["result"]))
528
+ return out
529
+
530
+
531
+ def _shape(client, cmd, result):
532
+ """Shape a raw reply the way the single-call method would (bytes / sets / tuples / bools)."""
533
+ c = cmd.upper()
534
+ if c in ("GET", "GETDEL", "GETSET", "HGET", "LPOP", "RPOP", "LINDEX", "TYPE"):
535
+ return client._s(result) if not isinstance(result, list) else client._ls(result)
536
+ if c in ("MGET", "HMGET", "HKEYS", "HVALS", "LRANGE", "KEYS", "SMEMBERS"):
537
+ return set(client._ls(result)) if c == "SMEMBERS" else client._ls(result)
538
+ if c == "HGETALL":
539
+ return {client._s(k): client._s(v) for k, v in result.items()}
540
+ if c in ("SETNX", "EXPIRE", "PEXPIRE", "EXPIREAT", "PERSIST", "HEXISTS", "HSETNX", "SISMEMBER"):
541
+ return bool(result)
542
+ if c in ("ZRANGE", "ZREVRANGE", "ZRANGEBYSCORE"):
543
+ return [(client._s(m), float(s)) for m, s in result] if result and isinstance(result[0], list) else client._ls(result)
544
+ if c in ("ZSCORE", "ZINCRBY"):
545
+ return None if result is None else float(result)
546
+ if c == "PING":
547
+ return result == "PONG"
548
+ return result
549
+
550
+
551
+ def _error(data):
552
+ kind, msg = data.get("kind"), data.get("error", "")
553
+ if kind == "NotSupportedError":
554
+ return NotSupportedError(msg)
555
+ if kind == "ResponseError":
556
+ return ResponseError(msg)
557
+ return ConnectionError(msg)
558
+
559
+
560
+ def from_url(url, **kw):
561
+ return Redis.from_url(url, **kw)
562
+
563
+
564
+ StrictRedis = Redis
@@ -0,0 +1,51 @@
1
+ Metadata-Version: 2.4
2
+ Name: dynamoredis
3
+ Version: 0.1.0
4
+ Summary: A truly serverless Redis on your own AWS account: Lambda URL endpoint, DynamoDB storage, ≈ $0 when idle. redis-py API: from dynamoredis import redis.
5
+ Author: Frank Zhang
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/cloudformer/dynamoredis
8
+ Project-URL: Repository, https://github.com/cloudformer/dynamoredis
9
+ Project-URL: Deploy, https://github.com/cloudformer/dynamoredis#quick-start
10
+ Project-URL: Compatibility, https://github.com/cloudformer/dynamoredis/blob/main/tests/compat/REPORT.md
11
+ Project-URL: Changelog, https://github.com/cloudformer/dynamoredis/blob/main/CHANGELOG.md
12
+ Project-URL: Issues, https://github.com/cloudformer/dynamoredis/issues
13
+ Keywords: serverless,aws,lambda,dynamodb,redis,cache,key-value
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Topic :: Database
19
+ Requires-Python: >=3.9
20
+ Description-Content-Type: text/markdown
21
+
22
+ # dynamoredis
23
+
24
+ **Python client for [dynamoredis](https://github.com/cloudformer/dynamoredis) — a truly serverless Redis:
25
+ one DynamoDB table, a Lambda Function URL as the endpoint, ≈ $0 when idle.** The redis-py API, standard
26
+ library only. Part of [Redefine AWS Serverless](https://github.com/topics/redefine-aws-serverless).
27
+
28
+ ```bash
29
+ pip install dynamoredis
30
+ ```
31
+
32
+ ```python
33
+ from dynamoredis import redis # later: `import redis` against a real Redis, nothing else changes
34
+
35
+ r = redis.Redis(host="xxx.lambda-url.us-east-2.on.aws", username="admin", password="...")
36
+ r.set("greeting", "hello", ex=60)
37
+ r.get("greeting") # b"hello"
38
+ r.incr("hits") # atomic (DynamoDB ADD)
39
+ r.hset("user:1", mapping={"name": "alice", "age": 30})
40
+ r.zadd("board", {"alice": 10, "bob": 20}); r.zrange("board", 0, -1, withscores=True)
41
+ with r.pipeline(transaction=False) as p:
42
+ p.set("a", 1); p.get("a"); p.execute() # one request
43
+ ```
44
+
45
+ Strings, keys/TTL, hashes, lists, sets, sorted sets, pipelines. Not supported (raises `NotSupportedError`):
46
+ pub/sub, blocking commands, Lua, `MULTI`/`EXEC`/`WATCH`, streams — those need a server that holds
47
+ connections; that is the moment to move to a real Redis.
48
+
49
+ The endpoint comes from deploying the [main repository](https://github.com/cloudformer/dynamoredis) —
50
+ same as any client needs its server. Compatibility with redis-py against Redis 7:
51
+ [REPORT.md](https://github.com/cloudformer/dynamoredis/blob/main/tests/compat/REPORT.md).
@@ -0,0 +1,8 @@
1
+ README.md
2
+ pyproject.toml
3
+ dynamoredis/__init__.py
4
+ dynamoredis/redis.py
5
+ dynamoredis.egg-info/PKG-INFO
6
+ dynamoredis.egg-info/SOURCES.txt
7
+ dynamoredis.egg-info/dependency_links.txt
8
+ dynamoredis.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ dynamoredis
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "dynamoredis"
7
+ version = "0.1.0"
8
+ description = "A truly serverless Redis on your own AWS account: Lambda URL endpoint, DynamoDB storage, ≈ $0 when idle. redis-py API: from dynamoredis import redis."
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = { text = "MIT" }
12
+ authors = [{ name = "Frank Zhang" }]
13
+ keywords = ["serverless", "aws", "lambda", "dynamodb", "redis", "cache", "key-value"]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Programming Language :: Python :: 3",
19
+ "Topic :: Database",
20
+ ]
21
+ dependencies = [] # standard library only
22
+
23
+ [project.urls]
24
+ Homepage = "https://github.com/cloudformer/dynamoredis"
25
+ Repository = "https://github.com/cloudformer/dynamoredis"
26
+ Deploy = "https://github.com/cloudformer/dynamoredis#quick-start"
27
+ Compatibility = "https://github.com/cloudformer/dynamoredis/blob/main/tests/compat/REPORT.md"
28
+ Changelog = "https://github.com/cloudformer/dynamoredis/blob/main/CHANGELOG.md"
29
+ Issues = "https://github.com/cloudformer/dynamoredis/issues"
30
+
31
+ [tool.setuptools]
32
+ packages = ["dynamoredis"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+