This document describes the implementation of a caching system for Val Town HTTP endpoints using Blob Storage and automated cron jobs. The system provides fast access to endpoint data through a beautiful web interface while maintaining data freshness through scheduled updates.
Architecture
Data Flow
Val Town API → vals-all-files.tsx → cache-manager.ts → Blob Storage
↓
Web Interface ← index.tsx ← cache-manager.ts ← Blob Storage
Storage Strategy
Primary Storage: Val Town Blob Storage (key-value pairs)
Cache Keys:
vals_with_files_cache - Main endpoint data
vals_with_files_cache_metadata - Update timestamps and performance metrics
Data Format: JSON with structured metadata
Components
Core Files
cache-manager.ts
Purpose: Blob storage interface and cache management
Key Functions:
setCachedVals(data, updateDurationMs) - Store data with metadata
getCachedVals() - Retrieve cached endpoint data
getCacheMetadata() - Get update timestamps and metrics
getCachedValsWithMetadata() - Get both data and metadata together
interfaceCacheMetadata {
lastUpdated: string; // ISO timestamptotalVals: number; // Count of vals processedupdateDurationMs: number; // Time taken to fetch and process data
}
cache-updater.ts (Cron Job)
Purpose: Automated cache refresh
Trigger: Cron schedule (set in Val Town UI)
Process:
Fetch fresh data from fetchValsWithFiles()
Calculate update duration
Store in cache with metadata
Log success/failure
Recommended Schedule: 0 * * * * (hourly)
index.tsx
Purpose: Web interface and API endpoints
Routes:
/ - Main HTML interface
/api/cached - JSON cached data
/api/live - JSON live data (bypasses cache)
/api/refresh - Manual cache refresh
/api/cache-info - Cache metadata and status
Supporting Files
vals-all-files.tsx
Purpose: Data fetching logic
Function: fetchValsWithFiles() - Combines val listing and file extraction
Returns: Structured data with vals and their HTTP endpoints
vals.tsx & val-files.tsx
Purpose: Val Town API interfaces
Functions:
fetchVals() - Get all user vals
fetchValFiles(valId) - Get files for specific val
Implementation Details
Cache Population Process
Fetch Vals: Get all user vals from Val Town API
Extract Files: For each val, get file list and filter for HTTP endpoints
Structure Data: Organize into ValWithFiles[] format
Store: Save to Blob Storage with performance metadata
Verify: Confirm successful storage
Performance Characteristics
Cache Hit Response: ~200-500ms
Live Data Response: ~2-5s (depends on Val Town API)
Cache Update Duration: ~300-500ms
Storage Efficiency: JSON compression in Blob Storage
Error Handling
Graceful Degradation: Shows cached data even if stale
Fallback Options: Manual refresh and live data endpoints
Error Logging: Console logging for debugging cron job failures
User Feedback: Clear status indicators in web interface
Web Interface Features
Main Dashboard
Summary Stats: Total vals, endpoints, and update performance
Freshness Indicator: Green (fresh) / Yellow (stale) status dot
Organized Display: Vals grouped with their endpoints
Direct Links: Clickable endpoint URLs opening in new tabs
Visual Design
Framework: TailwindCSS via CDN
Responsive: Mobile-friendly grid layout
Clean UI: Card-based design with proper spacing
Typography: Clear hierarchy with code formatting for filenames
Interactive Elements
Refresh Button: Manual cache update
Live Data Button: Bypass cache for real-time data
Cache Info Link: View detailed metadata
External Links: Direct access to all endpoints
API Endpoints
/api/cached
Method: GET
Response: JSON cached data
Use Case: Programmatic access to cached endpoints
Performance: Fast (~200ms)
/api/live
Method: GET Response: JSON live data from Val Town API
Use Case: Real-time data when cache is stale
Performance: Slow (~2-5s)
/api/refresh
Method: GET
Response: JSON success/failure status
Use Case: Manual cache updates
Returns: Update duration and val count
/api/cache-info
Method: GET
Response: JSON metadata
Use Case: Cache status monitoring
Includes: Last update time, freshness status, performance metrics
Configuration
Cron Job Setup
Navigate to Val Town web interface
Find cache-updater.ts in file list
Set cron schedule (recommended: 0 * * * * for hourly)
Save configuration
Cache Freshness Settings
Default: 24 hours considered fresh
Configurable: Modify maxAgeHours parameter in isCacheFresh()
Visual Indicator: Green dot (fresh) vs Yellow dot (stale)
Storage Limits
Blob Storage: Generous limits for JSON data
Key Strategy: Separate data and metadata for efficient access
Cleanup: No automatic cleanup implemented (manual if needed)
Monitoring and Maintenance
Health Checks
Cache Status: Check /api/cache-info for last update time
Data Integrity: Verify endpoint counts match expectations
Performance: Monitor update duration trends
Troubleshooting
Empty Cache: Run /api/refresh to populate manually
Stale Data: Check cron job execution in Val Town logs
API Errors: Review console logs in cron job execution
Performance Issues: Monitor updateDurationMs in metadata
Monitoring: Built-in performance and freshness tracking
Development Benefits
Debugging: Clear error handling and logging
Extensibility: Modular design for easy enhancements
Documentation: Self-documenting API endpoints
Testing: Manual refresh capability for development
Future Enhancements
Potential Improvements
Cache Invalidation: Smart invalidation based on val modifications
Filtering: Search and filter endpoints by name or val
Analytics: Track endpoint usage and popularity
Notifications: Alert on cache update failures
Backup: Secondary cache storage for redundancy
Scaling Considerations
Pagination: For users with many vals (100+)
Compression: Further optimize storage for large datasets
CDN: Consider CDN caching for global performance
Rate Limiting: Implement if API usage becomes intensive
Conclusion
The caching system successfully addresses the performance and reliability challenges of displaying Val Town HTTP endpoints. With automated updates, comprehensive monitoring, and multiple access methods, it provides a robust foundation for endpoint management and discovery.
The implementation demonstrates best practices for:
Caching Strategy: Appropriate use of Blob Storage
Error Handling: Graceful degradation and fallbacks
User Experience: Fast, informative interface
Maintainability: Clear code structure and documentation
Monitoring: Built-in performance and health metrics