Cache Warming with Python: Preload Hot Keys
Demonstrates a simple LRU-like cache with a warm method that preloads hot keys with mock values using OrderedDict.
Python code
33 linesimport time
from collections import OrderedDict
class CacheWarm:
def __init__(self, capacity=3):
self.capacity = capacity
self.cache = OrderedDict()
self.hot_keys = []
def warm(self, keys):
"""Preload hot keys into cache with mock values."""
for key in keys:
if len(self.cache) >= self.capacity:
self.cache.popitem(last=False)
self.cache[key] = f"mock_value_{key}"
self.hot_keys.append(key)
def get(self, key):
if key in self.cache:
self.cache.move_to_end(key)
return self.cache[key]
return None
def display(self):
return list(self.cache.items())
if __name__ == "__main__":
cache = CacheWarm(capacity=3)
cache.warm(["user_101", "product_42", "user_101", "product_99"])
print("Cache state after warming:", cache.display())
print("Get user_101:", cache.get("user_101"))
print("Get missing key:", cache.get("nonexistent"))
print("Hot keys tracked:", cache.hot_keys)
Output
Cache state after warming: [('product_42', 'mock_value_product_42'), ('user_101', 'mock_value_user_101'), ('product_99', 'mock_value_product_99')]
Get user_101: mock_value_user_101
Get missing key: None
Hot keys tracked: ['user_101', 'product_42', 'user_101', 'product_99']
How it works
The warm method preloads keys by inserting them into an OrderedDict. When capacity is reached, popitem(last=False) removes the least recently added item (FIFO behavior), maintaining a bounded cache. move_to_end in get simulates LRU semantics by marking a key as most recently used. The hot_keys list records every warm-up request including duplicates, useful for profiling. This pattern mirrors Redis cache warming where frequently accessed keys are loaded before traffic spikes.
Common mistakes
- Forgetting to handle duplicate keys in warm, causing premature evictions
- Using popitem(last=True) which evicts the most recent item instead of the oldest
- Not tracking hot keys separately for analytics or re-warming
- Assuming warm() is thread-safe without locking
Variations
- Use an actual Redis client with MGET to warm from a database snapshot
- Implement with `functools.lru_cache` decorator for function-level caching
Real-world use cases
- Preloading popular product pages into Redis before a flash sale event to reduce database load.
- Warming session data for top active users at service startup to decrease login latency.
- Prefetching frequently queried configuration keys into application cache after a deployment.
Sponsored
More from Caching & Redis
- Cache Asides in Python with a Read-Through Loader easy
- Cache Data in Redis with Python easy
- Cache Penetration Null Object Mock in Python medium
- Cache Stampede Prevention with SingleFlight in Python medium
- Coalescing duplicate in-flight requests: one shared result for concurrent callers hard
- Consistent Hashing Cache Shard in Python medium
Keep learning
Related tutorials and quizzes for this topic.