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/pynyaata2/cache/simple.py

26 lines
719 B
Python
Raw Permalink Normal View History

2022-10-26 13:06:04 +00:00
from datetime import datetime, timedelta
2023-01-04 15:57:16 +00:00
from os import getenv
2022-10-26 13:06:04 +00:00
from typing import Dict, List, Optional, Tuple
2022-12-22 00:01:21 +00:00
2023-06-04 15:53:29 +00:00
from pynyaata2.types import Cache, RemoteFile
2022-10-26 13:06:04 +00:00
2023-01-04 15:57:16 +00:00
CACHE_TIMEOUT = int(getenv("CACHE_TIMEOUT", 60 * 60))
2022-10-26 13:06:04 +00:00
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]
2022-12-22 00:01:21 +00:00
2023-01-06 13:17:11 +00:00
if datetime.now() < timeout + timedelta(seconds=CACHE_TIMEOUT):
2022-10-26 13:06:04 +00:00
return data
else:
CACHE_DATA.pop(key)
2022-12-22 00:01:21 +00:00
2022-10-26 13:06:04 +00:00
return None
def set(self, key: str, data: List[RemoteFile]):
CACHE_DATA[key] = (data, datetime.now())