Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Metadata/Property/Factory/CachedPropertyMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class CachedPropertyMetadataFactory implements PropertyMetadataFactoryInte

private $cacheItemPool;
private $decorated;
private $memoryCache = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$memoryCache is not a very descriptive name... 😞

Perhaps $localCache and $localCacheKey. Descriptive is better.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@teohhanhui however it's exactly what it is, a memory cache.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APCu is also a (shared) memory cache. This is the local memory cache. 😆

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word memory is just redundant and meaningless since all the variables are in memory anyway...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about loaded ?


public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyMetadataFactoryInterface $decorated)
{
Expand All @@ -40,13 +41,18 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyMetad
*/
public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
{
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $property, $options]));
$localKey = serialize([$resourceClass, $property, $options]);
if (isset($this->memoryCache[$localKey])) {
return $this->memoryCache[$localKey];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($localKey);

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);

if ($cacheItem->isHit()) {
return $cacheItem->get();
return $this->memoryCache[$localKey] = $cacheItem->get();
}
} catch (CacheException $e) {
// do nothing
Expand All @@ -55,12 +61,12 @@ public function create(string $resourceClass, string $property, array $options =
$propertyMetadata = $this->decorated->create($resourceClass, $property, $options);

if (!isset($cacheItem)) {
return $propertyMetadata;
return $this->memoryCache[$localKey] = $propertyMetadata;
}

$cacheItem->set($propertyMetadata);
$this->cacheItemPool->save($cacheItem);

return $propertyMetadata;
return $this->memoryCache[$localKey] = $propertyMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class CachedPropertyNameCollectionFactory implements PropertyNameCollectio

private $cacheItemPool;
private $decorated;
private $memoryCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyNameCollectionFactoryInterface $decorated)
{
Expand All @@ -40,13 +41,18 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyNameC
*/
public function create(string $resourceClass, array $options = []): PropertyNameCollection
{
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $options]));
$localKey = serialize([$resourceClass, $options]);
if (isset($this->memoryCache[$localKey])) {
return $this->memoryCache[$localKey];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($localKey);

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);

if ($cacheItem->isHit()) {
return $cacheItem->get();
return $this->memoryCache[$localKey] = $cacheItem->get();
}
} catch (CacheException $e) {
// do nothing
Expand All @@ -55,12 +61,12 @@ public function create(string $resourceClass, array $options = []): PropertyName
$propertyNameCollection = $this->decorated->create($resourceClass, $options);

if (!isset($cacheItem)) {
return $propertyNameCollection;
return $this->memoryCache[$localKey] = $propertyNameCollection;
}

$cacheItem->set($propertyNameCollection);
$this->cacheItemPool->save($cacheItem);

return $propertyNameCollection;
return $this->memoryCache[$localKey] = $propertyNameCollection;
}
}
13 changes: 9 additions & 4 deletions src/Metadata/Resource/Factory/CachedResourceMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class CachedResourceMetadataFactory implements ResourceMetadataFactoryInte

private $cacheItemPool;
private $decorated;
private $memoryCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceMetadataFactoryInterface $decorated)
{
Expand All @@ -40,13 +41,17 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceMetad
*/
public function create(string $resourceClass): ResourceMetadata
{
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass]));
if (isset($this->memoryCache[$resourceClass])) {
return $this->memoryCache[$resourceClass];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass);

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);

if ($cacheItem->isHit()) {
return $cacheItem->get();
return $this->memoryCache[$resourceClass] = $cacheItem->get();
}
} catch (CacheException $e) {
// do nothing
Expand All @@ -55,12 +60,12 @@ public function create(string $resourceClass): ResourceMetadata
$resourceMetadata = $this->decorated->create($resourceClass);

if (!isset($cacheItem)) {
return $resourceMetadata;
return $this->memoryCache[$resourceClass] = $resourceMetadata;
}

$cacheItem->set($resourceMetadata);
$this->cacheItemPool->save($cacheItem);

return $resourceMetadata;
return $this->memoryCache[$resourceClass] = $resourceMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class CachedResourceNameCollectionFactory implements ResourceNameCollectio

private $cacheItemPool;
private $decorated;
private $memoryCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceNameCollectionFactoryInterface $decorated)
{
Expand All @@ -40,11 +41,15 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceNameC
*/
public function create(): ResourceNameCollection
{
if (isset($this->memoryCache[self::CACHE_KEY])) {
return $this->memoryCache[self::CACHE_KEY];
}

try {
$cacheItem = $this->cacheItemPool->getItem(self::CACHE_KEY);

if ($cacheItem->isHit()) {
return $cacheItem->get();
return $this->memoryCache[self::CACHE_KEY] = $cacheItem->get();
}
} catch (CacheException $e) {
// do nothing
Expand All @@ -53,12 +58,12 @@ public function create(): ResourceNameCollection
$resourceNameCollection = $this->decorated->create();

if (!isset($cacheItem)) {
return $resourceNameCollection;
return $this->memoryCache[self::CACHE_KEY] = $resourceNameCollection;
}

$cacheItem->set($resourceNameCollection);
$this->cacheItemPool->save($cacheItem);

return $resourceNameCollection;
return $this->memoryCache[self::CACHE_KEY] = $resourceNameCollection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function testCreateWithItemHit()
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');

$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);
$expectedResult = new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false);
$this->assertEquals($expectedResult, $resultedPropertyMetadata);
$this->assertEquals($expectedResult, $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy'), 'Trigger the local cache');
}

public function testCreateWithItemNotHit()
Expand All @@ -63,7 +65,9 @@ public function testCreateWithItemNotHit()
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');

$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);
$expectedResult = new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false);
$this->assertEquals($expectedResult, $resultedPropertyMetadata);
$this->assertEquals($expectedResult, $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy'), 'Trigger the local cache');
}

