Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

afix: Cache SHA key implementation

Clémence Lesnéc2854bbd6f94
diff --git a/persistence/icache.py b/persistence/icache.py
index 0904ded..07d3118 100644
--- a/persistence/icache.py
+++ b/persistence/icache.py
@@ -4,11 +4,9 @@ from typing import Optional, Union
class ICache(ABC):
@abstractmethod
- async def aget(self, key: Union[str, bytes]) -> Optional[bytes]:
+ async def aget(self, key: str) -> Optional[bytes]:
pass
@abstractmethod
- async def aset(
- self, key: Union[str, bytes], value: Union[str, bytes, None]
- ) -> bool:
+ async def aset(self, key: str, value: Union[str, bytes, None]) -> bool:
pass
diff --git a/persistence/memory.py b/persistence/memory.py
index 1849f3c..32d679c 100644
--- a/persistence/memory.py
+++ b/persistence/memory.py
@@ -15,18 +15,15 @@ class MemoryCache(ICache):
_logger.info(f"Using memory cache with {config.max_size} size limit")
self._config = config
- async def aget(self, key: Union[str, bytes]) -> Optional[bytes]:
+ async def aget(self, key: str) -> Optional[bytes]:
"""
Get a value from the cache.
If the key does not exist, return `None`.
"""
- str_key = self._key_to_str(key)
- return self._cache.get(str_key, None)
+ return self._cache.get(key, None)
- async def aset(
- self, key: Union[str, bytes], value: Union[str, bytes, None]
- ) -> bool:
+ async def aset(self, key: str, value: Union[str, bytes, None]) -> bool:
"""
Set a value in the cache.
@@ -34,10 +31,5 @@ class MemoryCache(ICache):
"""
if len(self._cache) >= self._config.max_size:
self._cache = {}
- str_key = self._key_to_str(key)
- self._cache[str_key] = value.encode() if isinstance(value, str) else value
+ self._cache[key] = value.encode() if isinstance(value, str) else value
return True
-
- @staticmethod
- def _key_to_str(key: Union[str, bytes]) -> str:
- return key.decode() if isinstance(key, bytes) else str(key)
diff --git a/persistence/redis.py b/persistence/redis.py
index 4264f5f..fc4c7b2 100644
--- a/persistence/redis.py
+++ b/persistence/redis.py
@@ -1,5 +1,4 @@
from contextlib import asynccontextmanager
-from functools import wraps
from helpers.config_models.cache import RedisModel
from helpers.logging import build_logger
from persistence.icache import ICache
@@ -7,7 +6,7 @@ from redis.asyncio import Redis
from redis.asyncio.retry import Retry
from redis.backoff import ExponentialBackoff
from redis.exceptions import BusyLoadingError, ConnectionError, TimeoutError
-from typing import AsyncGenerator, Awaitable, Callable, Optional, Union
+from typing import AsyncGenerator, Optional, Union
import hashlib
@@ -22,27 +21,7 @@ class RedisCache(ICache):
_logger.info(f"Using Redis cache {config.host}:{config.port}")
self._config = config
- @staticmethod
- def _key_as_sha(func: Callable[..., Awaitable]):
- """
- Decorator to convert the key to a SHA-256 hash.
- """
-
- @wraps(func)
- async def wrapper(self, key: Union[str, bytes], *args, **kwargs):
- bytes_key = key.encode() if isinstance(key, str) else key
- sha_key = hashlib.sha256(bytes_key, usedforsecurity=False).digest()
- return await func(
- self,
- *args,
- key=sha_key,
- **kwargs,
- )
-
- return wrapper
-
- @_key_as_sha
- async def aget(self, key: Union[str, bytes]) -> Optional[bytes]:
+ async def aget(self, key: str) -> Optional[bytes]:
"""
Get a value from the cache.
@@ -50,15 +29,13 @@ class RedisCache(ICache):
Catch errors for a maximum of 3 times, then raise the error.
"""
+ sha_key = self._key_to_hash(key)
res = None
async with self._use_db() as db:
- res = await db.get(key)
+ res = await db.get(sha_key)
return res
- @_key_as_sha
- async def aset(
- self, key: Union[str, bytes], value: Union[str, bytes, None]
- ) -> bool:
+ async def aset(self, key: str, value: Union[str, bytes, None]) -> bool:
"""
Set a value in the cache.
@@ -66,9 +43,9 @@ class RedisCache(ICache):
Catch errors for a maximum of 3 times, then raise the error.
"""
- # TODO: Catch errors
+ sha_key = self._key_to_hash(key)
async with self._use_db() as db:
- await db.set(key, value if value else "")
+ await db.set(sha_key, value if value else "")
return True
@asynccontextmanager
@@ -94,3 +71,7 @@ class RedisCache(ICache):
yield client
finally:
await client.aclose()
+
+ @staticmethod
+ def _key_to_hash(key: str) -> bytes:
+ return hashlib.sha256(key.encode(), usedforsecurity=False).digest()