-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy path_lru_cache.py
More file actions
43 lines (34 loc) · 1.14 KB
/
_lru_cache.py
File metadata and controls
43 lines (34 loc) · 1.14 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
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Any
_SENTINEL = object()
class LRUCache:
def __init__(self, max_size: int) -> None:
if max_size <= 0:
raise AssertionError(f"invalid max_size: {max_size}")
self.max_size = max_size
self._data: "dict[Any, Any]" = {}
self.hits = self.misses = 0
self.full = False
def set(self, key: "Any", value: "Any") -> None:
current = self._data.pop(key, _SENTINEL)
if current is not _SENTINEL:
self._data[key] = value
elif self.full:
self._data.pop(next(iter(self._data)))
self._data[key] = value
else:
self._data[key] = value
self.full = len(self._data) >= self.max_size
def get(self, key: "Any", default: "Any" = None) -> "Any":
try:
ret = self._data.pop(key)
except KeyError:
self.misses += 1
ret = default
else:
self.hits += 1
self._data[key] = ret
return ret
def get_all(self) -> "list[tuple[Any, Any]]":
return list(self._data.items())