diff --git a/Classes/Frontend/DataProcessing/SkillSets.php b/Classes/Frontend/DataProcessing/SkillSets.php index b7006c3..5fe5113 100644 --- a/Classes/Frontend/DataProcessing/SkillSets.php +++ b/Classes/Frontend/DataProcessing/SkillSets.php @@ -25,6 +25,8 @@ namespace SkillDisplay\SkilldisplayContent\Frontend\DataProcessing; use Exception; use SkillDisplay\PHPToolKit\Api\SkillSet; +use TYPO3\CMS\Core\Cache\CacheManager; +use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface; @@ -45,14 +47,36 @@ class SkillSets implements DataProcessorInterface array $processorConfiguration, array $processedData ) { + $as = $cObj->stdWrapValue('as', $processorConfiguration, 'skillSets'); + $skillSetsIdsRaw = (string)$cObj->stdWrapValue('skillSets', $processorConfiguration); $skillSetIds = GeneralUtility::intExplode( ',', - (string)$cObj->stdWrapValue('skillSets', $processorConfiguration), + $skillSetsIdsRaw, true ); - $skillSets = []; + $enableCache = $processorConfiguration['cache'] ?? false; + if ($enableCache) { + $cacheKey = md5($skillSetsIdsRaw) . $as; + /** @var VariableFrontend $cache */ + $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('sdcontent'); + if ($cache->has($cacheKey)) { + $data = $cache->get($cacheKey); + } else { + $data = $this->generateData($skillSetIds); + $cache->set($cacheKey, $data); + } + } else { + $data = $this->generateData($skillSetIds); + } + $processedData[$as] = $data; + return $processedData; + } + + protected function generateData(array $skillSetIds): array + { + $skillSets = []; foreach ($skillSetIds as $skillSetId) { try { $skillSets[] = $this->skillSetApi->getById($skillSetId, true); @@ -60,8 +84,6 @@ class SkillSets implements DataProcessorInterface continue; } } - - $processedData[$as] = $skillSets; - return $processedData; + return $skillSets; } } diff --git a/Configuration/TypoScript/constants.typoscript b/Configuration/TypoScript/constants.typoscript new file mode 100644 index 0000000..626251c --- /dev/null +++ b/Configuration/TypoScript/constants.typoscript @@ -0,0 +1 @@ +SkilldisplayContent.skillSet.cache = 0 \ No newline at end of file diff --git a/Configuration/TypoScript/setup.typoscript b/Configuration/TypoScript/setup.typoscript index 9e94c32..c8f5b20 100644 --- a/Configuration/TypoScript/setup.typoscript +++ b/Configuration/TypoScript/setup.typoscript @@ -17,6 +17,7 @@ tt_content.skilldisplay_skillset { dataProcessing { 10 = SkillDisplay\SkilldisplayContent\Frontend\DataProcessing\SkillSets 10 { + cache = {$SkilldisplayContent.skillSet.cache} skillSets.field = skilldisplay_skillset } } diff --git a/README.rst b/README.rst index 841aa76..4c584b5 100644 --- a/README.rst +++ b/README.rst @@ -48,6 +48,10 @@ Or copy the files from `/Resources/Private/Templates/ContentElements/` to your e Configuration ============= +It is possible to cache the API results for Skill Sets with this TypoScript constant:: + + SkilldisplayContent.skillSet.cache = 1 + You may include default CSS for the templates in your TypoScript:: page.includeCSS.skilldisplay = EXT:skilldisplay_content/Resources/Public/Css/Styles.css diff --git a/ext_localconf.php b/ext_localconf.php index fbe740f..fb03015 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -25,4 +25,20 @@ // todo v11? $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'][$extKey] = \SkillDisplay\SkilldisplayContent\Backend\Preview::class; + + + $caches = [ + 'sdcontent', + ]; + foreach ($caches as $cacheName) { + $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheName] = [ + 'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, + 'backend' => \TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend::class, + 'options' => [ + 'defaultLifetime' => 2592000, // 30 days + ], + 'groups' => ['lowlevel'] + ]; + } + })('skilldisplay_content');