Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-3167: Fixed performance issue in URL Alias cache invalidation #326

Merged
merged 4 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions eZ/Publish/API/Repository/Values/Content/ContentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ public function getMainLanguage(): Language
return $this->mainLanguage;
}

public function getMainLanguageCode(): string
{
return $this->mainLanguageCode;
}

public function getMainLocation(): ?Location
{
return $this->mainLocation;
Expand Down
30 changes: 29 additions & 1 deletion eZ/Publish/API/Repository/Values/Content/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Field extends ValueObject
*
* @todo may be not needed
*
* @var mixed
* @var int
*/
protected $id;

Expand Down Expand Up @@ -57,4 +57,32 @@ class Field extends ValueObject
* @var string
*/
protected $fieldTypeIdentifier;

public function getId(): int
{
return $this->id;
}

public function getFieldDefinitionIdentifier(): string
{
return $this->fieldDefIdentifier;
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}

public function getLanguageCode(): string
{
return $this->languageCode;
}

public function getFieldTypeIdentifier(): string
{
return $this->fieldTypeIdentifier;
}
}
36 changes: 24 additions & 12 deletions eZ/Publish/API/Repository/Values/Content/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,40 @@ class Language extends ValueObject
/**
* The language id (auto generated).
*
* @var mixed
* @var int
*/
protected $id;

/**
* the languageCode code.
*
* @var string
*/
/** @var string */
protected $languageCode;

/**
* Human readable name of the language.
* Human-readable name of the language.
*
* @var string
*/
protected $name;

/**
* Indicates if the language is enabled or not.
*
* @var bool
*/
/** @var bool */
protected $enabled;

public function getId(): int
{
return $this->id;
}

public function getLanguageCode(): string
{
return $this->languageCode;
}

public function getName(): string
{
return $this->name;
}

public function isEnabled(): bool
{
return $this->enabled;
}
}
49 changes: 14 additions & 35 deletions eZ/Publish/Core/Persistence/Cache/Tests/UrlAliasHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,24 @@ public function getHandlerClassName(): string
return SPIUrlAliasHandler::class;
}

public function testPublishUrlAliasForLocation(): void
{
$this->loggerMock->expects(self::once())->method('logCall');

$innerHandlerMock = $this->createMock($this->getHandlerClassName());
$this->persistenceHandlerMock
->expects(self::once())
->method('urlAliasHandler')
->willReturn($innerHandlerMock);

$innerHandlerMock
->expects(self::once())
->method('publishUrlAliasForLocation')
->with(44, 2, 'name', 'eng-GB', true, false);

$innerHandlerMock
->expects(self::once())
->method('listURLAliasesForLocation')
->with(44)
->willReturn([]);

$this->cacheIdentifierGeneratorMock
->expects(self::exactly(3))
->method('generateTag')
->withConsecutive(
['url_alias_location', [44], false],
['url_alias_location_path', [44], false],
['url_alias_not_found', [], false]
)
->willReturnOnConsecutiveCalls('urlal-44', 'urlalp-44', 'urlanf');

$handler = $this->persistenceCacheHandler->urlAliasHandler();
$handler->publishUrlAliasForLocation(44, 2, 'name', 'eng-GB', true, false);
}

