Hi Mike,
Here's a pattern I've been using in my plugin for some time (this caches whatever variable you'd like):
// 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 my case I was caching on the list of provided Category IDs for the current User...your criteria will likely be different):
$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);
}
}
I hope this helps!
-Omar