from dataclasses import dataclass from typing import Optional @dataclass(frozen=True) class SessionToken: token: bytes class SessionCache: def __init__(self, session_expiration_duration): self.cache = {} self.token_duration = session_expiration_duration self.current_epoch = 0 def contains(self, key: str): return key in self.cache def try_get_value(self, key: str) -> Optional[SessionToken]: if not key: return None return self.cache.get(key) def set_value(self, key: str, value: SessionToken): if key is not None: self.cache[key] = value def delete_by_prefix(self, prefix: str): # Collect keys to avoid modifying dictionary during iteration keys_to_delete = [key for key in self.cache if key.startswith(prefix)] for key in keys_to_delete: del self.cache[key] @staticmethod def form_cache_key(address: str, key: str): return address + key