Quick Start¶
Configure as Cache Backend¶
CACHES = {
"default": {
"BACKEND": "django_cachex.cache.ValkeyCache",
"LOCATION": "valkey://127.0.0.1:6379/1",
}
}
Backend Classes¶
| Backend | Description |
|---|---|
ValkeyCache |
Standard Valkey connection |
RedisCache |
Standard Redis connection |
ValkeySentinelCache |
Valkey Sentinel high availability |
RedisSentinelCache |
Redis Sentinel high availability |
ValkeyClusterCache |
Valkey Cluster sharding |
RedisClusterCache |
Redis Cluster sharding |
Valkey and Redis Compatibility
Valkey and Redis are probably still fully compatible, meaning you could use either backend with either server. We recommend Valkey as it remains fully open source.
Connection URL Formats¶
django-cachex uses the valkey-py/redis-py native URL notation for connection strings:
valkey://[[username]:[password]]@localhost:6379/0- Valkey TCP connectionredis://[[username]:[password]]@localhost:6379/0- Redis TCP connectionvalkeys://[[username]:[password]]@localhost:6379/0- Valkey SSL/TLS connectionrediss://[[username]:[password]]@localhost:6379/0- Redis SSL/TLS connectionunix://[[username]:[password]]@/path/to/socket.sock?db=0- Unix socket
Database Selection¶
There are two ways to specify the database number:
- Query string:
valkey://localhost?db=0 - Path (for
valkey://orredis://scheme):valkey://localhost/0
Configure as Session Backend¶
Django can use any cache backend as session storage:
Basic Usage¶
from django.core.cache import cache
# Standard Django cache methods
cache.set("key", "value", timeout=300)
value = cache.get("key")
# Extended data structure methods
cache.hset("user:1", "name", "Alice")
cache.zrange("leaderboard", 0, 10)
# Async versions
await cache.aget("key")
await cache.ahset("user:1", "name", "Alice")
Raw Client Access¶
For operations not exposed by the cache interface: