Compare commits
4
Commits
v0.1.4
...
7aed6853ed
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7aed6853ed | ||
|
|
c7deeca5a7 | ||
|
|
52864e513a | ||
|
|
ea1e3620e6 |
@@ -14,19 +14,19 @@ datastore trio (`redis` / `psql` / `mysql`), a sibling of the `mongo` lib.
|
||||
`requirements.txt`:
|
||||
|
||||
```
|
||||
redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.4
|
||||
redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.2.1
|
||||
```
|
||||
|
||||
Direct:
|
||||
|
||||
```bash
|
||||
pip install "redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.4"
|
||||
pip install "redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.2.1"
|
||||
```
|
||||
|
||||
Pulls `redis>=5.0.1` (redis-py, which ships the asyncio client — **not** the dead standalone
|
||||
`aioredis`; `5.0.1` is the floor because `5.0.0` lacks the async client's `aclose()`).
|
||||
|
||||
Drop the `@v0.1.4` suffix from the line above to install the latest unpinned.
|
||||
Drop the `@v0.2.1` suffix from the line above to install the latest unpinned.
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "redis_store"
|
||||
version = "0.1.4"
|
||||
version = "1.0.0"
|
||||
description = "async redis wrapper over redis-py asyncio with a raw escape hatch, fail-loud and config-free"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
from importlib.metadata import version, PackageNotFoundError
|
||||
|
||||
from .redis_store import RedisDB
|
||||
|
||||
__version__ = "0.1.4"
|
||||
try:
|
||||
__version__ = version("redis_store")
|
||||
except PackageNotFoundError:
|
||||
__version__ = "0.0.0+unknown"
|
||||
|
||||
__all__ = ["RedisDB", "__version__"]
|
||||
|
||||
@@ -12,24 +12,13 @@ context manager:
|
||||
async with RedisDB(host="localhost") as kv:
|
||||
await kv.incr("hits")
|
||||
|
||||
lifecycle: construction is sync, opens no socket (pool connects lazily). connect()
|
||||
pings so bad host/port/auth fails loud immediately, returns self. close() tears
|
||||
down client + pool (client.aclose() + pool.disconnect()). a failed connect()/
|
||||
__aenter__ ping also disconnects the pool before re-raising, so a discarded
|
||||
RedisDB from a failed `async with` doesn't hold a live pool.
|
||||
|
||||
type contract: decode_responses=True by default -> str values, None for absent key.
|
||||
decode_responses=False for raw bytes. counters (incr/decr/exists/ttl) always int.
|
||||
|
||||
errors (FAIL LOUD — unlike mongo's swallow-and-default): every wrapped method catches
|
||||
RedisError, logs via getLogger(__name__), re-raises. None/[]/{} is only ever a
|
||||
real result, never a swallowed failure. unwrapped ops (pipelines, pub/sub, scan,
|
||||
Lua, ...) go through the raw `.client` escape hatch: full driver surface, raises.
|
||||
fail loud: wrapped methods catch RedisError, log, and re-raise; see each method's
|
||||
docstring for its own contract. unwrapped ops go through the raw `.client` escape
|
||||
hatch: full driver surface, raises.
|
||||
|
||||
notes:
|
||||
- import is `redis_store`; repo is `redis`, distribution is `redis_store` (the
|
||||
driver owns the `redis` import name, so the package can't also be `redis`)
|
||||
- pub/sub and pipeline helpers 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
|
||||
@@ -61,26 +50,14 @@ class RedisDB:
|
||||
):
|
||||
"""build the (not-yet-connected) pool + client; no I/O happens here
|
||||
|
||||
host/port/db/password/max_connections injected by the caller. extra
|
||||
pool_kwargs pass through to the connection pool (socket_timeout,
|
||||
socket_connect_timeout, ssl, etc). decode_responses=True returns str, False
|
||||
raw bytes.
|
||||
uses a BlockingConnectionPool so callers wait up to pool_timeout seconds for a
|
||||
free connection instead of raising MaxConnectionsError immediately.
|
||||
|
||||
a BlockingConnectionPool is used so callers WAIT up to pool_timeout seconds
|
||||
for a free connection under concurrency exceeding max_connections (matching
|
||||
motor/mongo's blocking pool), instead of raising MaxConnectionsError
|
||||
immediately. pool_timeout=None waits forever.
|
||||
|
||||
`timeout` is the pool's own kwarg (filled from pool_timeout here); passing it
|
||||
in pool_kwargs would collide, so it's rejected with a clear ValueError instead
|
||||
of a cryptic driver "multiple values for 'timeout'".
|
||||
|
||||
`ssl=True` maps to `connection_class=redis.asyncio.SSLConnection` — the
|
||||
default async Connection class has no `ssl` parameter, so passing it straight
|
||||
through pool_kwargs would defer the failure to connect() as a raw TypeError.
|
||||
pass ssl-prefixed kwargs (ssl_ca_certs, ssl_certfile, ...) alongside ssl=True
|
||||
to configure TLS; an explicit connection_class in pool_kwargs is left
|
||||
untouched.
|
||||
`timeout` in pool_kwargs collides with pool_timeout, so it's rejected with a
|
||||
clear ValueError. `ssl=True` maps to `connection_class=redis.asyncio.SSLConnection`
|
||||
since the default Connection class has no `ssl` parameter (pass ssl-prefixed
|
||||
kwargs alongside it to configure TLS; an explicit connection_class is left
|
||||
untouched).
|
||||
"""
|
||||
if "timeout" in pool_kwargs:
|
||||
raise ValueError(
|
||||
@@ -116,18 +93,26 @@ class RedisDB:
|
||||
return self
|
||||
|
||||
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:
|
||||
await self._client.aclose()
|
||||
await self._pool.disconnect()
|
||||
try:
|
||||
await self._client.aclose()
|
||||
finally:
|
||||
await self._pool.disconnect()
|
||||
except RedisError:
|
||||
log.exception("redis.close()")
|
||||
raise
|
||||
|
||||
async def __aenter__(self) -> "RedisDB":
|
||||
"""enter: connect() and return self"""
|
||||
return await self.connect()
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||
"""exit: close(), ignoring exc_type/exc/tb"""
|
||||
await self.close()
|
||||
|
||||
@property
|
||||
@@ -138,9 +123,6 @@ class RedisDB:
|
||||
"""
|
||||
return self._client
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# key / value
|
||||
|
||||
async def get(self, key: str) -> Optional[str]:
|
||||
"""return the string value at key, or None if the key is absent"""
|
||||
try:
|
||||
@@ -189,9 +171,6 @@ class RedisDB:
|
||||
log.exception("redis.decr(%s)", key)
|
||||
raise
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# hash
|
||||
|
||||
async def hget(self, name: str, field: str) -> Optional[str]:
|
||||
"""return the value of field in hash name, or None if either is absent"""
|
||||
try:
|
||||
@@ -227,9 +206,6 @@ class RedisDB:
|
||||
log.exception("redis.hdel(%s)", name)
|
||||
raise
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# expiry / ttl
|
||||
|
||||
async def expire(self, key: str, seconds: int) -> bool:
|
||||
"""set a ttl of seconds on key; returns False if the key does not exist"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user