Value Serialization#
Infocyph\CacheLayer\Serializer\ValueSerializer is used by adapters to encode
and decode cached payloads.
What it handles#
scalar values and arrays
closures (via
opis/closure)registered resource types
Core Methods#
serialize(mixed $value): stringunserialize(string $blob): mixedencode(mixed $value, bool $base64 = true): stringdecode(string $payload, bool $base64 = true): mixedwrap(mixed $value): mixedunwrap(mixed $value): mixedregisterResourceHandler(string $type, callable $wrapFn, callable $restoreFn): voidclearResourceHandlers(): void
Resource Handler Example#
use Infocyph\CacheLayer\Serializer\ValueSerializer;
ValueSerializer::registerResourceHandler(
'stream',
function ($res): array {
$meta = stream_get_meta_data($res);
rewind($res);
return [
'mode' => $meta['mode'],
'content' => stream_get_contents($res),
];
},
function (array $data) {
$s = fopen('php://memory', $data['mode']);
fwrite($s, $data['content']);
rewind($s);
return $s;
},
);
Notes#
Registering the same resource type twice throws
InvalidArgumentException.Wrapping/serializing unregistered resources throws
InvalidArgumentException.Closure detection has an internal bounded memo cache.