[FEATURE] Add optional caching for skill set data retrieval

This commit is contained in:
Markus Klein 2023-07-07 12:18:44 +02:00
parent 80dcd33175
commit 15ddcb4158
5 changed files with 49 additions and 5 deletions

View file

@ -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;
}
}

View file

@ -0,0 +1 @@
SkilldisplayContent.skillSet.cache = 0

View file

@ -17,6 +17,7 @@ tt_content.skilldisplay_skillset {
dataProcessing {
10 = SkillDisplay\SkilldisplayContent\Frontend\DataProcessing\SkillSets
10 {
cache = {$SkilldisplayContent.skillSet.cache}
skillSets.field = skilldisplay_skillset
}
}

View file

@ -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

View file

@ -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');