Add JSON caching to stories and timeline modules

Previously, stories.py loaded all story JSON files on every request
and timeline.py loaded biblical_timeline.json on every request.
Now both use module-level caching for better performance.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-02 19:58:14 -05:00
parent 8f63478a5c
commit 91b2e2cab0
2 changed files with 37 additions and 14 deletions
+22 -12
View File
@@ -23,24 +23,34 @@ def init_templates(t: Jinja2Templates):
# Helper Functions
# =============================================================================
def get_biblical_timeline_context():
"""
Load comprehensive biblical timeline data from JSON file.
# Cache timeline data at module level
_TIMELINE_CACHE: dict | None = None
Returns tuple of (timeline_events, introduction, chronology_note, chronology_comparison, conclusion)
"""
# Load timeline data from JSON file
def _load_timeline_data() -> dict:
"""Load timeline data from JSON file (internal, uncached)."""
data_dir = Path(__file__).parent.parent / "data"
timeline_path = data_dir / "biblical_timeline.json"
with open(timeline_path, 'r', encoding='utf-8') as f:
timeline_data = json.load(f)
return json.load(f)
timeline_events = timeline_data.get("timeline_events", {})
introduction = timeline_data.get("introduction", "")
chronology_note = timeline_data.get("chronology_note", "")
chronology_comparison = timeline_data.get("chronology_comparison", [])
conclusion = timeline_data.get("conclusion", "")
def get_biblical_timeline_context():
"""
Load comprehensive biblical timeline data from JSON file (cached).
Returns tuple of (timeline_events, introduction, chronology_note, chronology_comparison, conclusion)
"""
global _TIMELINE_CACHE
if _TIMELINE_CACHE is None:
_TIMELINE_CACHE = _load_timeline_data()
timeline_events = _TIMELINE_CACHE.get("timeline_events", {})
introduction = _TIMELINE_CACHE.get("introduction", "")
chronology_note = _TIMELINE_CACHE.get("chronology_note", "")
chronology_comparison = _TIMELINE_CACHE.get("chronology_comparison", [])
conclusion = _TIMELINE_CACHE.get("conclusion", "")
return timeline_events, introduction, chronology_note, chronology_comparison, conclusion
+15 -2
View File
@@ -3,6 +3,7 @@
Loads all story JSON files and provides access to stories by category and slug.
"""
import json
from functools import lru_cache
from pathlib import Path
from typing import Optional
@@ -11,8 +12,8 @@ from typing import Optional
STORIES_DIR = Path(__file__).parent / "data" / "stories"
def load_all_stories() -> list[dict]:
"""Load all story categories from JSON files."""
def _load_all_stories_uncached() -> list[dict]:
"""Load all story categories from JSON files (internal, uncached)."""
categories = []
if not STORIES_DIR.exists():
@@ -33,6 +34,18 @@ def load_all_stories() -> list[dict]:
return categories
# Load stories once at module import time
_STORIES_CACHE: list[dict] | None = None
def load_all_stories() -> list[dict]:
"""Load all story categories from JSON files (cached)."""
global _STORIES_CACHE
if _STORIES_CACHE is None:
_STORIES_CACHE = _load_all_stories_uncached()
return _STORIES_CACHE
def get_all_stories_flat() -> list[dict]:
"""Get all stories as a flat list with category info attached."""
categories = load_all_stories()