This commit is contained in:
Nuno Campos
2025-05-08 16:49:01 -07:00
parent a11a62e68f
commit 057da43cd0
2 changed files with 16 additions and 16 deletions
+8 -8
View File
@@ -21,22 +21,22 @@ class BaseCache(ABC, Generic[T]):
def get(self, keys: Sequence[str]) -> dict[str, T]:
"""Get the cached values for the given keys."""
@abstractmethod
def set(self, mapping: Mapping[str, tuple[T, int | None]]) -> None:
"""Set the cached values for the given keys and TTLs."""
@abstractmethod
def delete(self, keys: Sequence[str]) -> None:
"""Delete the cached values for the given keys."""
@abstractmethod
async def aget(self, keys: Sequence[str]) -> dict[str, T]:
"""Asynchronously get the cached values for the given keys."""
@abstractmethod
def set(self, mapping: Mapping[str, tuple[T, int | None]]) -> None:
"""Set the cached values for the given keys and TTLs."""
@abstractmethod
async def aset(self, mapping: Mapping[str, tuple[T, int | None]]) -> None:
"""Asynchronously set the cached values for the given keys and TTLs."""
@abstractmethod
def delete(self, keys: Sequence[str]) -> None:
"""Delete the cached values for the given keys."""
@abstractmethod
async def adelete(self, keys: Sequence[str]) -> None:
"""Asynchronously delete the cached values for the given keys."""
+8 -8
View File
@@ -28,25 +28,25 @@ class FileCache(BaseCache):
if key in self._db
}
async def aget(self, keys: list[str]) -> dict[str, bytes]:
"""Asynchronously get the cached values for the given keys."""
return await asyncio.to_thread(self.get, keys)
def set(self, mapping: dict[str, tuple[bytes, int | None]]) -> None:
"""Set the cached values for the given keys and TTLs."""
for key, (value, _) in mapping.items():
# File-based caches do not support TTLs, so we ignore them.
self._db[key] = ormsgpack.packb(self.serde.dumps_typed(value))
async def aset(self, mapping: dict[str, tuple[bytes, int | None]]) -> None:
"""Asynchronously set the cached values for the given keys and TTLs."""
await asyncio.to_thread(self.set, mapping)
def delete(self, keys: list[str]) -> None:
"""Delete the cached values for the given keys."""
for key in keys:
self._db.pop(key, None)
async def aget(self, keys: list[str]) -> dict[str, bytes]:
"""Asynchronously get the cached values for the given keys."""
return await asyncio.to_thread(self.get, keys)
async def aset(self, mapping: dict[str, tuple[bytes, int | None]]) -> None:
"""Asynchronously set the cached values for the given keys and TTLs."""
await asyncio.to_thread(self.set, mapping)
async def adelete(self, keys: list[str]) -> None:
"""Asynchronously delete the cached values for the given keys."""
await asyncio.to_thread(self.delete, keys)