- WIP on Object serialization

This commit is contained in:
Dave Mc Nicoll 2023-06-13 15:33:21 -04:00
parent 43c0627319
commit 28a4065a69
3 changed files with 60 additions and 2 deletions

View File

@ -2,10 +2,13 @@
namespace Kash; namespace Kash;
use Kash\Serialize\ObjectSerialize;
use Laravel\SerializableClosure\Contracts\Serializable;
use Psr\SimpleCache\CacheException; use Psr\SimpleCache\CacheException;
use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException; use Psr\SimpleCache\InvalidArgumentException;
use Ulmus\EntityCollection;
use function apcu_fetch, apcu_store, apcu_delete, apcu_clear_cache, apcu_exists, apcu_enabled; use function apcu_fetch, apcu_store, apcu_delete, apcu_clear_cache, apcu_exists, apcu_enabled;
class ApcuCache implements CacheInterface class ApcuCache implements CacheInterface
@ -25,18 +28,25 @@ class ApcuCache implements CacheInterface
} }
}; };
} }
} }
public function get(string $key, mixed $default = null) : mixed public function get(string $key, mixed $default = null) : mixed
{ {
$value = apcu_fetch($this->fullKey($key), $status); $value = apcu_fetch($this->fullKey($key), $status);
if ($value instanceof Serialize\ObjectSerialize) {
$value = $value->restore();
}
return $status === true ? $value : $default; return $status === true ? $value : $default;
} }
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null) : bool public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null) : bool
{ {
/*if ( is_object($value) && ! ( $value instanceof Serialize\ObjectSerialize )) {
$value = $this->serializeObject($value);
}*/
return apcu_store($this->fullKey($key), $value, $this->handleTTL($ttl)); return apcu_store($this->fullKey($key), $value, $this->handleTTL($ttl));
} }
@ -102,4 +112,10 @@ class ApcuCache implements CacheInterface
{ {
return $this->invalidator . $this->namespace . static::DEFAULT_NS_SEPARATOR . $key; return $this->invalidator . $this->namespace . static::DEFAULT_NS_SEPARATOR . $key;
} }
protected function serializeObject(object $input)
{
$objSerialize = new ObjectSerialize();
return $objSerialize->transform($input);
}
} }

View File

@ -17,7 +17,7 @@ class ArrayCache implements CacheInterface
protected array $storageTTL = []; protected array $storageTTL = [];
public function __construct( public function __construct(
protected CacheInvalidator $invalidator, protected null|CacheInvalidator $invalidator,
protected string $namespace, protected string $namespace,
protected int $ttl = -1, protected int $ttl = -1,
) { } ) { }

View File

@ -0,0 +1,42 @@
<?php
namespace Kash\Serialize;
class ObjectSerialize
{
public function __construct(
public string|null $objectType = null,
public string|null $objectData = null,
) { }
public function transform(object $object) : self
{
$this->objectType = $object::class;
if ($object instanceof \Serializable) {
$this->objectData = $object->serialize();
}
else {
$this->objectData = serialize($object);
}
return $this;
}
public function restore() : object
{
$cls = $this->objectType;
$obj = new $cls();
if ($object instanceof \Serializable) {
$obj->unserialize($this->objectData);
}
else {
# unserialize()
}
return $obj;
}
}