Skip to content

Commit

Permalink
refactor: Simplify object caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 13, 2024
1 parent 43ed976 commit 8d40fca
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/MainTransformer/MainTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public function transform(
throw new TransformerReturnsUnexpectedValueException($targetType, $result);
}

// if the target type is not null, cache it

if ($targetTypeForTransformer !== null) {
$objectCache->saveTarget($source, $targetTypeForTransformer, $result, true);
}

return $result;
}

Expand Down
4 changes: 0 additions & 4 deletions src/MethodMapper/ClassMethodTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ public function transform(

$objectCache = $context->get(ObjectCache::class);

// save to object cache

$objectCache->saveTarget($source, $targetType, $target);

return $result;
}

Expand Down
21 changes: 11 additions & 10 deletions src/ObjectCache/ObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
final class ObjectCache
{
/**
* @var \WeakMap<object,\ArrayObject<string,object>>
* @var \SplObjectStorage<object,\ArrayObject<string,object>>
*/
private \WeakMap $cache;
private \SplObjectStorage $cache;

/**
* @var \WeakMap<object,\ArrayObject<string,true>>
* @var \SplObjectStorage<object,\ArrayObject<string,true>>
*/
private \WeakMap $preCache;
private \SplObjectStorage $preCache;

public function __construct(
private TypeResolverInterface $typeResolver
) {
/** @psalm-suppress MixedPropertyTypeCoercion */
$this->cache = new \WeakMap();
$this->cache = new \SplObjectStorage();
/** @psalm-suppress MixedPropertyTypeCoercion */
$this->preCache = new \WeakMap();
$this->preCache = new \SplObjectStorage();
}

private function isBlacklisted(mixed $source): bool
Expand Down Expand Up @@ -83,7 +83,7 @@ public function preCache(mixed $source, Type $targetType): void
$this->preCache[$source] = $arrayObject;
}

$this->preCache->offsetGet($source)?->offsetSet($targetTypeString, true);
$this->preCache->offsetGet($source)->offsetSet($targetTypeString, true);
}

private function isPreCached(mixed $source, Type $targetType): bool
Expand Down Expand Up @@ -157,7 +157,8 @@ public function getTarget(mixed $source, Type $targetType): mixed
public function saveTarget(
mixed $source,
Type $targetType,
mixed $target
mixed $target,
bool $addIfAlreadyExists = false
): void {
if (!is_object($source) || !is_object($target)) {
return;
Expand All @@ -169,7 +170,7 @@ public function saveTarget(

$targetTypeString = $this->typeResolver->getTypeString($targetType);

if (isset($this->cache[$source][$targetTypeString])) {
if ($addIfAlreadyExists === false && $this->containsTarget($source, $targetType)) {
throw new LogicException(sprintf(
'Target object for source object "%s" and target type "%s" already exists',
get_class($source),
Expand All @@ -183,7 +184,7 @@ public function saveTarget(
$this->cache[$source] = $arrayObject;
}

$this->cache->offsetGet($source)?->offsetSet($targetTypeString, $target);
$this->cache->offsetGet($source)->offsetSet($targetTypeString, $target);
$this->removePrecache($source, $targetType);
}
}
7 changes: 0 additions & 7 deletions src/Transformer/InheritanceMapTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\Exception\InvalidArgumentException;
use Rekalogika\Mapper\Exception\UnexpectedValueException;
use Rekalogika\Mapper\ObjectCache\ObjectCache;
use Rekalogika\Mapper\Transformer\Contracts\MainTransformerAwareInterface;
use Rekalogika\Mapper\Transformer\Contracts\MainTransformerAwareTrait;
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface;
Expand Down Expand Up @@ -111,12 +110,6 @@ public function transform(
);
}

# cache the result. we cache the abstract class/interface as the key.
# the concrete should be cached by whatever transformer that handles it.

$context->get(ObjectCache::class)
->saveTarget($source, $targetType, $result);

return $result;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/UnitTest/Model/ObjectCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rekalogika\Mapper\Tests\UnitTest\Model;

use PHPUnit\Framework\TestCase;
use Rekalogika\Mapper\ObjectCache\Exception\CachedTargetObjectNotFoundException;
use Rekalogika\Mapper\ObjectCache\ObjectCache;
use Rekalogika\Mapper\TypeResolver\TypeResolver;
use Rekalogika\Mapper\Util\TypeFactory;
Expand All @@ -34,7 +35,7 @@ public function testObjectCache(): void
$this->assertTrue($objectCache->containsTarget($source, TypeFactory::int()));
$this->assertSame($target, $objectCache->getTarget($source, TypeFactory::int()));

$this->expectException(\Rekalogika\Mapper\ObjectCache\Exception\CachedTargetObjectNotFoundException::class);
$this->expectException(CachedTargetObjectNotFoundException::class);
$objectCache->getTarget($source, TypeFactory::float());
}
}

0 comments on commit 8d40fca

Please sign in to comment.