Caching
Overview
SpeedPy uses Redis for caching in production, configured via the CACHE_URL environment variable. In development, it falls back to a dummy cache.
Configuration
CACHES = {
"default": env.cache(default="dummycache://"),
}
Cache URL Examples
# Redis (production)
CACHE_URL=redis://redis:6379/1
# Dummy cache (development default)
# Leave CACHE_URL unset
info
The dummy cache accepts all cache operations but doesn't actually store anything. This is useful in development so caching calls don't error but also don't mask stale data.
Using the Cache
Django's cache framework works as normal:
from django.core.cache import cache
# Set a value
cache.set('my_key', 'my_value', timeout=300)
# Get a value
value = cache.get('my_key')
# Delete
cache.delete('my_key')
Cache Decorator
from django.views.decorators.cache import cache_page
@cache_page(60 * 15) # 15 minutes
def my_view(request):
...
Redis in Docker
The same Redis instance is used for both Celery (broker/results) and caching. Use different database numbers to separate them:
# Celery broker
REDIS_URL=redis://redis:6379/0
# Cache
CACHE_URL=redis://redis:6379/1