A Cloudflare Workers middleware that implements intelligent caching with AI-driven cache duration and proper content type handling.
- Automated cache duration decisions using AI analysis
- Preserves and restores content types
- Backwards compatible with legacy cache entries
- Robust parsing of AI responses
- Fallback mechanisms for error cases
The middleware stores cache entries as JSON objects with the following structure:
interface CacheEntry {
content: string;
contentType: string;
timestamp: number;
}
- Cache Check: First attempts to retrieve content from cache using the URL pathname as key
- Origin Fetch: If not cached, fetches from origin server
- AI Analysis: Sends content to AI for cache duration recommendation
- Cache Storage: Stores content with metadata in cache using AI-recommended duration
The AI provides cache duration recommendations in this format:
<think>
Analysis reasoning here
</think>
3600
The middleware processes this by:
- Removing the
<think>
tags and their content - Extracting the last numeric value from the remaining text
- Falling back to a default of 1 hour (3600 seconds) if parsing fails
- Content type is preserved from the origin response
- Stored alongside content in cache entry
- Properly restored when serving cached content
- Includes fallback handling for legacy cache entries
Required environment variables:
ORIGIN
: Base URL of the origin serverCACHE
: KV namespace for cache storageAI
: AI service binding for analysis
The middleware includes several fallback mechanisms:
- Default cache duration of 1 hour if AI analysis fails
- Graceful handling of legacy cache entries
- Default content type of 'text/plain' if none provided
// Worker script
import app from './middleware';
export default app;
Uses URL pathname as the cache key, allowing for path-based caching while maintaining query parameter flexibility.
The AI considers:
- Content type (dynamic vs static)
- Time sensitivity
- Update frequency
- Minimal parsing overhead
- Efficient cache entry structure
- Backwards compatibility without performance penalty
- Monitor cache hit rates to verify effectiveness
- Periodically review AI-suggested cache durations
- Consider implementing manual cache invalidation using the timestamp
- Test with various content types to ensure proper handling
Potential improvements to consider:
- Cache variation by query parameters
- Manual cache invalidation endpoints
- Cache warmup mechanisms
- Stale-while-revalidate implementation
- Analytics for cache performance