Skip to content

Commit

Permalink
move serialization responsibility to cache objects
Browse files Browse the repository at this point in the history
  • Loading branch information
EriBloo committed Oct 14, 2024
1 parent 31b0a20 commit 5835293
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 42 deletions.
5 changes: 5 additions & 0 deletions src/Contracts/CacheObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ interface CacheObject
public function key(): CacheKey;

public function ttl(): CarbonInterval;

/**
* @return CacheValueModifier<TValue>
*/
public function modifier(): CacheValueModifier;
}
21 changes: 21 additions & 0 deletions src/Contracts/CacheValueModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace EriBloo\CacheObjects\Contracts;

/**
* @template T
*/
interface CacheValueModifier
{
/**
* @return T
*/
public function onLoad(string $value): mixed;

/**
* @param T $value
*/
public function onSave(mixed $value): string;
}
46 changes: 9 additions & 37 deletions src/Drivers/LaravelDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ public function __construct(

public function set(mixed $value, CacheObject $cacheObject): string
{
$key = $this->prepareKey($cacheObject);
$value = $this->prepareValue($value);
$ttl = $this->prepareTtl($cacheObject);
$key = (string) $cacheObject->key();
$value = $cacheObject->modifier()
->onSave($value);
$ttl = (int) $cacheObject->ttl()
->totalSeconds;

if ($ttl <= 0) {
$this->repository->forever($key, $value);
Expand All @@ -31,49 +33,19 @@ public function set(mixed $value, CacheObject $cacheObject): string

public function get(CacheObject $cacheObject): mixed
{
$key = $this->prepareKey($cacheObject);
$key = (string) $cacheObject->key();
$value = $this->repository->get($key);

if ($value === null) {
return null;
}

return $this->prepareReturn($value);
return $cacheObject->modifier()
->onLoad($value);
}

public function delete(CacheObject $cacheObject): bool
{
return $this->repository->forget($this->prepareKey($cacheObject));
}

/**
* @template TValue
*
* @param CacheObject<TValue> $cacheObject
*/
private function prepareKey(CacheObject $cacheObject): string
{
return (string) $cacheObject->key();
}

private function prepareValue(mixed $value): string
{
return serialize($value);
}

private function prepareReturn(string $value): mixed
{
return unserialize($value);
}

/**
* @template TValue
*
* @param CacheObject<TValue> $cacheObject
*/
private function prepareTtl(CacheObject $cacheObject): int
{
return (int) $cacheObject->ttl()
->totalSeconds;
return $this->repository->forget((string) $cacheObject->key());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace EriBloo\CacheObjects\ValueObjects;
namespace EriBloo\CacheObjects\ValueObjects\Keys;

use EriBloo\CacheObjects\Contracts\CacheKey;
use InvalidArgumentException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace EriBloo\CacheObjects\ValueObjects;
namespace EriBloo\CacheObjects\ValueObjects\Keys;

use EriBloo\CacheObjects\Contracts\CacheKey;

Expand Down
34 changes: 34 additions & 0 deletions src/ValueObjects/Values/SerializeModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace EriBloo\CacheObjects\ValueObjects\Values;

use EriBloo\CacheObjects\Contracts\CacheValueModifier;

/**
* @template TValue
*
* @implements CacheValueModifier<TValue>
*/
final readonly class SerializeModifier implements CacheValueModifier
{
/**
* @param class-string[]|bool $allowedClasses
*/
public function __construct(
private array|bool $allowedClasses = true,
) {}

public function onLoad(string $value): mixed
{
return unserialize($value, [
'allowed_classes' => $this->allowedClasses,
]);
}

public function onSave(mixed $value): string
{
return serialize($value);
}
}
9 changes: 8 additions & 1 deletion tests/Fixtures/BasicCacheObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Carbon\CarbonInterval;
use EriBloo\CacheObjects\Concerns\CacheObjectActions;
use EriBloo\CacheObjects\Contracts\CacheObject;
use EriBloo\CacheObjects\ValueObjects\StringKey;
use EriBloo\CacheObjects\Contracts\CacheValueModifier;
use EriBloo\CacheObjects\ValueObjects\Keys\StringKey;
use EriBloo\CacheObjects\ValueObjects\Values\SerializeModifier;

/**
* @implements CacheObject<string>
Expand All @@ -30,4 +32,9 @@ public function ttl(): CarbonInterval
{
return CarbonInterval::seconds(0);
}

public function modifier(): CacheValueModifier
{
return new SerializeModifier(false);
}
}
11 changes: 9 additions & 2 deletions tests/Fixtures/HashedCacheObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Carbon\CarbonInterval;
use EriBloo\CacheObjects\Concerns\CacheObjectActions;
use EriBloo\CacheObjects\Contracts\CacheObject;
use EriBloo\CacheObjects\ValueObjects\HashedKey;
use EriBloo\CacheObjects\ValueObjects\StringKey;
use EriBloo\CacheObjects\Contracts\CacheValueModifier;
use EriBloo\CacheObjects\ValueObjects\Keys\HashedKey;
use EriBloo\CacheObjects\ValueObjects\Keys\StringKey;
use EriBloo\CacheObjects\ValueObjects\Values\SerializeModifier;

/**
* @implements CacheObject<string>
Expand All @@ -26,4 +28,9 @@ public function ttl(): CarbonInterval
{
return CarbonInterval::seconds(0);
}

public function modifier(): CacheValueModifier
{
return new SerializeModifier(false);
}
}

0 comments on commit 5835293

Please sign in to comment.