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.

Easy Python 3.9+ Aug 9, 2026 Caching & Redis 17 views 0 copies

Python code

33 lines
Python 3.9+
import 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

stdout
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

  1. Use an actual Redis client with MGET to warm from a database snapshot
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Caching & Redis

Related tutorials and quizzes for this topic.