Skip to content

Commit

Permalink
Adding a cache key dependency on the configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
shcherbanich committed Nov 1, 2023
1 parent 9c28630 commit 9f969e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
32 changes: 20 additions & 12 deletions src/Core/Parser/Entity/Cache/CacheableEntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,47 @@

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);
}

/**
* @throws InvalidArgumentException
*/
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];
}

Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
8 changes: 1 addition & 7 deletions src/LanguageHandler/Php/Parser/Entity/BaseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) : '';
}
}

0 comments on commit 9f969e0

Please sign in to comment.