{"id":6210,"library":"python-redis-cache","title":"Python Redis Cache","description":"python-redis-cache is a Python library providing a simple decorator for Redis-based caching of function results. It integrates with `redis-py` to store and retrieve data, offering features like TTL (Time To Live) and cache limits. The library is actively maintained, with frequent releases addressing bug fixes, new features, and occasional breaking changes to improve functionality.","status":"active","version":"4.0.2","language":"python","source_language":"en","source_url":"https://github.com/taylorhakes/python-redis-cache","tags":["redis","cache","decorator","performance"],"install":[{"cmd":"pip install python-redis-cache","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for connecting to and interacting with Redis.","package":"redis","optional":false}],"imports":[{"symbol":"RedisCache","correct":"from redis_cache import RedisCache"}],"quickstart":{"code":"import os\nimport redis\nfrom redis_cache import RedisCache\nimport time\n\n# Ensure Redis server is running (e.g., docker run --name some-redis -p 6379:6379 -d redis)\n# Get Redis connection details from environment variables, or use defaults\nREDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')\nREDIS_PORT = int(os.environ.get('REDIS_PORT', '6379'))\nREDIS_DB = int(os.environ.get('REDIS_DB', '0'))\n\ntry:\n    # Connect to Redis. decode_responses=True is often useful.\n    redis_client = redis.StrictRedis(\n        host=REDIS_HOST,\n        port=REDIS_PORT,\n        db=REDIS_DB,\n        decode_responses=True\n    )\n    redis_client.ping() # Test connection\n    print(f\"Successfully connected to Redis at {REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}\")\n\n    # Initialize RedisCache with the Redis client and a TTL (Time To Live)\n    cache = RedisCache(redis_client=redis_client, ttl=30) # Cache entries for 30 seconds\n\n    @cache\n    def get_user_data(user_id: int):\n        \"\"\"Simulates fetching user data from a slow external source.\"\"\"\n        print(f\"Fetching user {user_id} from original source...\")\n        time.sleep(1) # Simulate network delay\n        return {\"id\": user_id, \"name\": f\"User {user_id}\", \"fetch_time\": time.time()}\n\n    print(\"\\n--- First call (should fetch from source) ---\")\n    user_data1 = get_user_data(101)\n    print(f\"User Data 1: {user_data1}\")\n\n    print(\"\\n--- Second call (should be served from cache) ---\")\n    user_data2 = get_user_data(101)\n    print(f\"User Data 2: {user_data2}\") # fetch_time should be the same as user_data1\n\n    print(\"\\n--- Third call (different user, fetches from source) ---\")\n    user_data3 = get_user_data(102)\n    print(f\"User Data 3: {user_data3}\")\n\n    print(\"\\n--- Invalidate cache for user 101 and re-fetch ---\")\n    cache.delete_memoized(get_user_data, 101) # Invalidate specific cache entry\n    user_data4 = get_user_data(101) # Should fetch from source again\n    print(f\"User Data 4: {user_data4}\")\n\nexcept redis.exceptions.ConnectionError as e:\n    print(f\"ERROR: Could not connect to Redis at {REDIS_HOST}:{REDIS_PORT}. \"\n          \"Please ensure Redis server is running. Quickstart cannot proceed.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize `RedisCache` with a `redis-py` client and use the `@cache` decorator on a function. It shows how the cache stores results for subsequent calls with the same arguments, and how to manually invalidate a specific cache entry. Ensure a Redis server is accessible at the specified host and port (or defaults)."},"warnings":[{"fix":"Upgrade your Python environment to 3.8+ if using version 4.0.0 or later.","message":"Version 4.0.0 introduced a breaking change requiring Python 3.8 or newer. It also added support for caching functions with positional-only arguments.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"When upgrading from a version older than 3.0.0, consider flushing your Redis cache if you rely on existing cached data, as it will become inaccessible. New data will be stored in the new format.","message":"Version 3.0.0 changed the key format used for storing cached items. This means cached data stored with older versions will not be accessible or compatible with versions 3.0.0 and newer.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always test your `redis.StrictRedis` or `redis.Redis` connection independently (e.g., with `client.ping()`) before passing it to `RedisCache`. Pay attention to `host`, `port`, `db`, `password`, and `decode_responses` parameters.","message":"Proper configuration of the `redis-py` client is crucial. Common issues include incorrect host/port, authentication failures, or not setting `decode_responses=True` if you expect string outputs directly from Redis, which can lead to byte-string issues in your application logic.","severity":"gotcha","affected_versions":"All"},{"fix":"Be explicit about how `self` affects your cache keys. If you want a class method to be cached globally regardless of instance, you might need to adjust key generation or consider caching outside the method. For instance methods, use `cache.delete_memoized(YourClass.method, obj_instance=your_instance)` for invalidation.","message":"Caching class or instance methods requires careful handling. By default, `self` (the instance itself) is part of the cache key, meaning each instance will have its own cache. When invalidating, you must provide the instance to `delete_memoized` if `self` was part of the key.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'activ':48 'address':53 'base':19 'break':60 'bug':54 'cach':3,7,20,43,66 'chang':61 'data':34 'decor':15,67 'featur':36,57 'fix':55 'frequent':51 'function':22,64 'improv':63 'integr':25 'librari':11,46 'like':37 'limit':44 'live':41 'maintain':49 'new':56 'occasion':59 'offer':35 'perform':68 'provid':12 'py':29 'python':1,5,10 'python-redis-cach':4 'redi':2,6,18,28,65 'redis-bas':17 'redis-pi':27 'releas':52 'result':23 'retriev':33 'simpl':14 'store':31 'time':39 'ttl':38","created_at":"2026-04-14T18:46:45.403362+00:00","updated_at":"2026-04-16T20:27:44.258054+00:00","problems":[{"fix":"pip install python-redis-cache","cause":"The `python-redis-cache` library is not installed or not available in the current Python environment.","error":"ModuleNotFoundError: No module named 'redis_cache'"},{"fix":"import redis\nfrom redis_cache import RedisCache\n\nredis_client = redis.Redis(host='localhost', port=6379, db=0)\ncache = RedisCache(redis_client=redis_client)\n\n@cache.cache()\ndef my_function():\n    return \"cached data\"","cause":"The `RedisCache` class constructor requires an initialized `redis.Redis` client instance as its `redis_client` argument.","error":"TypeError: __init__() missing 1 required positional argument: 'redis_client'"},{"fix":"import redis\nfrom redis_cache import cache\n\n# Configure the globally imported cache instance with a Redis client\ncache.redis_client = redis.Redis(host='localhost', port=6379, db=0)\n\n@cache.cache()\ndef my_function():\n    return \"cached data\"","cause":"The globally imported `cache` decorator (from `from redis_cache import cache`) has not been configured with a `redis_client`, so its internal client remains `None`.","error":"AttributeError: 'NoneType' object has no attribute 'get'"},{"fix":"import redis\nfrom redis_cache import RedisCache\n\nredis_client = redis.Redis(host='localhost', port=6379, db=0)\ncache = RedisCache(redis_client=redis_client)\n\n# To use the cache for a function:\n@cache.cache(ttl=60)\ndef get_user_data(user_id):\n    # ... fetch from DB ...\n    return {\"id\": user_id, \"name\": f\"User {user_id}\"}\n\nuser_data = get_user_data(1) # This retrieves from cache if available\n\n# For direct Redis key operations, use the underlying redis_client instance:\nredis_client.set(\"my_key\", \"my_value\")\nvalue = redis_client.get(\"my_key\")","cause":"The `RedisCache` instance is primarily designed as a decorator factory for functions and for cache management operations (like `clear`, `delete`), not for direct key-value `get`/`set` operations like a raw `redis-py` client.","error":"AttributeError: 'RedisCache' object has no attribute 'get'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"4.0.2","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"http://github.com/taylorhakes/python-redis-cache","docs":null,"changelog":null,"pypi":"https://pypi.org/project/python-redis-cache/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}