-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachedApi.py
More file actions
80 lines (67 loc) · 2.41 KB
/
cachedApi.py
File metadata and controls
80 lines (67 loc) · 2.41 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from time import time_ns, sleep
from typing import Any, Optional
from sqlitedict import SqliteDict
import logging
logger = logging.getLogger()
class CachedApi:
def __init__(self, fullpath: str) -> None:
self.__fullpath = fullpath
self.__db: Optional[SqliteDict] = None
logger.debug("Initializing cache at %s", self.__fullpath)
def open_db(self) -> None:
"""Open the database and perform cleanup of expired entries."""
logger.warning("Opening DB %s", self.__fullpath)
self.__db = SqliteDict(self.__fullpath)
self._cleanup_expired_entries()
def _cleanup_expired_entries(self) -> None:
"""Remove expired cache entries."""
if not self.__db:
return
removed = 0
kept = 0
for key in list(self.__db.keys()):
try:
counter, period, _ = key.split("\t", 2)
current = int(time_ns() / 1_000_000_000 / int(period))
if current - int(counter) > 1:
del self.__db[key]
removed += 1
else:
kept += 1
except ValueError:
# Invalid key format, skip
pass
logger.debug("DB cleanup: keeping %d items, removed %d items", kept, removed)
def __del__(self) -> None:
logger.debug(f"Instance {self} destroyed.")
def cache_get(self, key: str, period: int) -> Any:
"""
Retrieve a value from cache.
Args:
key: Cache key.
period: Cache period in seconds.
Returns:
Cached value or None if not found.
"""
result = None
idx = self._get_idx(key, period)
try:
result = self.__db[idx] # type: ignore
except KeyError:
sleep(0.1) # Brief pause on miss
return result
def cache_set(self, key: str, period: int, value: Any) -> None:
"""
Store a value in cache.
Args:
key: Cache key.
period: Cache period in seconds.
value: Value to cache.
"""
idx = self._get_idx(key, period)
self.__db[idx] = value # type: ignore
self.__db.commit() # type: ignore
def _get_idx(self, key: str, period: int) -> str:
"""Generate cache index."""
counter = int(time_ns() / 1_000_000_000 / period)
return f"{counter}\t{period}\t{key}"