Appearance
14.6 Caching Strategy & Compiled Template Memory
1. Caching Layers
2. Strategies & Invalidation
- Cache-Aside Pattern: When fetching templates or user preferences, the engine checks local cache, then Redis, and falls back to PostgreSQL on misses.
- Compiled AST Caching: Jinja2 parsed Abstract Syntax Trees are compiled and cached in memory, eliminating string parsing overhead during rendering bursts.
- Cache Invalidation: When an administrator updates or deactivates a template (
api/app.py:481), a Redis Pub/Sub broadcast invalidates the local in-memory cache across all running worker instances.
3. Say It Out Loud
"To minimize database load, we employ a multi-tier caching strategy using the cache-aside pattern. Templates and user preferences are cached in memory using LRU caches with a 5-minute TTL. Compiled Jinja2 bytecode is retained in memory, which reduces template rendering latency to sub-millisecond speeds. When a template is edited, an invalidation message is published to immediately clear stale copies."