{"id":1456,"library":"django-redis","title":"django-redis","description":"django-redis is a full-featured Redis cache backend for Django, providing robust integration with Redis as Django's caching system. The current version is 6.0.0, released in June 2025, and it typically sees several updates per year, following Django's release cycle.","status":"active","version":"6.0.0","language":"en","source_language":"en","source_url":"https://github.com/jazzband/django-redis","tags":["django","redis","cache","backend","web"],"install":[{"cmd":"pip install django-redis","lang":"bash","label":"Base installation"},{"cmd":"pip install django-redis hiredis","lang":"bash","label":"With hiredis for performance"}],"dependencies":[{"reason":"Core Redis client library utilized by django-redis.","package":"redis","optional":false},{"reason":"Optional C-based parser for redis-py, significantly improving performance.","package":"hiredis","optional":true}],"imports":[{"note":"While 'DefaultClient' is a component, 'RedisCache' is the backend class itself, specified in Django settings.py.","wrong":"from django_redis.client import DefaultClient","symbol":"RedisCache","correct":"from django_redis.cache import RedisCache"}],"quickstart":{"code":"# settings.py\nCACHES = {\n    \"default\": {\n        \"BACKEND\": \"django_redis.cache.RedisCache\",\n        \"LOCATION\": \"redis://127.0.0.1:6379/1\",\n        \"OPTIONS\": {\n            \"CLIENT_CLASS\": \"django_redis.client.DefaultClient\",\n            # For redis-py >= 5.0 with hiredis:\n            # \"PARSER_CLASS\": \"redis.connection.HiredisParser\",\n            \"SERIALIZER\": \"django_redis.serializers.json.JSONSerializer\" # Example of changing serializer\n        }\n    }\n}\n\n# app_name/views.py or similar\nfrom django.core.cache import cache\n\ndef my_view(request):\n    value = cache.get('my_key')\n    if value is None:\n        value = 'data_from_db'\n        cache.set('my_key', value, timeout=300) # Cache for 5 minutes\n    return f\"<h1>Cached Value: {value}</h1>\"","lang":"python","description":"To get started, configure the `CACHES` setting in your Django project's `settings.py` file to use `django_redis.cache.RedisCache` as the backend. Specify the Redis connection string in `LOCATION` and any desired `OPTIONS` like client class or serializer. Once configured, you can interact with the cache using Django's standard `django.core.cache.cache` object."},"warnings":[{"fix":"Ensure `pip install hiredis` is run, then add `\"PARSER_CLASS\": \"redis.connection.HiredisParser\"` to the `OPTIONS` dictionary in your `CACHES` configuration.","message":"When using `redis-py` version 5.0 or newer with `hiredis` installed, you must explicitly set `PARSER_CLASS` in `CACHES OPTIONS` to `redis.connection.HiredisParser` for `hiredis` to be utilized correctly. Failure to do so may result in `hiredis` not being used, impacting performance.","severity":"gotcha","affected_versions":"5.4.0+ with redis-py >= 5.0"},{"fix":"Consult the `django-redis` GitHub releases or documentation (e.g., `requires_python` field) for the supported Django versions before upgrading either library.","message":"Always verify `django-redis` compatibility with your specific Django version. New `django-redis` versions often introduce support for newer Django versions, while dropping support for older ones. For example, Django 4.x support was added in `django-redis` 5.3.0.","severity":"breaking","affected_versions":"All versions"},{"fix":"For improved security and interoperability, configure a different serializer, such as JSON, by setting `\"SERIALIZER\": \"django_redis.serializers.json.JSONSerializer\"` in your `CACHES OPTIONS`.","message":"By default, `django-redis` uses Python's `pickle` module for serialization. This can pose security risks if untrusted data is deserialized, and limits interoperability with non-Python clients or services that expect common formats like JSON.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Double-check the Redis URI format in your `settings.py` (e.g., `redis://127.0.0.1:6379/1`). Ensure the Redis server is running and accessible from the Django application's host and port.","message":"Incorrect `LOCATION` string format or inaccessible Redis server can lead to `ConnectionError` or `TimeoutError`. The format is crucial (e.g., `redis://host:port/db_number`).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}