|
|
@@ -12,24 +12,15 @@ 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: construction is sync, opens no socket (pool connects lazily). connect()
|
|
|
|
fail loud: wrapped methods catch RedisError and re-raise it - the raised exception IS
|
|
|
|
pings so bad host/port/auth fails loud immediately, returns self. close() tears
|
|
|
|
the signal, and the caller (which alone knows fatal-vs-routine) decides and logs; the
|
|
|
|
down client + pool (client.aclose() + pool.disconnect()). a failed connect()/
|
|
|
|
wrapped method logs only at DEBUG (with the traceback) so a failure isn't reported
|
|
|
|
__aenter__ ping also disconnects the pool before re-raising, so a discarded
|
|
|
|
twice (raise XOR log). see each method's docstring for its own contract. unwrapped ops
|
|
|
|
RedisDB from a failed `async with` doesn't hold a live pool.
|
|
|
|
go through the raw `.client` escape hatch: full driver surface, raises.
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
notes:
|
|
|
|
notes:
|
|
|
|
- import is `redis_store`; repo is `redis`, distribution is `redis_store` (the
|
|
|
|
- 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`)
|
|
|
|
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
|
|
|
|
- ssl=True maps to connection_class=redis.asyncio.SSLConnection so TLS works via
|
|
|
|
the documented pool_kwargs path; pass ssl-prefixed kwargs alongside it
|
|
|
|
the documented pool_kwargs path; pass ssl-prefixed kwargs alongside it
|
|
|
|
(ssl_ca_certs, ssl_certfile, ...) to configure the handshake
|
|
|
|
(ssl_ca_certs, ssl_certfile, ...) to configure the handshake
|
|
|
@@ -61,26 +52,14 @@ class RedisDB:
|
|
|
|
):
|
|
|
|
):
|
|
|
|
"""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 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, False
|
|
|
|
|
|
|
|
raw bytes.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a BlockingConnectionPool is used so callers WAIT up to pool_timeout seconds
|
|
|
|
`timeout` in pool_kwargs collides with pool_timeout, so it's rejected with a
|
|
|
|
for a free connection under concurrency exceeding max_connections (matching
|
|
|
|
clear ValueError. `ssl=True` maps to `connection_class=redis.asyncio.SSLConnection`
|
|
|
|
motor/mongo's blocking pool), instead of raising MaxConnectionsError
|
|
|
|
since the default Connection class has no `ssl` parameter (pass ssl-prefixed
|
|
|
|
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 (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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if "timeout" in pool_kwargs:
|
|
|
|
if "timeout" in pool_kwargs:
|
|
|
|
raise ValueError(
|
|
|
|
raise ValueError(
|
|
|
@@ -110,24 +89,32 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
await self._client.ping()
|
|
|
|
await self._client.ping()
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.connect() ping failed")
|
|
|
|
log.debug("redis.connect() ping failed", exc_info=True)
|
|
|
|
await self._pool.disconnect()
|
|
|
|
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:
|
|
|
|
try:
|
|
|
|
await self._client.aclose()
|
|
|
|
await self._client.aclose()
|
|
|
|
|
|
|
|
finally:
|
|
|
|
await self._pool.disconnect()
|
|
|
|
await self._pool.disconnect()
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.close()")
|
|
|
|
log.debug("redis.close()", exc_info=True)
|
|
|
|
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
|
|
|
@@ -138,15 +125,12 @@ 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:
|
|
|
|
return await self._client.get(key)
|
|
|
|
return await self._client.get(key)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.get(%s)", key)
|
|
|
|
log.debug("redis.get(%s)", key, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def set(self, key: str, value, ex: Optional[int] = None) -> bool:
|
|
|
|
async def set(self, key: str, value, ex: Optional[int] = None) -> bool:
|
|
|
@@ -154,7 +138,7 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return bool(await self._client.set(key, value, ex=ex))
|
|
|
|
return bool(await self._client.set(key, value, ex=ex))
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.set(%s)", key)
|
|
|
|
log.debug("redis.set(%s)", key, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def delete(self, *keys: str) -> int:
|
|
|
|
async def delete(self, *keys: str) -> int:
|
|
|
@@ -162,7 +146,7 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self._client.delete(*keys)
|
|
|
|
return await self._client.delete(*keys)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.delete(%s)", keys)
|
|
|
|
log.debug("redis.delete(%s)", keys, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def exists(self, *keys: str) -> int:
|
|
|
|
async def exists(self, *keys: str) -> int:
|
|
|
@@ -170,7 +154,7 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self._client.exists(*keys)
|
|
|
|
return await self._client.exists(*keys)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.exists(%s)", keys)
|
|
|
|
log.debug("redis.exists(%s)", keys, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def incr(self, key: str, amount: int = 1) -> int:
|
|
|
|
async def incr(self, key: str, amount: int = 1) -> int:
|
|
|
@@ -178,7 +162,7 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self._client.incrby(key, amount)
|
|
|
|
return await self._client.incrby(key, amount)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.incr(%s)", key)
|
|
|
|
log.debug("redis.incr(%s)", key, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def decr(self, key: str, amount: int = 1) -> int:
|
|
|
|
async def decr(self, key: str, amount: int = 1) -> int:
|
|
|
@@ -186,18 +170,15 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self._client.decrby(key, amount)
|
|
|
|
return await self._client.decrby(key, amount)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.decr(%s)", key)
|
|
|
|
log.debug("redis.decr(%s)", key, exc_info=True)
|
|
|
|
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:
|
|
|
|
return await self._client.hget(name, field)
|
|
|
|
return await self._client.hget(name, field)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.hget(%s, %s)", name, field)
|
|
|
|
log.debug("redis.hget(%s, %s)", name, field, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def hset(self, name: str, key: Optional[str] = None, value=None, mapping: Optional[dict] = None) -> int:
|
|
|
|
async def hset(self, name: str, key: Optional[str] = None, value=None, mapping: Optional[dict] = None) -> int:
|
|
|
@@ -208,7 +189,7 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self._client.hset(name, key=key, value=value, mapping=mapping)
|
|
|
|
return await self._client.hset(name, key=key, value=value, mapping=mapping)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.hset(%s)", name)
|
|
|
|
log.debug("redis.hset(%s)", name, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def hgetall(self, name: str) -> dict:
|
|
|
|
async def hgetall(self, name: str) -> dict:
|
|
|
@@ -216,7 +197,7 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self._client.hgetall(name)
|
|
|
|
return await self._client.hgetall(name)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.hgetall(%s)", name)
|
|
|
|
log.debug("redis.hgetall(%s)", name, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def hdel(self, name: str, *fields: str) -> int:
|
|
|
|
async def hdel(self, name: str, *fields: str) -> int:
|
|
|
@@ -224,18 +205,15 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self._client.hdel(name, *fields)
|
|
|
|
return await self._client.hdel(name, *fields)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.hdel(%s)", name)
|
|
|
|
log.debug("redis.hdel(%s)", name, exc_info=True)
|
|
|
|
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:
|
|
|
|
return bool(await self._client.expire(key, seconds))
|
|
|
|
return bool(await self._client.expire(key, seconds))
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.expire(%s)", key)
|
|
|
|
log.debug("redis.expire(%s)", key, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
async def ttl(self, key: str) -> int:
|
|
|
|
async def ttl(self, key: str) -> int:
|
|
|
@@ -247,5 +225,5 @@ class RedisDB:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return await self._client.ttl(key)
|
|
|
|
return await self._client.ttl(key)
|
|
|
|
except RedisError:
|
|
|
|
except RedisError:
|
|
|
|
log.exception("redis.ttl(%s)", key)
|
|
|
|
log.debug("redis.ttl(%s)", key, exc_info=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|