public function providerForUnCachedMethods(): array
{
// string $method, array $arguments, array? $tagGeneratingArguments, array? $keyGeneratingArguments, array? $tags, array? $key, ?mixed $returnValue
return [
[
'publishUrlAliasForLocation',
[44, 2, 'name', 'eng-GB', true, false],
[
['url_alias_location', [44], false],
['url_alias_location_path', [44], false],
['url_alias', ['44-abc'], false],
['url_alias_not_found', [], false],
],
null,
['urlal-44', 'urlalp-44', 'urla-44-abc', 'urlanf'],
null,
'44-abc',
],
[
'createCustomUrlAlias',
[44, '1/2/44', true, null, false],
Expand Down
37 changes: 9 additions & 28 deletions eZ/Publish/Core/Persistence/Cache/UrlAliasHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
namespace eZ\Publish\Core\Persistence\Cache;

use eZ\Publish\API\Repository\Exceptions\BadStateException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\SPI\Persistence\Content\UrlAlias;
Expand Down Expand Up @@ -42,7 +41,7 @@ public function publishUrlAliasForLocation(
$languageCode,
$alwaysAvailable = false,
$updatePathIdentificationString = false
) {
): string {
$this->logger->logCall(
__METHOD__,
[
Expand All @@ -54,9 +53,7 @@ public function publishUrlAliasForLocation(
]
);

$urlAliasHandler = $this->persistenceHandler->urlAliasHandler();

$urlAliasHandler->publishUrlAliasForLocation(
$urlAliasIdentity = $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation(
alongosz marked this conversation as resolved.
Show resolved Hide resolved
$locationId,
$parentLocationId,
$name,
Expand All @@ -65,30 +62,14 @@ public function publishUrlAliasForLocation(
$updatePathIdentificationString
);

try {
$existingLocationAliases = $urlAliasHandler->listURLAliasesForLocation($locationId);
} catch (BadStateException $e) {
$existingLocationAliases = [];
}

$existingLocationAliasesTags = [];
foreach ($existingLocationAliases as $existingAlias) {
$existingLocationAliasesTags[] = $this->cacheIdentifierGenerator->generateTag(
self::URL_ALIAS_IDENTIFIER,
[$existingAlias->id]
);
}
$this->cache->invalidateTags([
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_LOCATION_IDENTIFIER, [$locationId]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_LOCATION_PATH_IDENTIFIER, [$locationId]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_IDENTIFIER, [$urlAliasIdentity]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_NOT_FOUND_IDENTIFIER),
]);

$this->cache->invalidateTags(
array_merge(
[
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_LOCATION_IDENTIFIER, [$locationId]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_LOCATION_PATH_IDENTIFIER, [$locationId]),
$this->cacheIdentifierGenerator->generateTag(self::URL_ALIAS_NOT_FOUND_IDENTIFIER),
],
$existingLocationAliasesTags
)
);
return $urlAliasIdentity;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ public function publishUrlAliasForLocation(
$languageCode,
$alwaysAvailable = false,
$updatePathIdentificationString = false
) {
): string {
$languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id;

$this->internalPublishUrlAliasForLocation(
return $this->internalPublishUrlAliasForLocation(
$locationId,
$parentLocationId,
$name,
Expand Down Expand Up @@ -172,7 +172,7 @@ private function internalPublishUrlAliasForLocation(
$alwaysAvailable = false,
$updatePathIdentificationString = false,
$newId = null
) {
): string {
$parentId = $this->getRealAliasId($parentLocationId);
$name = $this->slugConverter->convert($name, 'location_' . $locationId);
$uniqueCounter = $this->slugConverter->getUniqueCounterValue($name, $parentId == 0);
Expand Down Expand Up @@ -300,6 +300,8 @@ private function internalPublishUrlAliasForLocation(
if ($cleanup) {
$this->gateway->cleanupAfterPublish($action, $languageId, $newId, $parentId, $newTextMD5);
}

return $this->mapper->generateIdentityKey($parentId, $newTextMD5);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function extractUrlAliasFromData($data)
$urlAlias = new UrlAlias();

list($type, $destination) = $this->matchTypeAndDestination($data['action']);
$urlAlias->id = $data['parent'] . '-' . $data['text_md5'];
$urlAlias->id = $this->generateIdentityKey((int)$data['parent'], $data['text_md5']);
$urlAlias->pathData = $this->normalizePathData($data['raw_path_data']);
$urlAlias->languageCodes = $this->languageMaskGenerator->extractLanguageCodesFromMask($data['lang_mask']);
$urlAlias->alwaysAvailable = $this->languageMaskGenerator->isAlwaysAvailable($data['lang_mask']);
Expand Down Expand Up @@ -90,6 +90,11 @@ public function extractLanguageCodesFromData(array $rows): array
return $this->languageMaskGenerator->extractLanguageCodesFromMask($languageMask);
}

public function generateIdentityKey(int $parentId, string $hash): string
{
return sprintf('%d-%s', $parentId, $hash);
}

/**
* @throws \RuntimeException
*
Expand Down
13 changes: 8 additions & 5 deletions eZ/Publish/Core/Repository/Values/Content/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ public function getField(string $fieldDefIdentifier, ?string $languageCode = nul
}

foreach ($this->getFields() as $field) {
if ($field->fieldDefIdentifier === $fieldDefIdentifier
&& $field->languageCode === $languageCode) {
if (
$field->getFieldDefinitionIdentifier() === $fieldDefIdentifier
&& $field->getLanguageCode() === $languageCode
) {
return $field;
}
}
Expand All @@ -152,7 +154,8 @@ public function getField(string $fieldDefIdentifier, ?string $languageCode = nul

public function getDefaultLanguageCode(): string
{
return $this->prioritizedFieldLanguageCode ?: $this->versionInfo->contentInfo->mainLanguageCode;
return $this->prioritizedFieldLanguageCode
?? $this->versionInfo->getContentInfo()->getMainLanguageCode();
}

/**
Expand All @@ -170,10 +173,10 @@ public function __get($property)
{
switch ($property) {
case 'id':
return $this->versionInfo->contentInfo->id;
return $this->versionInfo->getContentInfo()->id;

case 'contentInfo':
return $this->versionInfo->contentInfo;
return $this->versionInfo->getContentInfo();

case 'thumbnail':
return $this->getThumbnail();
Expand Down
8 changes: 7 additions & 1 deletion eZ/Publish/SPI/Persistence/Content/UrlAlias/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ interface Handler
* @param string $languageCode
* @param bool $alwaysAvailable
*/
public function publishUrlAliasForLocation($locationId, $parentLocationId, $name, $languageCode, $alwaysAvailable = false);
public function publishUrlAliasForLocation(
$locationId,
$parentLocationId,
$name,
$languageCode,
$alwaysAvailable = false
): string;

/**
* Create a user chosen $alias pointing to $locationId in $languageCode.
Expand Down