{"id":8941,"library":"django-cache-url","title":"Django Cache URL","description":"This simple Django utility allows you to utilize the 12factor inspired CACHE_URL environment variable to configure your Django application. It supports various cache backends like locmem, db, file, memcached, and redis. The library is actively maintained, with a recent release on January 24, 2026.","status":"active","version":"3.4.6","language":"en","source_language":"en","source_url":"https://github.com/epicserve/django-cache-url","tags":["django","cache","configuration","12-factor","environment-variables"],"install":[{"cmd":"pip install django-cache-url","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for using Redis, Rediss, or Hiredis cache backends.","package":"django-redis","optional":true},{"reason":"Required for using the pymemcache cache backend.","package":"pymemcache","optional":true},{"reason":"Required for using the djangopylibmc cache backend (SASL-based memcached).","package":"django-pylibmc","optional":true},{"reason":"Required for using the uwsgicache backend.","package":"uwsgicache","optional":true}],"imports":[{"note":"The primary function to parse a CACHE_URL environment variable.","symbol":"config","correct":"from django_cache_url import config"},{"note":"Used to parse an arbitrary cache URL string, rather than an environment variable.","symbol":"parse","correct":"from django_cache_url import parse"}],"quickstart":{"code":"import os\nfrom django_cache_url import config\n\n# Recommended: Set CACHE_URL in your environment, e.g., in .env file or deployment config\n# Example: export CACHE_URL=\"redis://localhost:6379/1\"\n# Example: export CACHE_URL=\"memcached://127.0.0.1:11211\"\n# Example: export CACHE_URL=\"locmem://\"\n\n# In your Django settings.py:\nCACHES = {\n    'default': config(\n        # Provide a default if CACHE_URL environment variable is not set\n        default=os.environ.get('CACHE_URL', 'locmem://')\n    )\n}\n\n# To parse an arbitrary URL string directly (not from env var):\n# CACHES['custom_cache'] = parse('file:///tmp/django_cache')\n\n# Example using the cache (e.g., in a view or management command)\nfrom django.core.cache import cache\n\ndef my_cached_function():\n    data = cache.get('my_key')\n    if data is None:\n        data = \"Expensive calculation result!\"\n        cache.set('my_key', data, 60*5) # Cache for 5 minutes\n    return data\n\nprint(f\"Cache configured for backend: {CACHES['default']['BACKEND']}\")\nprint(f\"Retrieved data: {my_cached_function()}\")","lang":"python","description":"Configure your Django application's cache settings using the CACHE_URL environment variable, leveraging the `config` function. This example shows how to set up the default cache and a basic usage pattern."},"warnings":[{"fix":"For Redis-only, refer to Django's official documentation on Redis cache configuration. Otherwise, continue using `django-cache-url` for its intended purpose.","message":"For new projects using only Redis cache with Django 4.2 or newer, consider using Django's native `django.core.cache.backends.redis.RedisCache` backend directly, as it offers built-in Redis support. `django-cache-url` remains highly useful for other cache types, unified multi-backend configuration, or legacy projects.","severity":"gotcha","affected_versions":"Django >= 4.2"},{"fix":"Ensure you install the corresponding Python package for the cache backend specified in your `CACHE_URL` (e.g., `pip install django-redis` for `redis://` URLs).","message":"Using certain cache backends (e.g., Redis, Memcached, PyLibMC, UWSGI) via `django-cache-url` requires installing additional Python packages (e.g., `django-redis`, `pymemcache`). The library itself only provides the URL parsing logic, not the backend implementation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement explicit cache invalidation strategies (e.g., `cache.delete()`, `cache.clear()`) or leverage Django's `vary_on_headers` / `vary_on_cookie` decorators for views where content depends on request headers or user context.","message":"Cache invalidation can be complex, especially with URL-based caching. If underlying data changes, a cached URL might serve stale content until its expiry. `django-cache-url` configures the cache, but managing invalidation remains an application-level concern.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the required package: `pip install django-redis` (or `pymemcache`, `django-pylibmc`, `uwsgicache` for other backends specified in your CACHE_URL).","cause":"The Python library for the 'redis' cache backend (django-redis) is not installed.","error":"django.core.exceptions.ImproperlyConfigured: Unknown cache backend: 'redis'"},{"fix":"Set the `CACHE_URL` environment variable (e.g., `export CACHE_URL=\"locmem://\"`) or provide a default directly in `settings.py`: `CACHES = {'default': config(default='locmem://')}`.","cause":"The `config()` function was called without the `CACHE_URL` environment variable being defined, and no default value was provided.","error":"django.core.exceptions.ImproperlyConfigured: CACHE_URL environment variable is not set."},{"fix":"Review Django's caching documentation, especially regarding `vary_on_headers`, `vary_on_cookie`, and `key_prefix`. Ensure that dynamic content that relies on user state or query parameters is properly accounted for in cache key generation, or implement manual cache invalidation where appropriate.","cause":"The cache key generated by Django is not sufficiently distinguishing between different variations of a page (e.g., per user, or based on query parameters), or the cache is not being invalidated when underlying data changes.","error":"Cached pages are not updating correctly or show stale data for different users/requests."}]}