Skip to content

Commit

Permalink
Merge pull request #3318 from neos/bugfix/use-configured-cachefactory
Browse files Browse the repository at this point in the history
BUGFIX: Use configured cache factory
  • Loading branch information
mhsdesign committed Feb 21, 2024
2 parents e7ecdf2 + e128885 commit a97efd9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
16 changes: 0 additions & 16 deletions Neos.Flow/Classes/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ class CacheFactory extends \Neos\Cache\CacheFactory
*/
protected $context;

/**
* A reference to the cache manager
*
* @var CacheManager
*/
protected $cacheManager;

/**
* @var Environment
*/
Expand All @@ -55,15 +48,6 @@ class CacheFactory extends \Neos\Cache\CacheFactory
*/
protected $environmentConfiguration;

/**
* @param CacheManager $cacheManager
* @Flow\Autowiring(enabled=false)
*/
public function injectCacheManager(CacheManager $cacheManager)
{
$this->cacheManager = $cacheManager;
}

/**
* @param EnvironmentConfiguration $environmentConfiguration
* @Flow\Autowiring(enabled=false)
Expand Down
8 changes: 5 additions & 3 deletions Neos.Flow/Classes/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* source code.
*/

use Neos\Cache\CacheFactoryInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Cache\Backend\FileBackend;
use Neos\Cache\Exception\DuplicateIdentifierException;
Expand All @@ -37,7 +38,7 @@
class CacheManager
{
/**
* @var CacheFactory
* @var CacheFactoryInterface
*/
protected $cacheFactory;

Expand Down Expand Up @@ -100,10 +101,10 @@ public function injectLogger(LoggerInterface $logger)
}

/**
* @param CacheFactory $cacheFactory
* @param CacheFactoryInterface $cacheFactory
* @return void
*/
public function injectCacheFactory(CacheFactory $cacheFactory): void
public function injectCacheFactory(CacheFactoryInterface $cacheFactory): void
{
$this->cacheFactory = $cacheFactory;
}
Expand Down Expand Up @@ -496,6 +497,7 @@ protected function createCache(string $identifier): void
$backend = isset($this->cacheConfigurations[$identifier]['backend']) ? $this->cacheConfigurations[$identifier]['backend'] : $this->cacheConfigurations['Default']['backend'];
$backendOptions = isset($this->cacheConfigurations[$identifier]['backendOptions']) ? $this->cacheConfigurations[$identifier]['backendOptions'] : $this->cacheConfigurations['Default']['backendOptions'];
$persistent = isset($this->cacheConfigurations[$identifier]['persistent']) ? $this->cacheConfigurations[$identifier]['persistent'] : $this->cacheConfigurations['Default']['persistent'];
// @phpstan-ignore-next-line - $persistent is not yet part of the CacheFactoryInterface
$cache = $this->cacheFactory->create($identifier, $frontend, $backend, $backendOptions, $persistent);
$this->registerCache($cache, $persistent);
}
Expand Down
16 changes: 11 additions & 5 deletions Neos.Flow/Classes/Core/Booting/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,18 @@ public static function initializeCacheManagement(Bootstrap $bootstrap)
$configurationManager = $bootstrap->getEarlyInstance(ConfigurationManager::class);
$environment = $bootstrap->getEarlyInstance(Environment::class);

$cacheFactoryObjectConfiguration = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_OBJECTS, CacheFactoryInterface::class);
$cacheFactoryClass = isset($cacheFactoryObjectConfiguration['className']) ? $cacheFactoryObjectConfiguration['className'] : CacheFactory::class;
// Workaround to find the correct CacheFactory implementation at compile time.
// We can rely on the $objectConfiguration being ordered by the package names after their loading order.
// Normally this wiring would be done for proxy building a similar way, see ConfigurationBuilder.
$cacheFactoryClass = CacheFactory::class;
$cacheFactoryObjectConfiguration = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_OBJECTS);
foreach ($cacheFactoryObjectConfiguration as $objectConfiguration) {
if (isset($objectConfiguration[CacheFactoryInterface::class]['className'])) {
$cacheFactoryClass = $objectConfiguration[CacheFactoryInterface::class]['className'];
}
}

/** @var CacheFactory $cacheFactory */
/** @var CacheFactoryInterface $cacheFactory */
$cacheFactory = new $cacheFactoryClass($bootstrap->getContext(), $environment, $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Neos.Flow.cache.applicationIdentifier'));

$cacheManager = new CacheManager();
Expand All @@ -369,8 +377,6 @@ public static function initializeCacheManagement(Bootstrap $bootstrap)
$cacheManager->injectEnvironment($environment);
$cacheManager->injectCacheFactory($cacheFactory);

$cacheFactory->injectCacheManager($cacheManager);

$bootstrap->setEarlyInstance(CacheManager::class, $cacheManager);
$bootstrap->setEarlyInstance(CacheFactory::class, $cacheFactory);
}
Expand Down
2 changes: 0 additions & 2 deletions Neos.Flow/Tests/Unit/Cache/CacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ public function createInjectsAnInstanceOfTheSpecifiedBackendIntoTheCacheFrontend
*/
public function aDifferentDefaultCacheDirectoryIsUsedForPersistentFileCaches()
{
$cacheManager = new CacheManager();
$factory = new CacheFactory(new ApplicationContext('Testing'), $this->mockEnvironment, 'UnitTesting');
$factory->injectCacheManager($cacheManager);
$factory->injectEnvironmentConfiguration($this->mockEnvironmentConfiguration);

$cache = $factory->create('Persistent_Cache', VariableFrontend::class, FileBackend::class, [], true);
Expand Down

0 comments on commit a97efd9

Please sign in to comment.