How to cache filtered data in Redis with Python
This code caches filtered list results in Redis using an MD5 hash key, returning cached results when available.
pip install redis
Python code
41 linesimport redis
import json
import hashlib
import time
cache = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
def filter_data(data, predicate_key, predicate_value):
"""Filter a list of dicts by key-value pair, with Redis caching."""
cache_key = hashlib.md5(
f"{predicate_key}:{predicate_value}".encode()
).hexdigest()
cached_result = cache.get(cache_key)
if cached_result:
print("Cache HIT")
return json.loads(cached_result)
print("Cache MISS")
filtered = [item for item in data if item.get(predicate_key) == predicate_value]
array_key = f"{cache_key}:{predicate_value}"
cache.setex(cache_key, 300, json.dumps(filtered))
return filtered
if __name__ == "__main__":
users = [
{"id": 1, "name": "Alice", "role": "admin"},
{"id": 2, "name": "Bob", "role": "user"},
{"id": 3, "name": "Charlie", "role": "user"},
{"id": 4, "name": "Diana", "role": "admin"},
]
result1 = filter_data(users, "role", "admin")
print(f"First call: {result1}")
result2 = filter_data(users, "role", "admin")
print(f"Second call: {result2}")
cache.flushdb()
Output
Cache MISS
First call: [{'id': 1, 'name': 'Alice', 'role': 'admin'}, {'id': 4, 'name': 'Diana', 'role': 'admin'}]
Cache HIT
Second call: [{'id': 1, 'name': 'Alice', 'role': 'admin'}, {'id': 4, 'name': 'Diana', 'role': 'admin'}]
How it works
The function generates a cache key using an MD5 hash of the search parameters to ensure uniqueness. On a cache miss, it filters the input data and stores the result in Redis with a 300-second expiration using setex. On subsequent calls, the cached JSON is loaded and returned, avoiding repeated processing. The decode_responses=True option ensures Redis returns strings, making JSON handling straightforward.
Common mistakes
- Using `cache.get` without checking for None, leading to errors on cache misses
- Forgetting to serialize the filtered list to JSON before storing
- Placing the cache lookup after filtering, which defeats the purpose
Variations
- Use `pickle` instead of JSON for arbitrary Python objects
- Implement a TTL based on data freshness rather than a fixed 300 seconds
Real-world use cases
- Caching results of expensive database queries that are frequently repeated with the same filters.
- Reducing latency in API endpoints by serving cached filtered responses for popular filter combinations.
- Storing precomputed subsets of user data in a microservice to avoid recomputation on each request.
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
- Cache Warming with Python: Preload Hot Keys easy
- Coalescing duplicate in-flight requests: one shared result for concurrent callers hard
Keep learning
Related tutorials and quizzes for this topic.