Search code examples
cachingtypo3

Typo3 and Frontend Variable in cache


I have typo3 11.5.25. I want to store in cache a variable so i tried to use: TYPO3\CMS\Core\Cache\Frontend\VariableFrontend class

I tried this:

$backend = \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::getBackend();
$cache = new VariableFrontend('VariableFrontend', $backend);
$cache->set("exampleIdentifier","nomebariabile",array("tag-sede"),10000);

I got this error:

Cannot call abstract method TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::getBackend()

There are some things I don't know, how can I store a variable in cache?


Solution

  • I found the solution:

    In my ext_localconf.php

    if( !isset($GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations'][$extensionKey] ) ) {
            $GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations'][$extensionKey] = array();
        }
    
        if( !isset($GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations'][$extensionKey]['frontend'] ) ) {
            $GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations'][$extensionKey]['frontend'] = 'TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend' ;
        }
    
        if( !isset($GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations'][$extensionKey]['options'] ) ) {
            $GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations'][$extensionKey]['options'] = array('defaultLifetime' => 3600);
        }
        if( !isset($GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations'][$extensionKey]['groups'] ) ) {
            $GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations'][$extensionKey]['groups'] = array('pages');
        }
    

    In controller:

    $cacheIdentifier = 'identifier';
    
    $this->cacheInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache('extname');
    if ( $this->cacheInstance->has($cacheIdentifier))
    {
      // Cache available
      $aResult = $this->cacheInstance->get($cacheIdentifier);
    }
    else
    {
       $this->cacheInstance->set($cacheIdentifier, array('myvariable'=>'value'), array('mytag'));
    }
    

    All data is stored in my mysql tables:

    cache_myext_tags
    cache_myext
    

    Hope this may help