Search code examples
phpcachingtypo3extbasetypo3-9.x

TYPO3 9.5 - QuerieResult Caching in my Extension - function map() on null


I am trying to get a Produclist cache working in my plugin.

My ext_localconf.php

if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['product_cache'])) {
    $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['product_cache'] = [];
}
if( !isset($GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations']['product_cache']['frontend'] ) ) {
    $GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations']['product_cache']['frontend'] = 'TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend';
}

And my Controller

$cache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('product_cache');

if(($products = $cache->get($cacheIdentifier)) === FALSE){

    $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
    $productController = $objectManager->get(ProductController::class);
    $productController->setSettings($this->settings);
    $products = $productController->getEditedProducts($catId);
    $cache->set($cacheIdentifier, $products, ['productajax'], 84600);

}

Normal content like string, int or array works fine, but when i try this with an DatabaseResultquerie than crashed by System with this error: Call to a member function map() on null

enter image description here

(only on get, set works fine)


Solution

  • You cannot cache this class, because that implies serializing it, and the class contains explicit methods that prevent certain properties from being included in the serialized string. In fact, the only property that is included, is query (the input query that caused the result).

    You might be able to cache the QueryResult and then manually call the injection methods to add instances of DataMapper etc. - but even if you did, the serialized QueryResult would not contain the results and would fire again every time you attempt to load an entity from it.

    The right way would be to extract to a not-QueryResult (array, iterator of own choosing) that you know will allow the results to be serialized.

    See: https://github.com/TYPO3/TYPO3.CMS/blob/v8.7.26/typo3/sysext/extbase/Classes/Persistence/Generic/QueryResult.php#L250