Selaa lähdekoodia

update LRU and max entries in cache

Khanh Nguyen 2 kuukautta sitten
vanhempi
commit
2576ce8b2a

+ 1 - 0
.env.example

@@ -2,3 +2,4 @@ BASE_URL=https://wiki.example.com
 WIKI_USERNAME=votre_utilisateur
 WIKI_USERNAME=votre_utilisateur
 WIKI_PASSWORD=votre_mot_de_passe
 WIKI_PASSWORD=votre_mot_de_passe
 CACHE_TTL=60
 CACHE_TTL=60
+CACHE_MAX_ENTRIES=20

+ 4 - 1
README.md

@@ -17,8 +17,11 @@ BASE_URL=https://votre-wiki.example.com
 WIKI_USERNAME=votre_utilisateur
 WIKI_USERNAME=votre_utilisateur
 WIKI_PASSWORD=votre_mot_de_passe
 WIKI_PASSWORD=votre_mot_de_passe
 CACHE_TTL=60
 CACHE_TTL=60
+CACHE_MAX_ENTRIES=20
 ```
 ```
 
 
+Le cache pages a un TTL (`CACHE_TTL`) et un plafond LRU (`CACHE_MAX_ENTRIES`, 20 par défaut).
+
 Chaque requête envoie `Authorization: Basic` suivi de `base64(username:password)`.
 Chaque requête envoie `Authorization: Basic` suivi de `base64(username:password)`.
 
 
 Le fichier `.env` ne doit pas être commité (`.gitignore` / `.cursorignore`).
 Le fichier `.env` ne doit pas être commité (`.gitignore` / `.cursorignore`).
@@ -53,7 +56,7 @@ Outils : `search_pages`, `save_page`, `append_page` (réponse brute de l'API).
 Ressources :
 Ressources :
 
 
 - `dokuwiki://pages/{namespace}` — enfants directs (`-` = racine)
 - `dokuwiki://pages/{namespace}` — enfants directs (`-` = racine)
-- `dokuwiki://page/{id}` — texte wiki via le cache TTL
+- `dokuwiki://page/{id}` — texte wiki via le cache (TTL + plafond LRU)
 - `dokuwiki://medias/{namespace}` — médias du namespace et en dessous
 - `dokuwiki://medias/{namespace}` — médias du namespace et en dessous
 - `dokuwiki://media/{id}` — contenu média
 - `dokuwiki://media/{id}` — contenu média
 
 

+ 14 - 6
src/dokuwiki_mcp/config/initialisation.py

@@ -11,6 +11,7 @@ from dokuwiki_mcp.config.logging_setup import configure_logging, get_logger
 REQUIRED_VARS = ("BASE_URL", "WIKI_USERNAME", "WIKI_PASSWORD")
 REQUIRED_VARS = ("BASE_URL", "WIKI_USERNAME", "WIKI_PASSWORD")
 JSONRPC_ENDPOINT = "/lib/exe/jsonrpc.php"
 JSONRPC_ENDPOINT = "/lib/exe/jsonrpc.php"
 DEFAULT_TTL = 60
 DEFAULT_TTL = 60
+DEFAULT_MAX_ENTRIES = 20
 DEFAULT_TIMEOUT = 5.0
 DEFAULT_TIMEOUT = 5.0
 
 
 
 
@@ -67,19 +68,26 @@ class Initialisation:
     def password(self) -> str:
     def password(self) -> str:
         return self._require("WIKI_PASSWORD")
         return self._require("WIKI_PASSWORD")
 
 
-    @property
-    def ttl(self) -> int:
-        raw = (os.environ.get("CACHE_TTL") or "").strip()
+    def _positive_int(self, name: str, default: int) -> int:
+        raw = (os.environ.get(name) or "").strip()
         if not raw:
         if not raw:
-            return DEFAULT_TTL
+            return default
         try:
         try:
             value = int(raw)
             value = int(raw)
         except ValueError as exc:
         except ValueError as exc:
-            raise ConfigError("CACHE_TTL doit être un entier (secondes).") from exc
+            raise ConfigError(f"{name} doit être un entier.") from exc
         if value <= 0:
         if value <= 0:
-            raise ConfigError("CACHE_TTL doit être strictement positif.")
+            raise ConfigError(f"{name} doit être strictement positif.")
         return value
         return value
 
 
+    @property
+    def ttl(self) -> int:
+        return self._positive_int("CACHE_TTL", DEFAULT_TTL)
+
+    @property
+    def max_entries(self) -> int:
+        return self._positive_int("CACHE_MAX_ENTRIES", DEFAULT_MAX_ENTRIES)
+
     @property
     @property
     def timeout(self) -> float:
     def timeout(self) -> float:
         return DEFAULT_TIMEOUT
         return DEFAULT_TIMEOUT

+ 18 - 5
src/dokuwiki_mcp/services/cache.py

@@ -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):

