# retoor <retoor@molodetz.nl>

from devplacepy.cache import TTLCache
from devplacepy.database import bump_cache_version, sync_local_cache

_user_cache = TTLCache(ttl=300, max_size=2000)


def _sync_auth_cache() -> None:
    sync_local_cache("auth", _user_cache)


def clear_user_cache(user_uid: str, *, propagate: bool = True) -> None:
    for token, user in _user_cache.items():
        if user.get("uid") == user_uid:
            _user_cache.pop(token)
    if propagate:
        bump_cache_version("auth")


def clear_session_cache(token: str) -> None:
    _user_cache.pop(token)
    bump_cache_version("auth")
