Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
23 lines
512 B
Python
23 lines
512 B
Python
"""Letvægts in-memory TTL-cache. Authentik-læsninger er I/O-bottleneck (ikke Python),
|
|
så vi cacher dem kort og rydder ved skrivninger."""
|
|
import time
|
|
|
|
_CACHE = {}
|
|
|
|
|
|
def get(key):
|
|
ent = _CACHE.get(key)
|
|
if ent and ent[0] > time.monotonic():
|
|
return ent[1]
|
|
return None
|
|
|
|
|
|
def put(key, val, ttl):
|
|
_CACHE[key] = (time.monotonic() + ttl, val)
|
|
|
|
|
|
def clear(*prefixes):
|
|
for k in list(_CACHE):
|
|
if not prefixes or any(k.startswith(p) for p in prefixes):
|
|
_CACHE.pop(k, None)
|