Skip to content

Commit

Permalink
TASK: Alternate fix for fusion partials cache #4595
Browse files Browse the repository at this point in the history
> This should not be considered an error. It's a cache, things can happen, it should be able to deal with this.

That's why get will be used directly instead of has.

In the rare edge-case of a fusion dsl returning `false` we cannot cache it anymore.
This is an acceptable compromise.
  • Loading branch information
mhsdesign committed Feb 2, 2024
1 parent b4ac8e9 commit a5eedb2
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions Neos.Fusion/Classes/Core/Cache/ParserCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@

use Neos\Flow\Annotations as Flow;
use Neos\Cache\Frontend\VariableFrontend;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Package\PackageManager;
use Neos\Fusion\Core\ObjectTreeParser\Ast\FusionFile;
use Neos\Utility\Unicode\Functions as UnicodeFunctions;
use Neos\Utility\Files;
use Psr\Log\LoggerInterface;

/**
* Helper around the ParsePartials Cache.
Expand All @@ -44,12 +42,6 @@ class ParserCache
*/
protected $packageManager;

/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;

/**
* @Flow\InjectConfiguration(path="enableParsePartialsCache")
*/
Expand All @@ -73,16 +65,6 @@ public function cacheForFusionFile(?string $contextPathAndFilename, \Closure $ge
}
$identifier = $this->getCacheIdentifierForAbsoluteUnixStyleFilePathWithoutDirectoryTraversal($fusionFileRealPath);
$value = $this->cacheForIdentifier($identifier, $generateValueToCache);
if (!$value instanceof FusionFile) {
$this->logger->error(sprintf(
'Unexpected cache error in parser cache while retrieving identifier %s. Expected cache %s with backend %s to return `FusionFile` but got `%s`.',
$identifier,
$this->parsePartialsCache->getIdentifier(),
get_class($this->parsePartialsCache->getBackend()),
get_debug_type($value)
), LogEnvironment::fromMethodName(__METHOD__));
return $generateValueToCache();
}
return $value;
}

Expand All @@ -97,11 +79,16 @@ public function cacheForDsl(string $identifier, string $code, \Closure $generate

private function cacheForIdentifier(string $identifier, \Closure $generateValueToCache): mixed
{
if ($this->parsePartialsCache->has($identifier)) {
return $this->parsePartialsCache->get($identifier);
$value = $this->parsePartialsCache->get($identifier);
if ($value !== false) {
return $value;
}
$value = $generateValueToCache();
$this->parsePartialsCache->set($identifier, $value);
if ($value !== false) {
// in the rare edge-case of a fusion dsl returning `false` we cannot cache it,
// as the above get would be ignored. This is an acceptable compromise.
$this->parsePartialsCache->set($identifier, $value);
}
return $value;
}

Expand Down

0 comments on commit a5eedb2

Please sign in to comment.