Repository navigation
Expand file tree
/
Copy pathcache.py
More file actions
51 lines (41 loc) · 1.5 KB
/
Copy pathcache.py
File metadata and controls
51 lines (41 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""In-memory TTL LRU cache for Forage.
Thread-safe, per-entry TTL, global max-entries cap. Used by both search
and extract (config: cache.*). Lost on restart, by design.
"""
from __future__ import annotations
import threading
import time
from collections import OrderedDict
from typing import Any, Optional
class TTLCache:
"""Thread-safe LRU cache with per-entry TTL (seconds)."""
def __init__(self, max_entries: int = 500) -> None:
self.max_entries = max_entries
self._data: "OrderedDict[str, tuple[float, Any]]" = OrderedDict()
self._lock = threading.Lock()
def get(self, key: str) -> Optional[Any]:
with self._lock:
item = self._data.get(key)
if item is None:
return None
expires_at, value = item
if expires_at < time.monotonic():
del self._data[key]
return None
self._data.move_to_end(key)
return value
def set(self, key: str, value: Any, ttl: int) -> None:
expires_at = time.monotonic() + max(ttl, 0)
with self._lock:
self._data[key] = (expires_at, value)
self._data.move_to_end(key)
while len(self._data) > self.max_entries:
self._data.popitem(last=False)
def clear(self) -> int:
with self._lock:
n = len(self._data)
self._data.clear()
return n
def __len__(self) -> int:
with self._lock:
return len(self._data)