esedic
11/21/2016 - 8:11 AM

Using Joomla JCache

Using Joomla JCache

<?php

// Initialize the output variable
$output = '';

// This will retrieve the data in the cache until it expires:
$cache = JFactory::getCache('com_example', '');
if ((int) $this->params->get('cache', 0))
{
    $cache->setCaching(true);
    $cache->setLifeTime((int) $this->params->get('cache_lifetime', 86400));

    // Create a Cache ID that makes sense for your data 
    // In this case we are caching on the list of provided Category IDs for the current user
    $userId = JFactory::getUser()->id;
    $cacheid = md5($catids.$userId);
    $output = $cache->get($cacheid);
}

if (empty($output))
{
    // You can build up your variable or do your API call in here:
    $output = $api->getData();

    // Then store data in the cache:
    if ((int) $this->params->get('cache', 0))
    {
        $cache->store($output, $cacheid);
    }
}