PHP
Joomla! 6
Joomla! 5
Joomla! 3 (Legacy)
Helper / API
Intermediate
Store and Read Native Cache via CacheControllerFactoryInterface
Caches expensive computations or external API responses using Joomla native cache handlers (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');
Lifetime is specified in minutes (e.g. 30 equals 30 minutes).
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);
Legacy Joomla 3 version via JFactory::getCache().
Technical explanation and best practices
Using CacheControllerFactoryInterface with the callback controller automatically honors the configured server cache driver and clears cleanly via cli/joomla.php cache:clean.