public function testCreateWithGetCacheItemThrowsCacheException()
Expand All @@ -81,7 +85,10 @@ public function testCreateWithGetCacheItemThrowsCacheException()
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');

$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);

$expectedResult = new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false);
$this->assertEquals($expectedResult, $resultedPropertyMetadata);
$this->assertEquals($expectedResult, $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy'), 'Trigger the local cache');
}

private function generateCacheKey(string $resourceClass = Dummy::class, string $property = 'dummy', array $options = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public function testCreateWithItemHit()
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);

$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);

$expectedResult = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);
$this->assertEquals($expectedResult, $resultedPropertyNameCollection);
$this->assertEquals($expectedResult, $cachedPropertyNameCollectionFactory->create(Dummy::class), 'Trigger the local cache');
}

public function testCreateWithItemNotHit()
Expand All @@ -63,7 +66,10 @@ public function testCreateWithItemNotHit()
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);

$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);

$expectedResult = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);
$this->assertEquals($expectedResult, $resultedPropertyNameCollection);
$this->assertEquals($expectedResult, $cachedPropertyNameCollectionFactory->create(Dummy::class), 'Trigger the local cache');
}

public function testCreateWithGetCacheItemThrowsCacheException()
Expand All @@ -81,7 +87,10 @@ public function testCreateWithGetCacheItemThrowsCacheException()
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);

$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);

$expectedResult = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);
$this->assertEquals($expectedResult, $resultedPropertyNameCollection);
$this->assertEquals($expectedResult, $cachedPropertyNameCollectionFactory->create(Dummy::class), 'Trigger the local cache');
}

private function generateCacheKey(string $resourceClass = Dummy::class, array $options = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public function testCreateWithItemNotHit()
$resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);

$this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
$this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);

$expectedResult = new ResourceMetadata(null, 'Dummy.');
$this->assertEquals($expectedResult, $resultedResourceMetadata);
$this->assertEquals($expectedResult, $cachedResourceMetadataFactory->create(Dummy::class), 'Trigger the local cache');
}

public function testCreateWithGetCacheItemThrowsCacheException()
Expand All @@ -81,11 +84,14 @@ public function testCreateWithGetCacheItemThrowsCacheException()
$resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);

$this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
$this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);

$expectedResult = new ResourceMetadata(null, 'Dummy.');
$this->assertEquals($expectedResult, $resultedResourceMetadata);
$this->assertEquals($expectedResult, $cachedResourceMetadataFactory->create(Dummy::class), 'Trigger the local cache');
}

private function generateCacheKey(string $resourceClass = Dummy::class)
{
return CachedResourceMetadataFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass]));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@teohhanhui you had a good reason to serialize here? (I think it comes from you)

Do we really need the md5 thing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary because the key must not exceed 64 chars (PSR-6). A long class name may exceed this.

return CachedResourceMetadataFactory::CACHE_KEY_PREFIX.md5($resourceClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public function testCreateWithItemHit()
$resultedResourceNameCollection = $cachedResourceNameCollectionFactory->create();

$this->assertInstanceOf(ResourceNameCollection::class, $resultedResourceNameCollection);
$this->assertEquals(new ResourceNameCollection([Dummy::class]), $resultedResourceNameCollection);

$expectedResult = new ResourceNameCollection([Dummy::class]);
$this->assertEquals($expectedResult, $resultedResourceNameCollection);
$this->assertEquals($expectedResult, $cachedResourceNameCollectionFactory->create(), 'Trigger the local cache');
}

public function testCreateWithItemNotHit()
Expand All @@ -63,7 +66,10 @@ public function testCreateWithItemNotHit()
$resultedResourceNameCollection = $cachedResourceNameCollectionFactory->create();

$this->assertInstanceOf(ResourceNameCollection::class, $resultedResourceNameCollection);
$this->assertEquals(new ResourceNameCollection([Dummy::class]), $resultedResourceNameCollection);

$expectedResult = new ResourceNameCollection([Dummy::class]);
$this->assertEquals($expectedResult, $resultedResourceNameCollection);
$this->assertEquals($expectedResult, $cachedResourceNameCollectionFactory->create(), 'Trigger the local cache');
}

public function testCreateWithGetCacheItemThrowsCacheException()
Expand All @@ -81,6 +87,9 @@ public function testCreateWithGetCacheItemThrowsCacheException()
$resultedResourceNameCollection = $cachedResourceNameCollectionFactory->create();

$this->assertInstanceOf(ResourceNameCollection::class, $resultedResourceNameCollection);
$this->assertEquals(new ResourceNameCollection([Dummy::class]), $resultedResourceNameCollection);

$expectedResult = new ResourceNameCollection([Dummy::class]);
$this->assertEquals($expectedResult, $resultedResourceNameCollection);
$this->assertEquals($expectedResult, $cachedResourceNameCollectionFactory->create(), 'Trigger the local cache');
}
}