Overview
The High IQ API uses a multi-tier caching system that automatically classifies endpoints and applies appropriate cache strategies. Caching is handled at both the CDN (Vercel Edge) and browser levels using standard HTTP cache headers, ETags, and stale-while-revalidate directives.Cache Tiers
Endpoints are automatically classified into one of eight endpoint types, each with its own caching strategy:| Type | TTL | Browser max-age | CDN s-maxage | Stale-While-Revalidate | ETag |
|---|---|---|---|---|---|
| Static | 24 hours | 24 hours | 7 days | 7 days | Strong |
| Catalog | 5 minutes | 5 minutes | 5 minutes | 15 minutes | Strong |
| Detail | 1 hour | 1 hour | 24 hours | 3 days | Strong |
| Search | 1 minute | 1 minute | 1 minute | 5 minutes | Weak |
| AI Generated | 10 minutes | 10 minutes | Private | 30 minutes | Weak |
| User Data | 1 minute | 0 (no browser cache) | Private | None | Weak |
| Analytics | 30 seconds | 0 (no browser cache) | Private | None | Weak |
| Realtime | No cache | No cache | No cache | None | None |
Endpoint Classification
The cache classifier uses URL pattern matching to determine the endpoint type. OnlyGET requests are cached. All other HTTP methods (POST, PUT, DELETE, etc.) bypass the cache entirely.
- Static
- Catalog
- Detail
- Search
- Realtime (No Cache)
Rarely-changing content:
Cache-Control Headers
The API buildsCache-Control headers dynamically based on the endpoint classification:
Header Components
| Directive | Meaning |
|---|---|
public | Response can be stored by any cache (browser, CDN) |
private | Response is user-specific, only browser can cache |
max-age=N | Browser cache duration in seconds |
s-maxage=N | CDN/shared cache duration in seconds |
stale-while-revalidate=N | Serve stale data for N seconds while refreshing in background |
no-store, no-cache, must-revalidate | No caching at all (realtime endpoints) |
Example Headers by Endpoint Type
- Catalog
- Detail
- User Data
- Realtime
ETag Support
The API supports both strong and weak ETags for conditional requests:| ETag Type | Format | Used For |
|---|---|---|
| Strong | "abc123" | Static, catalog, and detail endpoints where byte-level equality matters |
| Weak | W/"abc123" | Search, AI-generated, and user data where semantic equivalence is sufficient |
Conditional Requests
Clients can sendIf-None-Match headers to check if their cached version is still valid:
Conditional Response Optimization
The caching middleware inspects response content and adjusts headers dynamically:| Condition | Cache Behavior |
|---|---|
| Error responses | Cache-Control: no-cache (never cache errors) |
| Empty array results | Short cache: max-age=60 |
| Large list results (100+ items) | Extended cache: max-age doubled |
| Response has timestamp/lastModified | Last-Modified header added |
X-Item-Count header is added to list responses:
YouTube Video Cache
YouTube video data uses a specialized 30-day cache in Supabase to optimize YouTube API quota usage (10,000 units/day limit).| Aspect | Value |
|---|---|
| Cache Table | youtube_video_cache in Supabase |
| TTL | 30 days |
| Quota Savings | ~10x more efficient than pipeline-based fetching |
| Fallback | Returns YouTube search URL when quota is exhausted |
Cache Invalidation
Write operations (POST, PUT, PATCH, DELETE) automatically trigger cache invalidation for related content:
| Path Pattern | Invalidated Tags |
|---|---|
/strains/* | strains, catalog, search |
/users/*, /orders/*, /sessions/* | user_data |
X-Cache-Invalidate response header for CDN integration:
Cache Warming
The API includes proactive cache warming that triggers on approximately 0.1% of requests. It pre-populates cache entries for popular endpoints to reduce cold-cache latency for common queries.Cache Analytics
Cache performance is tracked internally with hit/miss/bypass counts per endpoint type. In development, analytics are periodically logged:X-Cache-Status header indicates the cache outcome:
| Value | Meaning |
|---|---|
HIT | Response served from cache |
MISS | Cache miss, response generated fresh |
| (absent) | Cache bypassed (non-GET, realtime endpoint) |