DKB_ARMKA/api/src/core/async_cache.py
2026-09-23 11:52:08 +03:00

52 lines
1.4 KiB
Python

import asyncio
from datetime import datetime, timedelta
import uuid
from dataclasses import dataclass
from datetime import datetime
@dataclass
class FileDictItem:
started_at: datetime
expires_at: datetime
file_id: str
job_id: str
s3_key: str
legal_entity_id: str
class AsyncInMemoryCache:
def __init__(self):
self.mapping: dict[str, FileDictItem] = {}
self._lock = asyncio.Lock()
async def add(self, job_id: str, file_id: str, s3_key: str, legal_entity_id: str) -> FileDictItem:
now = datetime.now()
expiration_date = datetime.now() + timedelta(minutes=10)
new_item = FileDictItem(
started_at= now,
expires_at= expiration_date,
file_id= str(file_id),
s3_key=s3_key,
job_id= str(job_id),
legal_entity_id = legal_entity_id
)
async with self._lock:
self.mapping[str(job_id)] = new_item
return new_item
async def get(self, job_id: uuid.UUID):
async with self._lock:
return self.mapping.get(str(job_id))
async def remove(self, source_link: str) -> None:
async with self._lock:
self.mapping.pop(source_link, None)
async def get_active_items(self) -> list[FileDictItem]:
async with self._lock:
return [
item
for item in self.mapping.values()
]