A new serialization.igbinary_lz4 service serializes values with igbinary and compresses them with LZ4. LZ4 decompresses several times faster than zlib, at a moderately larger size, which suits cache backends that are read far more often than written.
Requirements
The service requires the LZ4 PHP extension (ext-lz4), version 0.6.0 or later. It must be installed on every PHP environment (web and CLI) that reads or writes the data. The extension is only suggested in composer.json, as the other serializers of the module do not need it.
Without the extension, the service throws a \RuntimeException rather than storing or returning unusable data. It also throws on corrupted LZ4 data.
Usage
Pass the service to the backend, for example the Redis cache backend, in a site services.yml:
services:
cache.backend.redis:
class: Drupal\redis\Cache\CacheBackendFactory
arguments:
- '@redis.factory'
- '@cache_tags.invalidator.checksum'
- '@serialization.igbinary_lz4'
When using it with the Redis cache backend, do not also enable the Redis module's own compression ($settings['redis_compress_length']).
Settings
// Compression level: 0-2 use fast LZ4 (0 is the default), 3-12 use the LZ4 HC
// compressor, which produces smaller values and decompresses equally fast, but
// compresses several times slower. Values outside of 0-12 are clamped.
$settings['igbinary_lz4_compress_level'] = 0;
// Minimum length of the data to compress, shared with the zlib serializers.
// The default is 1000. As LZ4 decompresses much faster than zlib, it may be
// lowered, for example to 500: below around 500 bytes, values usually do not
// get smaller because of the LZ4 frame header.
$settings['igbinary_lz4_compress_length'] = 500;
As with the zlib serializers, a value is stored uncompressed if compressing it does not make it smaller.
Switching serializers
The new serializer reads the values written by all the other serializers of the module, and by the core PHP serializer. Existing data does not need to be cleared when switching to it.
The other way around does not work: only serialization.igbinary_lz4 can read the values it compressed with LZ4. When switching from it to another serializer, clear the cache bins using it, and convert the key-value data first, as when uninstalling the module.
AI disclosure
Generated with the help of an LLM.