RedisAdapter
What it is
- A Redis-backed implementation of
IKeyValueAdapterfor basic key/value operations. - Stores and returns raw bytes (
decode_responses=False) and normalizes returned Redis values intobytes.
Public API
class RedisAdapter(IKeyValueAdapter)__init__(redis_url: str, socket_timeout: Optional[float] = None)- Creates a Redis client from a URL.
get(key: str) -> bytes- Fetches a value by key.
- Raises
KVNotFoundErrorif the key does not exist.
set(key: str, value: bytes, ttl: int | None = None) -> None- Sets a key to a value with optional TTL (seconds).
set_if_not_exists(key: str, value: bytes, ttl: int | None = None) -> bool- Sets a key only if it does not already exist; returns
Trueif set.
- Sets a key only if it does not already exist; returns
delete(key: str) -> None- Deletes a key.
- Raises
KVNotFoundErrorif the key does not exist.
delete_if_value_matches(key: str, value: bytes) -> bool- Atomically deletes a key only if its current value matches; returns
Trueif deleted.
- Atomically deletes a key only if its current value matches; returns
exists(key: str) -> bool- Checks if a key exists.
Configuration/Dependencies
- Requires the
redisPython package:- Install:
pip install redis
- Install:
- Constructor parameters:
redis_url: required; passed toredis.Redis.from_url(...).socket_timeout: optional float; forwarded to the Redis client.
- Uses
decode_responses=False(values handled as bytes-like, then normalized tobytes).
Usage
from naas_abi_core.services.keyvalue.adapters.secondary.RedisAdapter import RedisAdapter
from naas_abi_core.services.keyvalue.KeyValuePorts import KVNotFoundError
kv = RedisAdapter(redis_url="redis://localhost:6379/0")
kv.set("k1", b"v1", ttl=60)
assert kv.get("k1") == b"v1"
created = kv.set_if_not_exists("k1", b"v2")
assert created is False
deleted = kv.delete_if_value_matches("k1", b"v1")
assert deleted is True
assert kv.exists("k1") is False
try:
kv.get("k1")
except KVNotFoundError:
pass
Caveats
get()anddelete()raiseKVNotFoundErrorwhen the key is missing.- Values are normalized to
bytes; passingstr,bytearray, ormemoryviewis accepted internally via normalization.