Persistent cache implementation for httpx and httpcore following RFC 9111 specification
74
Build a simple HTTP caching system that stores responses with configurable time-to-live (TTL) policies. The system should support both fixed expiration (TTL counted from creation time) and sliding expiration (TTL resets on each access).
The cache should store HTTP responses and automatically expire entries based on TTL settings. Implement a storage backend that:
None for expired entriesNone when retrieved after 2 seconds (entry expired) @test@generates
from typing import Optional, Any
from datetime import datetime
class CacheEntry:
"""Represents a cached HTTP response with metadata."""
def __init__(self, key: str, data: Any, created_at: datetime):
self.key = key
self.data = data
self.created_at = created_at
class CacheStorage:
"""
Storage backend for HTTP cache entries with TTL management.
Args:
default_ttl: Default time-to-live in seconds for cache entries.
None means entries never expire.
refresh_ttl_on_access: If True, enables sliding expiration (TTL resets on access).
If False (default), uses fixed expiration.
"""
def __init__(self, default_ttl: Optional[int] = None, refresh_ttl_on_access: bool = False):
pass
def store(self, key: str, data: Any, ttl_override: Optional[int] = None) -> None:
"""
Store a cache entry.
Args:
key: Unique identifier for the cache entry
data: The data to cache (typically an HTTP response)
ttl_override: Optional TTL in seconds that overrides default_ttl for this entry
"""
pass
def retrieve(self, key: str) -> Optional[CacheEntry]:
"""
Retrieve a cache entry if it exists and hasn't expired.
Args:
key: Unique identifier for the cache entry
Returns:
The CacheEntry if found and not expired, None otherwise.
For sliding expiration, this access updates the entry's timestamp.
"""
passProvides HTTP caching support with TTL management capabilities.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/pypi-hisheldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10