Nas0x1's picture
Deploy AI image forensics Space
f70b6e3 verified
Raw
History Blame Contribute Delete
869 Bytes
from __future__ import annotations
import copy
import hashlib
from collections import OrderedDict
from typing import Any
class AnalysisCache:
def __init__(self, max_items: int = 16) -> None:
self.max_items = max_items
self._items: OrderedDict[str, Any] = OrderedDict()
@staticmethod
def hash_bytes(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
def get(self, key: str) -> Any | None:
if key not in self._items:
return None
value = self._items.pop(key)
self._items[key] = value
return copy.deepcopy(value)
def set(self, key: str, value: Any) -> None:
self._items[key] = copy.deepcopy(value)
self._items.move_to_end(key)
while len(self._items) > self.max_items:
self._items.popitem(last=False)
GLOBAL_CACHE = AnalysisCache()