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.

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

Requires third-party packages — install first
pip install redis

Python code

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

stdout
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

  1. Use `pickle` instead of JSON for arbitrary Python objects
  2. 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

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Caching & Redis

Related tutorials and quizzes for this topic.