proxywhirl.cache.manager¶
Cache management for proxy storage with multi-tier support.
This module provides the CacheManager class for managing cached proxies across three storage tiers: L1 (memory), L2 (JSONL files), and L3 (SQLite).
Classes¶
Manages multi-tier caching of proxies with automatic promotion/demotion. |
|
Manages TTL-based expiration with hybrid lazy + background cleanup. |
Module Contents¶
- class proxywhirl.cache.manager.CacheManager(config)[source]¶
Manages multi-tier caching of proxies with automatic promotion/demotion.
Orchestrates caching across three tiers:
L1 (Memory): Fast in-memory cache using OrderedDict (LRU)
L2 (Disk): Persistent cache with configurable backend (JSONL for <10K entries, SQLite for larger caches with O(log n) lookups)
L3 (SQLite): Database cache for cold storage with full queryability
Supports TTL-based expiration, health-based invalidation, and graceful degradation when tiers fail.
Example
>>> # Default JSONL backend >>> config = CacheConfig() >>> manager = CacheManager(config)
>>> # SQLite backend for large caches >>> from proxywhirl.cache.models import L2BackendType >>> config = CacheConfig(l2_backend=L2BackendType.SQLITE) >>> manager = CacheManager(config)
Initialize cache manager with configuration.
- Parameters:
config (proxywhirl.cache.models.CacheConfig) – Cache configuration with tier settings
- clear()[source]¶
Clear all entries from all tiers.
Thread-safe: Uses lock to ensure atomic clearing across all tiers.
- Returns:
Total number of entries cleared
- Return type:
- delete(key)[source]¶
Delete entry from all tiers.
Thread-safe: Uses lock to ensure atomic deletion across all tiers.
- static generate_cache_key(proxy_url)[source]¶
Generate cache key from proxy URL.
Uses SHA256 hash for consistent, URL-safe keys.
- get(key)[source]¶
Retrieve entry from cache with tier promotion.
Checks L1 → L2 → L3 in order. Promotes entries to higher tiers on hit. Updates access_count and last_accessed on successful retrieval.
Thread-safe: Uses lock to prevent race conditions during promotion and expiration.
- Parameters:
key (str) – Cache key to retrieve
- Returns:
CacheEntry if found and not expired, None otherwise
- Return type:
- get_statistics()[source]¶
Get current cache statistics.
- Returns:
CacheStatistics with hit rates, sizes, and tier status
- Return type:
- invalidate_by_health(key)[source]¶
Mark proxy as unhealthy and evict if failure threshold reached.
Increments the failure_count for the proxy and sets health_status to UNHEALTHY. If failure_count reaches the configured failure_threshold, the proxy is removed from all cache tiers.
Thread-safe: Uses lock throughout entire operation to prevent TOCTOU race conditions.
- Parameters:
key (str) – Cache key to invalidate
- Return type:
None
Note
If health_check_invalidation is disabled, still tracks failures but doesn’t evict
Logs at DEBUG level for failures, INFO level for evictions
Updates statistics for health-based evictions
RACE-001 fix: Holds lock throughout entire operation to prevent race between get and update/delete operations
- put(key, entry)[source]¶
Store entry in all enabled tiers.
Writes to all tiers for redundancy. Credentials are automatically redacted in logs.
Thread-safe: Uses lock to ensure atomic writes across all tiers.
- Parameters:
key (str) – Cache key
entry (proxywhirl.cache.models.CacheEntry) – CacheEntry to store
- Returns:
True if stored in at least one tier, False otherwise
- Return type:
- class proxywhirl.cache.manager.TTLManager(cache_manager, cleanup_interval=60)[source]¶
Manages TTL-based expiration with hybrid lazy + background cleanup.
Combines two cleanup strategies: - Lazy expiration: Check TTL on every get() operation - Background cleanup: Periodic scan of all tiers to remove expired entries
Initialize TTL manager.
- Parameters:
cache_manager (CacheManager) – Parent CacheManager instance
cleanup_interval (int) – Seconds between cleanup runs