This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/pynyaata/cache/simple.py

22 lines
680 B
Python
Raw Normal View History

2022-10-26 13:06:04 +00:00
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Tuple
from pynyaata.cache import CACHE_TIMEOUT
from pynyaata.types import Cache, RemoteFile
CACHE_DATA: Dict[str, Tuple[List[RemoteFile], datetime]] = {}
class SimpleCache(Cache):
def get(self, key: str) -> Optional[List[RemoteFile]]:
if key in CACHE_DATA:
data, timeout = CACHE_DATA[key]
if datetime.now() > timeout + timedelta(seconds=CACHE_TIMEOUT):
return data
else:
CACHE_DATA.pop(key)
return None
def set(self, key: str, data: List[RemoteFile]):
CACHE_DATA[key] = (data, datetime.now())