Issue Feature Closed

#89054Provide cache frontends via dependency injection

Markus Poerschke 09/01/19, 20:39 updated 12/23/19, 22:57

Description

Instead of injecting the CacheManager, a cache frontend should be injectable directly to a class.

TYPO3 v9 and before:

class MyClass
{
    /**
     * @var TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
     */
    private $cache;

    public function __construct()
    {
        $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
        $this->cache = $cacheManager->getCache('my_cache');
    }
}

This code should become:

class MyClass
{
    /**
     * @var TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
     */
    private $cache;

    public function __construct(FrontendInterface $cache)
    {
        $this->cache = $cache;
    }
}

Since the cache frontends are not added to the service container, the injection has to be configured manually or the CacheManager has to be inejcted.

This feature suggest to add a service for each cache configuration and name it “cache.[NAME OF CONFIGURATION]” to make it available within the service container.

This will allow to configure the service above as follows:

MyClass:
   arguments:
      $cache: '@cache.my_cache'

Reviews 1