CompressionHelper
in package
Helper for lossless data compression in Redis cache
Uses gzcompress/gzuncompress (DEFLATE algorithm) which is built into PHP.
Features:
- Automatic compression of large values (above threshold)
- Magic bytes prefix for detecting compressed data (backward compatibility)
- Configurable compression level (1-9)
- Statistics tracking for compression ratio
Magic bytes: "\x1f\x8b\x08\x00" (gzip header signature) This allows automatic detection of compressed vs uncompressed data.
Table of Contents
Constants
- MAGIC_BYTES : mixed = "\x1f\x8b\x08\x00"
- Magic bytes prefix for compressed data Using first 4 bytes of gzip header as signature
Properties
- $compressedCount : int
- Statistics for monitoring compression effectiveness
- $config : Config
- $debugger : Debugger
- $skippedBelowThreshold : int
- $totalCompressedSize : int
- $totalOriginalSize : int
- $uncompressedCount : int
Methods
- __construct() : mixed
- analyze() : array{original_size: int, would_compress: bool, reason?: string, compressed_size?: int, ratio?: string, bytes_saved?: int}
- Estimate compression ratio for given data without actually compressing Useful for debugging and analysis
- compress() : string
- Compress data if compression is enabled and data is large enough
- decompress() : string
- Decompress data if it has compression magic bytes
- getStatistics() : array{enabled: bool, level: int, threshold: int, compressed_count: int, uncompressed_count: int, skipped_below_threshold: int, total_original_bytes: int, total_compressed_bytes: int, average_compression_ratio: string, total_bytes_saved: int}
- Get compression statistics
- isCompressed() : bool
- Check if data is compressed (has magic bytes prefix)
- resetStatistics() : void
- Reset statistics (useful for testing)
Constants
MAGIC_BYTES
Magic bytes prefix for compressed data Using first 4 bytes of gzip header as signature
private
mixed
MAGIC_BYTES
= "\x1f\x8b\x08\x00"
Properties
$compressedCount
Statistics for monitoring compression effectiveness
private
int
$compressedCount
= 0
$config read-only
private
Config
$config
$debugger read-only
private
Debugger
$debugger
$skippedBelowThreshold
private
int
$skippedBelowThreshold
= 0
$totalCompressedSize
private
int
$totalCompressedSize
= 0
$totalOriginalSize
private
int
$totalOriginalSize
= 0
$uncompressedCount
private
int
$uncompressedCount
= 0
Methods
__construct()
public
__construct(Config $config, Debugger $debugger) : mixed
Parameters
analyze()
Estimate compression ratio for given data without actually compressing Useful for debugging and analysis
public
analyze(string $data) : array{original_size: int, would_compress: bool, reason?: string, compressed_size?: int, ratio?: string, bytes_saved?: int}
Parameters
- $data : string
-
Data to analyze
Return values
array{original_size: int, would_compress: bool, reason?: string, compressed_size?: int, ratio?: string, bytes_saved?: int}compress()
Compress data if compression is enabled and data is large enough
public
compress(string $data) : string
Parameters
- $data : string
-
Raw data (typically JSON string)
Return values
string —Compressed data with magic prefix, or original data if compression skipped
decompress()
Decompress data if it has compression magic bytes
public
decompress(string $data) : string
Automatically detects compressed data by magic prefix. Returns original data unchanged if not compressed.
Parameters
- $data : string
-
Data from Redis (may or may not be compressed)
Return values
string —Decompressed data, or original if not compressed
getStatistics()
Get compression statistics
public
getStatistics() : array{enabled: bool, level: int, threshold: int, compressed_count: int, uncompressed_count: int, skipped_below_threshold: int, total_original_bytes: int, total_compressed_bytes: int, average_compression_ratio: string, total_bytes_saved: int}
Return values
array{enabled: bool, level: int, threshold: int, compressed_count: int, uncompressed_count: int, skipped_below_threshold: int, total_original_bytes: int, total_compressed_bytes: int, average_compression_ratio: string, total_bytes_saved: int}isCompressed()
Check if data is compressed (has magic bytes prefix)
public
isCompressed(string $data) : bool
Parameters
- $data : string
-
Data to check
Return values
bool —True if data appears to be compressed
resetStatistics()
Reset statistics (useful for testing)
public
resetStatistics() : void