diff --git a/kjvstudy_org/routes/timeline.py b/kjvstudy_org/routes/timeline.py index 8bb8e86..f5a9d4e 100644 --- a/kjvstudy_org/routes/timeline.py +++ b/kjvstudy_org/routes/timeline.py @@ -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 diff --git a/kjvstudy_org/stories.py b/kjvstudy_org/stories.py index 4a2d0cb..70987bb 100644 --- a/kjvstudy_org/stories.py +++ b/kjvstudy_org/stories.py @@ -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()