diff --git a/src/Core/Parser/Entity/Cache/CacheableEntityTrait.php b/src/Core/Parser/Entity/Cache/CacheableEntityTrait.php index 9b5456f6..5bbab53c 100644 --- a/src/Core/Parser/Entity/Cache/CacheableEntityTrait.php +++ b/src/Core/Parser/Entity/Cache/CacheableEntityTrait.php @@ -4,31 +4,39 @@ namespace BumbleDocGen\Core\Parser\Entity\Cache; +use BumbleDocGen\Core\Configuration\Configuration; use DI\Attribute\Inject; use Psr\Cache\InvalidArgumentException; trait CacheableEntityTrait { #[Inject] private EntityCacheStorageHelper $entityCacheStorageHelper; + #[Inject] private Configuration $configuration; - private string $cacheVersion = 'v7'; + private string $entityCacheVersion = 'v1'; + private string $entityCacheKey = ''; private bool $isCacheChanged = false; - abstract public function getCacheKey(): string; - - abstract public function entityCacheIsOutdated(): bool; - - private function getVersionedCacheKey(): string + public function getCacheKey(): string { - return "{$this->cacheVersion}_{$this->getCacheKey()}"; + if (!$this->entityCacheKey) { + $currentRootEntity = $this->getCurrentRootEntity(); + $configVersion = $this->configuration->getConfigurationVersion(); + $this->entityCacheKey = $currentRootEntity ? md5( + $this->entityCacheVersion . $configVersion . $this->getCurrentRootEntity()->getName() + ) : ''; + } + return $this->entityCacheKey; } + abstract public function entityCacheIsOutdated(): bool; + /** * @throws InvalidArgumentException */ protected function getEntityCacheValue(string $key): mixed { - return $this->entityCacheStorageHelper->getItemValueFromCache($this->getVersionedCacheKey(), $key); + return $this->entityCacheStorageHelper->getItemValueFromCache($this->getCacheKey(), $key); } /** @@ -36,7 +44,7 @@ protected function getEntityCacheValue(string $key): mixed */ protected function hasEntityCacheValue(string $key): bool { - $cacheValues = $this->entityCacheStorageHelper->getItemValues($this->getVersionedCacheKey()); + $cacheValues = $this->entityCacheStorageHelper->getItemValues($this->getCacheKey()); return array_key_exists($key, $cacheValues) && is_array($cacheValues[$key]) && $cacheValues[$key]; } @@ -47,7 +55,7 @@ protected function addEntityValueToCache(string $key, mixed $value, int $cacheEx { $this->isCacheChanged = true; $this->entityCacheStorageHelper->addItemValueToCache( - $this->getVersionedCacheKey(), + $this->getCacheKey(), $key, $value, $cacheExpiresAfter @@ -59,7 +67,7 @@ protected function addEntityValueToCache(string $key, mixed $value, int $cacheEx */ public function isEntityDataCacheOutdated(): bool { - $cacheKey = $this->getVersionedCacheKey(); + $cacheKey = $this->getCacheKey(); $values = $this->entityCacheStorageHelper->getItemValues($cacheKey); $usedCacheItemsKeys = $this->entityCacheStorageHelper->getUsedCacheItemsKeys($cacheKey); $isCacheChanged = $this->isCacheChanged || count($values) !== count($usedCacheItemsKeys); @@ -79,7 +87,7 @@ public function isEntityDataCacheOutdated(): bool */ public function removeNotUsedEntityDataCache(): void { - $cacheKey = $this->getVersionedCacheKey(); + $cacheKey = $this->getCacheKey(); $cacheValues = $this->entityCacheStorageHelper->getItemValues($cacheKey); $usedCacheItemsKeys = $this->entityCacheStorageHelper->getUsedCacheItemsKeys($cacheKey); foreach ($cacheValues as $k => $v) { diff --git a/src/LanguageHandler/Php/Parser/Entity/BaseEntity.php b/src/LanguageHandler/Php/Parser/Entity/BaseEntity.php index f3a1d0ea..39b59557 100644 --- a/src/LanguageHandler/Php/Parser/Entity/BaseEntity.php +++ b/src/LanguageHandler/Php/Parser/Entity/BaseEntity.php @@ -467,7 +467,7 @@ public function getDocNote(): string return $this->getReflection()->getDocComment(); } - private function getCurrentRootEntity(): ?RootEntityInterface + protected function getCurrentRootEntity(): ?RootEntityInterface { if (is_a($this, RootEntityInterface::class)) { return $this; @@ -618,10 +618,4 @@ final public function entityCacheIsOutdated(): bool $this->localObjectCache->cacheMethodResult(__METHOD__, $entityName, $entityCacheIsOutdated); return $entityCacheIsOutdated; } - - final public function getCacheKey(): string - { - $currentRootEntity = $this->getCurrentRootEntity(); - return $currentRootEntity ? md5($this->getCurrentRootEntity()->getName()) : ''; - } }