added redis types stub and fixed return type
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Definition of RedisAdapter class."""
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import cast
|
||||
|
||||
import os
|
||||
import structlog
|
||||
@@ -100,7 +101,7 @@ class RedisAdapter(
|
||||
if not isinstance(data, dict) or len(data) == 0:
|
||||
raise ValueError("Data must be a non-empty dictionary")
|
||||
# Check connection
|
||||
if not self.is_connected:
|
||||
if self._client is None or not self.is_connected:
|
||||
raise ConnectionError("Not connected to Redis")
|
||||
# Set data
|
||||
self._client.json().set(key, self.path, data)
|
||||
@@ -112,10 +113,13 @@ class RedisAdapter(
|
||||
if not isinstance(key, str) or len(key) == 0:
|
||||
raise ValueError("Key must be a non-empty string")
|
||||
# Check connection
|
||||
if not self.is_connected:
|
||||
if self._client is None or not self.is_connected:
|
||||
raise ConnectionError("Not connected to Redis")
|
||||
# Get data
|
||||
data = self._client.json().get(key)
|
||||
data = cast(
|
||||
dict | None,
|
||||
self._client.json().get(key),
|
||||
)
|
||||
self.logger.debug(f"Got {data} from {key}")
|
||||
return data
|
||||
|
||||
@@ -125,25 +129,28 @@ class RedisAdapter(
|
||||
if not isinstance(key, str) or len(key) == 0:
|
||||
raise ValueError("Key must be a non-empty string")
|
||||
# Check connection
|
||||
if not self.is_connected:
|
||||
if self._client is None or not self.is_connected:
|
||||
raise ConnectionError("Not connected to Redis")
|
||||
# Delete data
|
||||
self._client.json().delete(key)
|
||||
self.logger.debug(f"Deleted {key}")
|
||||
|
||||
def _list_keys(self, pattern: str) -> list[str]:
|
||||
async def _list_keys(self, pattern: str) -> list[str]:
|
||||
"""List keys in Redis matching a pattern."""
|
||||
# Check input
|
||||
if not isinstance(pattern, str) or len(pattern) == 0:
|
||||
raise ValueError("Pattern must be a non-empty string")
|
||||
# Check connection
|
||||
if not self.is_connected:
|
||||
if self._client is None or not self.is_connected:
|
||||
raise ConnectionError("Not connected to Redis")
|
||||
# List keys
|
||||
keys_raw = cast(
|
||||
list[bytes],
|
||||
self._client.keys(pattern),
|
||||
)
|
||||
keys: list[str] = [
|
||||
key.decode(self.encoding)
|
||||
for key
|
||||
in self._client.keys(pattern)
|
||||
for key in keys_raw
|
||||
]
|
||||
self.logger.debug(f"Got {keys} matching {pattern}")
|
||||
return keys
|
||||
|
||||
Reference in New Issue
Block a user