- extractor: trafilatura (precision) -> trafilatura (recall) -> readability-lxml fallback chain; longest result wins; 'thin' flag below 200 chars - orchestrator: Redis-backed scrape-result cache keyed by URL (cache hit skips render entirely); thin/empty results are never cached - /health reports orchestrator Redis reachability Verified: HN + synthetic thin/empty/boilerplate pages; cache hit path via fakeredis. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
"""Redis-backed result cache; degrades to no-op when Redis is unavailable."""
|
|
|
|
import json
|
|
import logging
|
|
|
|
import redis.asyncio as aioredis
|
|
|
|
log = logging.getLogger("orchestrator")
|
|
|
|
|
|
class Cache:
|
|
def __init__(self, redis_url: str | None):
|
|
self._redis = None
|
|
if redis_url:
|
|
self._redis = aioredis.from_url(redis_url, socket_timeout=2, socket_connect_timeout=2)
|
|
else:
|
|
log.warning("no REDIS_URL configured; orchestrator result cache disabled")
|
|
|
|
async def get_json(self, key: str) -> dict | None:
|
|
if not self._redis:
|
|
return None
|
|
try:
|
|
raw = await self._redis.get(key)
|
|
return json.loads(raw) if raw else None
|
|
except Exception:
|
|
return None
|
|
|
|
async def set_json(self, key: str, value: dict, ttl: int) -> None:
|
|
if not self._redis:
|
|
return
|
|
try:
|
|
await self._redis.set(key, json.dumps(value), ex=ttl)
|
|
except Exception:
|
|
pass
|
|
|
|
async def is_ready(self) -> bool:
|
|
if not self._redis:
|
|
return False
|
|
try:
|
|
return await self._redis.ping()
|
|
except Exception:
|
|
return False
|
|
|
|
async def close(self) -> None:
|
|
if self._redis:
|
|
await self._redis.aclose()
|