|
@@ -1,9 +1,10 @@
|
|
|
-"""Cache mémoire des pages wiki (id -> contenu + horodatage), avec TTL."""
|
|
|
|
|
|
|
+"""Cache mémoire des pages wiki (id -> contenu + horodatage), TTL + LRU."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import time
|
|
import time
|
|
|
import uuid
|
|
import uuid
|
|
|
|
|
+from collections import OrderedDict
|
|
|
from typing import Any
|
|
from typing import Any
|
|
|
|
|
|
|
|
from dokuwiki_mcp.config.initialisation import Initialisation
|
|
from dokuwiki_mcp.config.initialisation import Initialisation
|
|
@@ -11,7 +12,7 @@ from dokuwiki_mcp.services.client_http import ClientHTTP, ClientHTTPError
|
|
|
|
|
|
|
|
|
|
|
|
|
class PageCache:
|
|
class PageCache:
|
|
|
- """Cache clé/valeur en mémoire"""
|
|
|
|
|
|
|
+ """Cache clé/valeur en mémoire, plafond LRU et expiration TTL."""
|
|
|
|
|
|
|
|
_instance: PageCache | None = None
|
|
_instance: PageCache | None = None
|
|
|
|
|
|
|
@@ -23,8 +24,10 @@ class PageCache:
|
|
|
def __init__(self) -> None:
|
|
def __init__(self) -> None:
|
|
|
if getattr(self, "_ready", False):
|
|
if getattr(self, "_ready", False):
|
|
|
return
|
|
return
|
|
|
- self._ttl = Initialisation().ttl
|
|
|
|
|
- self._store: dict[str, tuple[str, float]] = {}
|
|
|
|
|
|
|
+ init = Initialisation()
|
|
|
|
|
+ self._ttl = init.ttl
|
|
|
|
|
+ self._max_entries = init.max_entries
|
|
|
|
|
+ self._store: OrderedDict[str, tuple[str, float]] = OrderedDict()
|
|
|
self._ready = True
|
|
self._ready = True
|
|
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
@@ -37,6 +40,7 @@ class PageCache:
|
|
|
if cached is not None:
|
|
if cached is not None:
|
|
|
content, stamped = cached
|
|
content, stamped = cached
|
|
|
if now - stamped <= self._ttl:
|
|
if now - stamped <= self._ttl:
|
|
|
|
|
+ self._store.move_to_end(page_id)
|
|
|
return content
|
|
return content
|
|
|
del self._store[page_id]
|
|
del self._store[page_id]
|
|
|
|
|
|
|
@@ -50,12 +54,21 @@ class PageCache:
|
|
|
},
|
|
},
|
|
|
)
|
|
)
|
|
|
content = _extract_page_text(body)
|
|
content = _extract_page_text(body)
|
|
|
- self._store[page_id] = (content, now)
|
|
|
|
|
|
|
+ self._put(page_id, content, now)
|
|
|
return content
|
|
return content
|
|
|
|
|
|
|
|
def invalidate(self, page_id: str) -> None:
|
|
def invalidate(self, page_id: str) -> None:
|
|
|
self._store.pop(page_id, None)
|
|
self._store.pop(page_id, None)
|
|
|
|
|
|
|
|
|
|
+ def _put(self, page_id: str, content: str, stamped: float) -> None:
|
|
|
|
|
+ if page_id in self._store:
|
|
|
|
|
+ self._store[page_id] = (content, stamped)
|
|
|
|
|
+ self._store.move_to_end(page_id)
|
|
|
|
|
+ return
|
|
|
|
|
+ while len(self._store) >= self._max_entries:
|
|
|
|
|
+ self._store.popitem(last=False)
|
|
|
|
|
+ self._store[page_id] = (content, stamped)
|
|
|
|
|
+
|
|
|
|
|
|
|
|
def _extract_page_text(body: Any) -> str:
|
|
def _extract_page_text(body: Any) -> str:
|
|
|
if not isinstance(body, dict):
|
|
if not isinstance(body, dict):
|