Compression¶
The compressor option compresses cached values to save server memory:
CACHES = {
"default": {
"BACKEND": "django_cachex.cache.ValkeyCache",
"LOCATION": "valkey://127.0.0.1:6379/1",
"OPTIONS": {
"compressor": "django_cachex.compressors.zstd.ZstdCompressor",
},
}
}
Available Compressors¶
| Compressor | Extra | Default level |
|---|---|---|
django_cachex.compressors.zlib.ZlibCompressor |
(stdlib) | 6 |
django_cachex.compressors.gzip.GzipCompressor |
(stdlib) | 9 |
django_cachex.compressors.lzma.LzmaCompressor |
(stdlib) | preset 4 |
django_cachex.compressors.lz4.Lz4Compressor |
lz4 |
0 (fast mode) |
django_cachex.compressors.zstd.ZstdCompressor |
(stdlib on 3.14+) | 3 |
Install the lz4 extra for Lz4Compressor:
Minimum size and level¶
min_length and level are keyword-only constructor arguments, not keys in
OPTIONS. A compressor stores payloads of min_length bytes or less
uncompressed, and min_length defaults to 256. To change either argument,
configure an instance or a subclass instead of a dotted path:
from django_cachex.compressors.zstd import ZstdCompressor
"OPTIONS": {
"compressor": ZstdCompressor(min_length=1024, level=10),
}
# A subclass stays configurable by dotted path.
class LargeOnlyZstdCompressor(ZstdCompressor):
min_length = 1024
level = 10
Choosing a compressor¶
zstdmatches thelzmaoutput size at a much higher speed and costs little end-to-end throughput. Pick it by default.lz4is the fastest and suits workloads where CPU is the bottleneck. Its output is the largest of the five, and its end-to-end throughput is close to no compression.lzmafits only when output size matters more than write latency. Even then,zstdreaches the same size faster.zlibandgzipare nearly identical. Pickzlibunless an external consumer needs gzip's framing.- No compression gives the highest throughput and uses the most server memory.
Ratios depend on the payload, and already-compressed bytes barely shrink. Benchmarks has the numbers.
Fallback for Migration¶
To migrate between formats, pass a list of compressors. The cache writes with the first and tries each in order on read: