Sorry, we don't support your browser.  Install a modern browser

Add msg_pack and igbinary support to caches

adding both msg_pack and igbinary support to the existing Cache\Value boils down to adding a few if-clauses.
the benefit of both of these is that they are 2x faster on deserialization (major usecase of caches) and a tiny bit faster and in case of msg_pack smaller than JSON serialization.

and example from my upcoming Turbo plugin

<?php

namespace Bnomei;

use Kirby\Cache\Value;

class TurboValue extends Value
{
    public function toJson(): string
    {
        if (extension_loaded('msgpack')) {
            return msgpack_pack($this->toArray()); // @phpstan-ignore-line
        }

        if (extension_loaded('igbinary')) {
            return igbinary_serialize($this->toArray()) ?: '';
        }

        return parent::toJson();
    }

    public static function fromJson(string $json): ?static
    {
        if (extension_loaded('msgpack')) {
            $data = msgpack_unpack($json); // @phpstan-ignore-line
            if (is_array($data)) {
                return parent::fromArray($data);
            }
        }

        if (extension_loaded('igbinary')) {
            $data = igbinary_unserialize($json);
            if (is_array($data)) {
                return parent::fromArray($data);
            }
        }

        return parent::fromJson($json);
    }
}
2 months ago