beerds/server/infra/cache.py

34 lines
905 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from abc import ABCMeta, abstractmethod
from typing import Optional
class CacheRepository(metaclass=ABCMeta):
@abstractmethod
async def get(self, key: str) -> Optional[str]:
pass
@abstractmethod
async def set(self, key: str, data: str, _exp_min: Optional[int] = None):
pass
@abstractmethod
async def delete(self, key: str):
pass
# TODO: сделать через общий кеш, например, redis. Работу с редис вынести в infra
class LocalCacheRepository(CacheRepository):
_data: dict[str, str]
def __init__(self) -> None:
self._data = {}
async def get(self, key: str) -> Optional[str]:
return self._data.get(key)
async def set(self, key: str, data: str, _exp_min: Optional[int] = None):
self._data[key] = data
async def delete(self, key: str):
del self._data[key]