Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24607aa5f7 | ||
|
|
c7deeca5a7 | ||
|
|
52864e513a | ||
|
|
ea1e3620e6 | ||
|
|
f0b002a4b3 | ||
|
|
4688dd4181 |
@@ -4,7 +4,7 @@ Async Redis wrapper over redis-py's asyncio client — a small, config-free, **f
|
|||||||
key/value + hash + ttl surface with a raw escape hatch for everything else. First of the
|
key/value + hash + ttl surface with a raw escape hatch for everything else. First of the
|
||||||
datastore trio (`redis` / `psql` / `mysql`), a sibling of the `mongo` lib.
|
datastore trio (`redis` / `psql` / `mysql`), a sibling of the `mongo` lib.
|
||||||
|
|
||||||
> **Import name ≠ repo name.** The repo/distribution is **`redis`**, but you import
|
> **Import name ≠ repo name.** The repo is **`redis`**, the distribution is
|
||||||
> **`redis_store`** — the driver package owns the `redis` import namespace, so the lib
|
> **`redis_store`** — the driver package owns the `redis` import namespace, so the lib
|
||||||
> can't also be `redis`. Install resolves `redis.git`; code does
|
> can't also be `redis`. Install resolves `redis.git`; code does
|
||||||
> `from redis_store import RedisDB`.
|
> `from redis_store import RedisDB`.
|
||||||
@@ -14,19 +14,19 @@ datastore trio (`redis` / `psql` / `mysql`), a sibling of the `mongo` lib.
|
|||||||
`requirements.txt`:
|
`requirements.txt`:
|
||||||
|
|
||||||
```
|
```
|
||||||
redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.2
|
redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v1.0.0
|
||||||
```
|
```
|
||||||
|
|
||||||
Direct:
|
Direct:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install "redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.2"
|
pip install "redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v1.0.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
Pulls `redis>=5` (redis-py, which ships the asyncio client — **not** the dead standalone
|
Pulls `redis>=5.0.1` (redis-py, which ships the asyncio client — **not** the dead standalone
|
||||||
`aioredis`).
|
`aioredis`; `5.0.1` is the floor because `5.0.0` lacks the async client's `aclose()`).
|
||||||
|
|
||||||
Drop the `@v0.1.2` suffix from the line above to install the latest unpinned.
|
Drop the `@v1.0.0` suffix from the line above to install the latest unpinned.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@@ -55,6 +55,8 @@ async with RedisDB(host="localhost") as kv:
|
|||||||
```
|
```
|
||||||
|
|
||||||
One client/pool per process — build it once, attach it to your app (`app.kv = ...`), share it.
|
One client/pool per process — build it once, attach it to your app (`app.kv = ...`), share it.
|
||||||
|
A failed `connect()`/`__aenter__` ping disconnects the pool before re-raising, so a
|
||||||
|
discarded `RedisDB` from a failed `async with` doesn't hold a live pool.
|
||||||
|
|
||||||
## Type contract
|
## Type contract
|
||||||
|
|
||||||
@@ -63,8 +65,14 @@ One client/pool per process — build it once, attach it to your app (`app.kv =
|
|||||||
Counters and counts (`incr`/`decr`/`exists`/`ttl`) always return `int` regardless.
|
Counters and counts (`incr`/`decr`/`exists`/`ttl`) always return `int` regardless.
|
||||||
|
|
||||||
Pool sizing/timeout are `max_connections=` and `pool_timeout=` (how long a caller waits for
|
Pool sizing/timeout are `max_connections=` and `pool_timeout=` (how long a caller waits for
|
||||||
a free connection when the pool is saturated). Pass the driver's own `timeout=` in
|
a free connection when the pool is saturated; `Optional[float]`, `None` waits forever).
|
||||||
`pool_kwargs` and construction raises a clear `ValueError` — use `pool_timeout=`.
|
Pass the driver's own `timeout=` in `pool_kwargs` and construction raises a clear
|
||||||
|
`ValueError` — use `pool_timeout=`.
|
||||||
|
|
||||||
|
`ssl=True` maps to `connection_class=redis.asyncio.SSLConnection`, so TLS works via the
|
||||||
|
documented `pool_kwargs` path (the default connection class has no `ssl` parameter and
|
||||||
|
would otherwise defer a `TypeError` to `connect()`). Pass ssl-prefixed kwargs alongside
|
||||||
|
it (`ssl_ca_certs`, `ssl_certfile`, ...) to configure the handshake.
|
||||||
|
|
||||||
## Error contract — fail loud
|
## Error contract — fail loud
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -4,11 +4,11 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "redis_store"
|
name = "redis_store"
|
||||||
version = "0.1.2"
|
version = "1.0.0"
|
||||||
description = "async redis wrapper over redis-py asyncio with a raw escape hatch, fail-loud and config-free"
|
description = "async redis wrapper over redis-py asyncio with a raw escape hatch, fail-loud and config-free"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"redis>=5",
|
"redis>=5.0.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[tool.hatch.build.targets.wheel]
|
[tool.hatch.build.targets.wheel]
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
from importlib.metadata import version, PackageNotFoundError
|
||||||
|
|
||||||
from .redis_store import RedisDB
|
from .redis_store import RedisDB
|
||||||
|
|
||||||
__all__ = ["RedisDB"]
|
try:
|
||||||
|
__version__ = version("redis_store")
|
||||||
|
except PackageNotFoundError:
|
||||||
|
__version__ = "0.0.0+unknown"
|
||||||
|
|
||||||
|
__all__ = ["RedisDB", "__version__"]
|
||||||
|
|||||||
@@ -12,28 +12,16 @@ context manager:
|
|||||||
async with RedisDB(host="localhost") as kv:
|
async with RedisDB(host="localhost") as kv:
|
||||||
await kv.incr("hits")
|
await kv.incr("hits")
|
||||||
|
|
||||||
lifecycle:
|
fail loud: wrapped methods catch RedisError, log, and re-raise; see each method's
|
||||||
construction is sync and opens no socket (the pool connects lazily). connect()
|
docstring for its own contract. unwrapped ops go through the raw `.client` escape
|
||||||
issues a ping() so a bad host/port/auth fails loud immediately rather than on the
|
hatch: full driver surface, raises.
|
||||||
first real op, and returns self for the one-liner above. close() tears the client
|
|
||||||
and pool down (await client.aclose() + pool.disconnect()).
|
|
||||||
|
|
||||||
type contract:
|
|
||||||
decode_responses=True by default, so keys and string values come back as str (and
|
|
||||||
None for an absent key). pass decode_responses=False at construction if you need raw
|
|
||||||
bytes. counters (incr/decr/exists/ttl) always return int regardless of this flag.
|
|
||||||
|
|
||||||
errors (FAIL LOUD — unlike the mongo lib's swallow-and-default):
|
|
||||||
every wrapped method catches the driver's RedisError, logs it via
|
|
||||||
getLogger(__name__), and re-raises. a None/[]/{} return is only ever a real result
|
|
||||||
(absent key, empty hash) — never a swallowed failure. for anything not wrapped
|
|
||||||
(pipelines, pub/sub, scan, Lua, ...) use the raw `.client` escape hatch, which
|
|
||||||
exposes the full redis.asyncio.Redis surface and raises untouched.
|
|
||||||
|
|
||||||
notes:
|
notes:
|
||||||
- import is `redis_store`; the repo/distribution is `redis` (the driver owns the
|
- import is `redis_store`; repo is `redis`, distribution is `redis_store` (the
|
||||||
`redis` import name, so the package can't also be `redis`)
|
driver owns the `redis` import name, so the package can't also be `redis`)
|
||||||
- pub/sub and pipeline helpers are intentionally not wrapped yet — use `.client`
|
- ssl=True maps to connection_class=redis.asyncio.SSLConnection so TLS works via
|
||||||
|
the documented pool_kwargs path; pass ssl-prefixed kwargs alongside it
|
||||||
|
(ssl_ca_certs, ssl_certfile, ...) to configure the handshake
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@@ -56,31 +44,28 @@ class RedisDB:
|
|||||||
password: Optional[str] = None,
|
password: Optional[str] = None,
|
||||||
*,
|
*,
|
||||||
max_connections: int = 10,
|
max_connections: int = 10,
|
||||||
pool_timeout: float = 30.0,
|
pool_timeout: Optional[float] = 30.0,
|
||||||
decode_responses: bool = True,
|
decode_responses: bool = True,
|
||||||
**pool_kwargs,
|
**pool_kwargs,
|
||||||
):
|
):
|
||||||
"""build the (not-yet-connected) pool + client; no I/O happens here
|
"""build the (not-yet-connected) pool + client; no I/O happens here
|
||||||
|
|
||||||
host/port/db/password/max_connections are injected by the caller. extra
|
uses a BlockingConnectionPool so callers wait up to pool_timeout seconds for a
|
||||||
pool_kwargs pass through to the connection pool (socket_timeout,
|
free connection instead of raising MaxConnectionsError immediately.
|
||||||
socket_connect_timeout, ssl, etc). decode_responses=True returns str; set it
|
|
||||||
False for raw bytes.
|
|
||||||
|
|
||||||
a BlockingConnectionPool is used so that, under concurrency exceeding
|
`timeout` in pool_kwargs collides with pool_timeout, so it's rejected with a
|
||||||
max_connections, callers WAIT up to pool_timeout seconds for a free connection
|
clear ValueError. `ssl=True` maps to `connection_class=redis.asyncio.SSLConnection`
|
||||||
(matching motor/mongo's blocking pool) rather than the plain pool's behavior of
|
since the default Connection class has no `ssl` parameter (pass ssl-prefixed
|
||||||
raising MaxConnectionsError immediately. pool_timeout=None waits forever.
|
kwargs alongside it to configure TLS; an explicit connection_class is left
|
||||||
|
untouched).
|
||||||
`timeout` is the pool's own kwarg (set from pool_timeout here), so passing it in
|
|
||||||
pool_kwargs would collide — reject it with a clear pointer rather than let the
|
|
||||||
driver raise a cryptic "multiple values for 'timeout'".
|
|
||||||
"""
|
"""
|
||||||
if "timeout" in pool_kwargs:
|
if "timeout" in pool_kwargs:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"redis_store: pass the pool wait timeout as pool_timeout=, not timeout= "
|
"redis_store: pass the pool wait timeout as pool_timeout=, not timeout= "
|
||||||
"(timeout is filled from pool_timeout)"
|
"(timeout is filled from pool_timeout)"
|
||||||
)
|
)
|
||||||
|
if pool_kwargs.pop("ssl", False):
|
||||||
|
pool_kwargs.setdefault("connection_class", redis.SSLConnection)
|
||||||
self._pool = redis.BlockingConnectionPool(
|
self._pool = redis.BlockingConnectionPool(
|
||||||
host=host,
|
host=host,
|
||||||
port=port,
|
port=port,
|
||||||
@@ -103,22 +88,31 @@ class RedisDB:
|
|||||||
await self._client.ping()
|
await self._client.ping()
|
||||||
except RedisError:
|
except RedisError:
|
||||||
log.exception("redis.connect() ping failed")
|
log.exception("redis.connect() ping failed")
|
||||||
|
await self._pool.disconnect()
|
||||||
raise
|
raise
|
||||||
return self
|
return self
|
||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
"""close the client and disconnect the pool on shutdown"""
|
"""close the client and disconnect the pool on shutdown
|
||||||
|
|
||||||
|
the pool disconnect always runs, even if the client close raises, so a
|
||||||
|
failure in one does not leak the other's resources.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
await self._client.aclose()
|
try:
|
||||||
await self._pool.disconnect()
|
await self._client.aclose()
|
||||||
|
finally:
|
||||||
|
await self._pool.disconnect()
|
||||||
except RedisError:
|
except RedisError:
|
||||||
log.exception("redis.close()")
|
log.exception("redis.close()")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def __aenter__(self) -> "RedisDB":
|
async def __aenter__(self) -> "RedisDB":
|
||||||
|
"""enter: connect() and return self"""
|
||||||
return await self.connect()
|
return await self.connect()
|
||||||
|
|
||||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||||
|
"""exit: close(), ignoring exc_type/exc/tb"""
|
||||||
await self.close()
|
await self.close()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -129,9 +123,6 @@ class RedisDB:
|
|||||||
"""
|
"""
|
||||||
return self._client
|
return self._client
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
|
||||||
# key / value
|
|
||||||
|
|
||||||
async def get(self, key: str) -> Optional[str]:
|
async def get(self, key: str) -> Optional[str]:
|
||||||
"""return the string value at key, or None if the key is absent"""
|
"""return the string value at key, or None if the key is absent"""
|
||||||
try:
|
try:
|
||||||
@@ -180,9 +171,6 @@ class RedisDB:
|
|||||||
log.exception("redis.decr(%s)", key)
|
log.exception("redis.decr(%s)", key)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
|
||||||
# hash
|
|
||||||
|
|
||||||
async def hget(self, name: str, field: str) -> Optional[str]:
|
async def hget(self, name: str, field: str) -> Optional[str]:
|
||||||
"""return the value of field in hash name, or None if either is absent"""
|
"""return the value of field in hash name, or None if either is absent"""
|
||||||
try:
|
try:
|
||||||
@@ -218,9 +206,6 @@ class RedisDB:
|
|||||||
log.exception("redis.hdel(%s)", name)
|
log.exception("redis.hdel(%s)", name)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
|
||||||
# expiry / ttl
|
|
||||||
|
|
||||||
async def expire(self, key: str, seconds: int) -> bool:
|
async def expire(self, key: str, seconds: int) -> bool:
|
||||||
"""set a ttl of seconds on key; returns False if the key does not exist"""
|
"""set a ttl of seconds on key; returns False if the key does not exist"""
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user