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 protocol-compatible, so either backend works with either server. Valkey is recommended as it remains fully open source.
Connection URL Formats¶
Uses the valkey-py/redis-py native URL notation:
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¶
Two ways to specify the database number:
- Query string:
valkey://localhost?db=0 - Path (for
valkey://orredis://scheme):valkey://localhost/0
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 (standard Django methods)
await cache.aget("key")
await cache.aset("key", "value", timeout=300)
Raw Client Access¶
For operations not exposed by the cache interface: