PHP
Joomla! 6
Joomla! 5
Joomla! 3 (Legado)
Helper / API
Intermediário
Armazenar e ler dados em cache nativo via CacheControllerFactoryInterface
Guarda resultados custosos (APIs externas ou agregações SQL) no sistema de cache do Joomla respeitando o handler configurado (File, Redis, APCu).
components/com_example/src/Helper/StatsHelper.php
<?php
declare(strict_types=1);
use Joomla\CMS\Cache\CacheControllerFactoryInterface;
use Joomla\CMS\Factory;
$cacheFactory = Factory::getContainer()->get(CacheControllerFactoryInterface::class);
$cache = $cacheFactory->createCacheController('callback', [
'defaultgroup' => 'com_example_stats',
'caching' => true,
'lifetime' => 30, // minutos
]);
$data = $cache->get(static function (int $catId): array {
return ['catid' => $catId, 'generated_at' => gmdate('c')];
}, [15], 'stats_cat_15');
O lifetime é informado em minutos (ex: 30 = 30 minutos).
components/com_example/src/Helper/StatsHelper.php
<?php
$cache = JFactory::getCache('com_example_stats', 'callback');
$cache->setCaching(true);
$cache->setLifeTime(30);
$data = $cache->call(array('MyHelper', 'computeStats'), 15);
Abordagem Joomla 3 com JFactory::getCache().
Explicação técnica e boas práticas
Usar CacheControllerFactoryInterface com o controlador callback garante que sua extensão respeite automaticamente o backend de cache do servidor (File, Redis, Memcached) e seja limpa por cache:clean.