{"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":"en","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,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}