+ 2 - 1
src/tests/conftest.py

@@ -15,6 +15,7 @@ def wiki_env(monkeypatch: pytest.MonkeyPatch) -> None:
     monkeypatch.setenv("WIKI_USERNAME", "alice")
     monkeypatch.setenv("WIKI_USERNAME", "alice")
     monkeypatch.setenv("WIKI_PASSWORD", "s3cret")
     monkeypatch.setenv("WIKI_PASSWORD", "s3cret")
     monkeypatch.delenv("CACHE_TTL", raising=False)
     monkeypatch.delenv("CACHE_TTL", raising=False)
+    monkeypatch.delenv("CACHE_MAX_ENTRIES", raising=False)
     Initialisation.reset()
     Initialisation.reset()
     ClientHTTP.reset()
     ClientHTTP.reset()
     PageCache.reset()
     PageCache.reset()
@@ -22,5 +23,5 @@ def wiki_env(monkeypatch: pytest.MonkeyPatch) -> None:
     Initialisation.reset()
     Initialisation.reset()
     ClientHTTP.reset()
     ClientHTTP.reset()
     PageCache.reset()
     PageCache.reset()
-    for name in ("BASE_URL", "WIKI_USERNAME", "WIKI_PASSWORD", "CACHE_TTL"):
+    for name in ("BASE_URL", "WIKI_USERNAME", "WIKI_PASSWORD", "CACHE_TTL", "CACHE_MAX_ENTRIES"):
         os.environ.pop(name, None)
         os.environ.pop(name, None)

+ 29 - 0
src/tests/test_client.py

@@ -124,6 +124,35 @@ def test_cache_ttl_expiry(wiki_env: None, monkeypatch: pytest.MonkeyPatch) -> No
     assert cache.get("wiki:spec") == "v2"
     assert cache.get("wiki:spec") == "v2"
 
 
 
 
+def test_cache_lru_eviction(wiki_env: None, monkeypatch: pytest.MonkeyPatch) -> None:
+    monkeypatch.setenv("CACHE_MAX_ENTRIES", "2")
+    Initialisation.reset()
+    PageCache.reset()
+    fetched: list[str] = []
+
+    def handler(request: httpx.Request) -> httpx.Response:
+        body = json.loads(request.content)
+        page = body["params"]["page"]
+        fetched.append(page)
+        return httpx.Response(
+            200,
+            json={"jsonrpc": "2.0", "id": body["id"], "result": page},
+        )
+
+    ClientHTTP.configure(http=httpx.Client(transport=httpx.MockTransport(handler)))
+    cache = PageCache()
+    cache.get("wiki:a")
+    cache.get("wiki:b")
+    cache.get("wiki:a")
+    cache.get("wiki:c")
+    assert list(cache._store) == ["wiki:a", "wiki:c"]
+    fetched.clear()
+    assert cache.get("wiki:a") == "wiki:a"
+    assert fetched == []
+    assert cache.get("wiki:b") == "wiki:b"
+    assert fetched == ["wiki:b"]
+
+
 def test_logs_do_not_contain_secrets(wiki_env: None, tmp_path, caplog: pytest.LogCaptureFixture) -> None:
 def test_logs_do_not_contain_secrets(wiki_env: None, tmp_path, caplog: pytest.LogCaptureFixture) -> None:
     configure_logging(tmp_path)
     configure_logging(tmp_path)